// stores/modalStore.ts
// 全局唯一弹出层 confirm 确认框，View dom 嵌入在 page-wrapper 组件内
// 暂时不支持多个弹出层同时弹出
import { create } from 'zustand';


type ModalConfig ={
  content: React.ReactNode;
  confirmText?: string,
  cancelText?: string,
  beforeClose?: () => Promise<void> | void; // 用户自定义关闭前回调
  onCancel?: () => Promise<void> | void; // 用户自定义关闭前回调
  showCancelButton?: boolean
  onConfirm?: () => Promise<void> | void; // 用户自定义关闭前回调
}
type ModalStore = {
  isVisible: boolean;
  config: ModalConfig | null;
  actions: {
    showModal: (config:ModalConfig) => void;
    hideModal: () => void;
    onConfirm: ()=> void;
    onCancel: ()=> void;
  };
};

export const useModalStore = create<ModalStore>((set, get) => ({
  isVisible: false,
  config: null,
  actions: {
    showModal: (config) => set({ isVisible: true, config }),
    hideModal: () => set({ isVisible: false }),
    onConfirm: () => {
      get().config?.onConfirm?.()
      get().actions.hideModal();
    },
    onCancel: () => {
      get().config?.onCancel?.()
      get().actions.hideModal();
    },
  },
}));

export const useModalStoreActions = () => {
  const actions = useModalStore(state => state.actions);
  return actions;
};
