import { create } from 'zustand'

export type ConfirmVariant = 'danger' | 'warning' | 'info'

interface ConfirmOptions {
  title: string
  message: string
  confirmText?: string
  cancelText?: string
  variant?: ConfirmVariant
}

interface ConfirmState {
  isOpen: boolean
  options: ConfirmOptions | null
  resolve: ((value: boolean) => void) | null

  confirm: (options: ConfirmOptions) => Promise<boolean>
  handleConfirm: () => void
  handleCancel: () => void
}

export const useConfirmStore = create<ConfirmState>((set, get) => ({
  isOpen: false,
  options: null,
  resolve: null,

  confirm: (options) => {
    return new Promise<boolean>((resolve) => {
      set({
        isOpen: true,
        options,
        resolve,
      })
    })
  },

  handleConfirm: () => {
    const { resolve } = get()
    if (resolve) resolve(true)
    set({ isOpen: false, options: null, resolve: null })
  },

  handleCancel: () => {
    const { resolve } = get()
    if (resolve) resolve(false)
    set({ isOpen: false, options: null, resolve: null })
  },
}))

// Convenient hook
export const useConfirm = () => {
  const confirm = useConfirmStore((state) => state.confirm)
  return confirm
}
