{"version":3,"file":"useCloseGuard-NAW7yZL7.cjs","sources":["../src/utils/useCloseGuard.ts"],"sourcesContent":["import { useCallback, useState } from 'react';\n\nexport interface CloseGuardProps {\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  dirty?: boolean;\n  onBeforeClose?: () => boolean | Promise<boolean>;\n}\n\n/**\n * Wraps Radix open-state so a close attempt (Esc, backdrop, X, Close) can be\n * vetoed. Always controlled internally, so blocking also works uncontrolled.\n * Guard is active when `dirty` is true, or when `onBeforeClose` is set and\n * `dirty` is left undefined. Active guard without `onBeforeClose` blocks outright.\n */\nexport function useCloseGuard({ open, defaultOpen, onOpenChange, dirty, onBeforeClose }: CloseGuardProps) {\n  const [inner, setInner] = useState(!!defaultOpen);\n  const isControlled = open !== undefined;\n  const current = isControlled ? open : inner;\n  const guarded = dirty === true || (dirty === undefined && !!onBeforeClose);\n\n  const commit = useCallback((next: boolean) => {\n    if (!isControlled) setInner(next);\n    onOpenChange?.(next);\n  }, [isControlled, onOpenChange]);\n\n  const handleOpenChange = useCallback((next: boolean) => {\n    if (next || !guarded) return commit(next);\n    if (!onBeforeClose) return;\n    const res = onBeforeClose();\n    if (res instanceof Promise) void res.then((ok) => { if (ok !== false) commit(false); });\n    else if (res !== false) commit(false);\n  }, [guarded, onBeforeClose, commit]);\n\n  return { open: current, onOpenChange: handleOpenChange };\n}\n"],"names":["useCloseGuard","open","defaultOpen","onOpenChange","dirty","onBeforeClose","inner","setInner","useState","isControlled","current","guarded","commit","useCallback","next","handleOpenChange","res","ok"],"mappings":"mDAgBO,SAASA,EAAc,CAAE,KAAAC,EAAM,YAAAC,EAAa,aAAAC,EAAc,MAAAC,EAAO,cAAAC,GAAkC,CACxG,KAAM,CAACC,EAAOC,CAAQ,EAAIC,EAAAA,SAAS,CAAC,CAACN,CAAW,EAC1CO,EAAeR,IAAS,OACxBS,EAAUD,EAAeR,EAAOK,EAChCK,EAAUP,IAAU,IAASA,IAAU,QAAa,CAAC,CAACC,EAEtDO,EAASC,cAAaC,GAAkB,CACvCL,GAAcF,EAASO,CAAI,EAChCX,GAAA,MAAAA,EAAeW,EACjB,EAAG,CAACL,EAAcN,CAAY,CAAC,EAEzBY,EAAmBF,cAAaC,GAAkB,CACtD,GAAIA,GAAQ,CAACH,EAAS,OAAOC,EAAOE,CAAI,EACxC,GAAI,CAACT,EAAe,OACpB,MAAMW,EAAMX,EAAA,EACRW,aAAe,QAAcA,EAAI,KAAMC,GAAO,CAAMA,IAAO,IAAOL,EAAO,EAAK,CAAG,CAAC,EAC7EI,IAAQ,IAAOJ,EAAO,EAAK,CACtC,EAAG,CAACD,EAASN,EAAeO,CAAM,CAAC,EAEnC,MAAO,CAAE,KAAMF,EAAS,aAAcK,CAAA,CACxC"}