import{o as e}from"./rolldown-runtime-BTPm5ob3.js";import{r as t}from"./animations-BBry1XJJ.js";var n=e(t(),1);function r({onSave:e,autoSaveDelay:t=1500}){let[r,i]=(0,n.useState)(`idle`),[a,o]=(0,n.useState)(!1),[s,c]=(0,n.useState)(!1),l=(0,n.useRef)(!0);(0,n.useEffect)(()=>()=>{l.current=!1},[]);let u=(0,n.useCallback)(async(t=!0)=>{t||c(!0),i(`saving`);try{let n=await e(t);l.current&&(n?(o(!1),i(`saved`),setTimeout(()=>{l.current&&i(`idle`)},3e3)):i(`error`))}catch(e){console.error(`Settings save error:`,e),l.current&&i(`error`)}finally{l.current&&!t&&c(!1)}},[e]);return(0,n.useEffect)(()=>{if(!a)return;let e=setTimeout(()=>{u(!0)},t);return()=>clearTimeout(e)},[a,u,t]),(0,n.useEffect)(()=>{let e=e=>{a&&(e.preventDefault(),e.returnValue=``)};return window.addEventListener(`beforeunload`,e),()=>window.removeEventListener(`beforeunload`,e)},[a]),{saveStatus:r,hasUnsavedChanges:a,isSaving:s,handleSave:u,markDirty:(0,n.useCallback)(()=>{o(!0)},[]),markClean:(0,n.useCallback)(()=>{o(!1)},[])}}export{r as t};