import"./DsnmJJEf.js";import{p as X,e as j,f as R,a as E,b as z,g as e,h as T,d as M,r as G,t as I,c as y,j as n,i as A,m as W}from"./BfUaJvxP.js";import{p as w,i as B}from"./C8yDvkQM.js";import{s as _,t as H}from"./CuIpA3jY.js";import{i as N}from"./CF1ujZlL.js";import{L as F}from"./BeSnoT4y.js";import{C as P}from"./DZHO7bvc.js";import{b as K,g as Y}from"./TWCB2oXp.js";import{p as L}from"./CCm9s9yb.js";var q=y('<span class="inline-flex h-7 w-7 items-center justify-center rounded-md border border-border bg-card text-muted-foreground"><!></span>'),Q=y('<span class="inline-flex h-7 w-7 items-center justify-center rounded-md border border-emerald-500/30 bg-emerald-500/10 text-emerald-600 dark:text-emerald-400"><!></span>'),Z=y('<span class="inline-flex h-7 w-7 items-center justify-center rounded-md border border-amber-500/30 bg-amber-500/10"><span class="relative flex h-2 w-2"><span class="absolute inline-flex h-full w-full animate-ping rounded-full bg-amber-400 opacity-70"></span> <span class="relative inline-flex h-2 w-2 rounded-full bg-amber-500"></span></span></span>');function ue(v,a){X(a,!0);let f=w(a,"saving",3,!1),m=w(a,"lastSavedAt",3,null),p=w(a,"autoSaveEnabled",3,!1);const l=T(()=>p()&&f()),t=T(()=>p()&&!f()&&!a.hasChanges&&m()!=null),h=T(()=>a.hasChanges&&!e(l));var g=j(),D=R(g);{var s=u=>{var r=q(),d=M(r);F(d,{size:12,class:"animate-spin"}),G(r),I((S,b)=>{_(r,"title",S),_(r,"aria-label",b)},[()=>N.t("ADMIN_NEXT.UNSAVED_INDICATOR.SAVING_CHANGES"),()=>N.t("ADMIN_NEXT.UNSAVED_INDICATOR.SAVING_CHANGES_2")]),E(u,r)},i=u=>{var r=Q(),d=M(r);P(d,{size:12,strokeWidth:2.5}),G(r),I((S,b)=>{_(r,"title",S),_(r,"aria-label",b)},[()=>N.t("ADMIN_NEXT.ALL_CHANGES_SAVED"),()=>N.t("ADMIN_NEXT.ALL_CHANGES_SAVED")]),E(u,r)},c=u=>{var r=Z();I((d,S)=>{_(r,"title",d),_(r,"aria-label",S)},[()=>N.t("ADMIN_NEXT.UNSAVED_INDICATOR.UNSAVED_CHANGES_CLICK_SAVE_TO_PERSIST"),()=>N.t("ADMIN_NEXT.UNSAVED_INDICATOR.YOU_HAVE_UNSAVED_CHANGES")]),E(u,r)};B(D,u=>{e(l)?u(s):e(t)?u(i,1):e(h)&&u(c,2)})}E(v,g),z()}function fe(v){let a=A(!1),f=A(null),m=!1;return K(({cancel:p,to:l})=>{v()&&!m&&(p(),n(f,l?.url?l.url.pathname+l.url.search+l.url.hash:null,!0),n(a,!0)),m=!1}),{get showModal(){return e(a)},bypass(){m=!0},confirm(){n(a,!1),e(f)&&(m=!0,Y(e(f)),n(f,null))},cancel(){n(a,!1),n(f,null)}}}function ce(v){const{save:a,getValue:f,applyChange:m,formName:p="Form",canSave:l}=v;let t=A(W([])),h=A(!1),g=A(null),D=0,s=null,i=null,c=[];const u=T(()=>e(t).length>0);async function r(){if(!e(h)){n(h,!0);try{await a(),n(g,Date.now(),!0),n(t,e(t).map(o=>o.savedToServer?o:{...o,savedToServer:!0}),!0)}catch{H.error(`Failed to save ${p}`)}finally{n(h,!1)}}}function d(){l&&!l()||(s&&clearTimeout(s),s=setTimeout(()=>{s=null,r()},500))}function S(o,C,U){if(!L.autoSaveEnabled)return;const V=U!==void 0?U:f(o);if(JSON.stringify(V)===JSON.stringify(C))return;const x={id:D++,path:o,oldValue:JSON.parse(JSON.stringify(V??null)),newValue:JSON.parse(JSON.stringify(C??null)),timestamp:Date.now(),label:`Changed ${$(o)}`,savedToServer:!1},O=L.autoSaveBatchWindowMs;O>0?(c.push(x),i&&clearTimeout(i),i=setTimeout(()=>{n(t,[...e(t),...c].slice(-50),!0),c=[],i=null,d()},O)):(n(t,[...e(t),x].slice(-50),!0),d())}function b(){if(e(t).length===0)return;const o=e(t)[e(t).length-1];n(t,e(t).slice(0,-1),!0),m(o.path,o.oldValue),o.savedToServer&&d(),H.info(`Undone: ${o.label}`,{duration:2e3})}async function k(){l&&!l()||(s&&(clearTimeout(s),s=null),c.length>0&&(n(t,[...e(t),...c].slice(-50),!0),c=[],i&&(clearTimeout(i),i=null)),await r())}function J(){n(t,[],!0),s&&(clearTimeout(s),s=null),i&&(clearTimeout(i),i=null),c=[],n(g,null)}return{get undoStack(){return e(t)},get canUndo(){return e(u)},get saving(){return e(h)},get lastSavedAt(){return e(g)},oncommit:S,undo:b,forceSave:k,reset:J}}function $(v){const a=v.split(".");return a[0]==="header"&&a.length>1&&a.shift(),a.join(" > ")}export{ue as U,ce as a,fe as c};
