// ~/composables/confirm.ts
import { reactive, readonly } from "vue";

// یک state مشترک برای تمام کامپوننت‌ها
const confirmState = reactive({
  isOpen: false,
  title: "تأیید عملیات",
  message: "آیا از انجام این عملیات اطمینان دارید؟",
  resolve: null as ((value: boolean) => void) | null,
});

export const useConfirmState = () => readonly(confirmState);

export const useConfirm = () => {
  const ucoShowConfirmModal = (
    options: { title?: string; message?: string } = {}
  ): Promise<boolean> => {
    confirmState.title = options.title || "تأیید عملیات";
    confirmState.message =
      options.message || "آیا از انجام این عملیات اطمینان دارید؟";
    confirmState.isOpen = true;

    return new Promise((resolve) => {
      confirmState.resolve = resolve;
    });
  };

  return { ucoShowConfirmModal };
};

export const useConfirmActions = () => {
  const ucoConfirm = () => {
    confirmState.isOpen = false;
    if (confirmState.resolve) confirmState.resolve(true);
    confirmState.resolve = null;
  };

  const ucoCancelConfirmModal = () => {
    confirmState.isOpen = false;
    if (confirmState.resolve) confirmState.resolve(false);
    confirmState.resolve = null;
  };

  return { ucoConfirm, ucoCancelConfirmModal };
};
