import {defineStore} from "pinia"; import {ref} from "vue"; export interface ConfirmOptions { title?: string; message: string; confirmLabel?: string; cancelLabel?: string; danger?: boolean; } export const useConfirmStore = defineStore('confirm', () => { const open = ref(false); const options = ref({message: ""}); let resolver: ((value: boolean) => void) | undefined; const settle = (value: boolean) => { open.value = false; resolver?.(value); resolver = undefined; }; const confirm = (opts: ConfirmOptions | string): Promise => { options.value = typeof opts === "string" ? {message: opts} : opts; open.value = true; return new Promise(resolve => { resolver = resolve; }); }; const accept = () => settle(true); const cancel = () => settle(false); return {open, options, confirm, accept, cancel}; }); export const confirm = (opts: ConfirmOptions | string): Promise => useConfirmStore().confirm(opts);