import{o as e}from"./rolldown-runtime-C_s2cVnS.js";import{a as t}from"./i18n-CsN8OUP9.js";import{h as n,m as r}from"./vendor-react-nmZsNlRz.js";import{t as i}from"./IconAlertTriangle-C0hVHaw_.js";import{t as a}from"./IconCheck-CTnHqwr6.js";import{n as o}from"./IconSearch-K9MAHGUb.js";import{t as s}from"./button-C_jHykqW.js";import{a as c,i as l,o as u,r as d,s as f,t as p}from"./dialog-B4nc4iPJ.js";var m=e(n(),1),h=e(r(),1);function g({state:e,idle:t,busy:n,done:r,idleIcon:i=null}){return(0,h.jsxs)(h.Fragment,{children:[e===`saving`?(0,h.jsx)(o,{className:`animate-spin`}):e===`done`?(0,h.jsx)(a,{}):i,e===`saving`?n:e===`done`?r:t]})}function _({open:e,onClose:n,onConfirm:r,title:a,description:o,confirmLabel:_,busyLabel:v,doneLabel:y,cancelLabel:b,destructive:x=!1,dismissOnConfirm:S=!1,secondaryAction:C}){let{t:w}=t(),T=(0,m.useRef)(null),[E,D]=(0,m.useState)(`idle`),[O,k]=(0,m.useState)(null),A=E===`saving`||E===`done`,j=_??w.common.confirm,M=v??w.common.loading,N=y??w.common.done,P=b??w.common.cancel;(0,m.useEffect)(()=>{e&&(D(`idle`),k(null))},[e]);async function F(){if(!A){if(k(null),S){try{await r(),n()}catch(e){k(e instanceof Error?e.message:w.errors.genericFailure)}return}D(`saving`);try{await r(),D(`done`),window.setTimeout(n,600)}catch(e){D(`idle`),k(e instanceof Error?e.message:w.errors.genericFailure)}}}return(0,h.jsx)(p,{onOpenChange:e=>!e&&!A&&n(),open:e,children:(0,h.jsxs)(d,{className:`max-w-md`,onKeyDown:e=>{(e.key===`Enter`||e.key===` `)&&!A&&(e.preventDefault(),F())},onOpenAutoFocus:e=>{e.preventDefault(),T.current?.focus()},children:[(0,h.jsxs)(u,{children:[(0,h.jsx)(f,{children:a}),o?(0,h.jsx)(l,{children:o}):null]}),O&&(0,h.jsxs)(`div`,{className:`flex items-start gap-2 rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-xs text-destructive`,children:[(0,h.jsx)(i,{className:`mt-0.5 size-3.5 shrink-0`}),(0,h.jsx)(`span`,{children:O})]}),(0,h.jsxs)(c,{children:[(0,h.jsx)(s,{disabled:A,onClick:n,type:`button`,variant:`ghost`,children:P}),C&&(0,h.jsx)(s,{disabled:A,onClick:()=>{C.onClick(),n()},type:`button`,variant:`secondary`,children:C.label}),(0,h.jsx)(s,{disabled:A,onClick:()=>void F(),ref:T,variant:x?`destructive`:`default`,children:(0,h.jsx)(g,{busy:M,done:N,idle:j,state:E})})]})]})})}export{g as n,_ as t};