import { create } from 'zustand'

interface PwdState {
  open: boolean
  title: string
  resolve: ((v: string | null) => void) | null
}

export const usePwd = create<PwdState>(() => ({ open: false, title: '', resolve: null }))

/** 弹出密码输入框，返回用户输入的密码；取消返回 null */
export function askPassword(title = '请输入工程文件密码'): Promise<string | null> {
  return new Promise((resolve) => usePwd.setState({ open: true, title, resolve }))
}

export function answerPassword(v: string | null): void {
  const r = usePwd.getState().resolve
  usePwd.setState({ open: false, resolve: null })
  r?.(v)
}
