import"./DsnmJJEf.js";import{p as H,b as k,g as e,k as S,e as F,f as z,a as p,d as y,r as G,t as b,c as V,h as I,m as B,j as i}from"./qex-fw8l.js";import{C as W}from"./DVSyFtXW.js";import{i as o}from"./dc0o8kHW.js";import{i as P}from"./BnUrxEZg.js";import{s as g,t as x}from"./HL7kq492.js";import{p as C}from"./pJHJUtud.js";import{L as Y}from"./BnIIe8z2.js";import{C as K}from"./BUsyjOtM.js";import{p as X}from"./CjREOnTl.js";function ce(m,a){H(a,!0);{let _=S(()=>o.t("ADMIN_NEXT.UNSAVED_CHANGES")),h=S(()=>o.t("ADMIN_NEXT.UNSAVED_CHANGES_DIALOG.LEAVE_MESSAGE")),c=S(()=>o.t("ADMIN_NEXT.UNSAVED_CHANGES_DIALOG.LEAVE")),v=S(()=>o.t("ADMIN_NEXT.UNSAVED_CHANGES_DIALOG.STAY"));W(m,{get open(){return a.guard.showModal},get title(){return e(_)},get message(){return e(h)},get confirmLabel(){return e(c)},get cancelLabel(){return e(v)},get onconfirm(){return a.guard.confirm},get oncancel(){return a.guard.cancel}})}k()}var q=V('<span class="inline-flex h-7 w-7 items-center justify-center rounded-md border border-border bg-card text-muted-foreground"><!></span>'),Q=V('<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=V('<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 de(m,a){H(a,!0);let _=C(a,"saving",3,!1),h=C(a,"lastSavedAt",3,null),c=C(a,"autoSaveEnabled",3,!1);const v=S(()=>c()&&_()),t=S(()=>c()&&!_()&&!a.hasChanges&&h()!=null),E=S(()=>a.hasChanges&&!e(v));var A=F(),D=z(A);{var l=u=>{var r=q(),f=y(r);Y(f,{size:12,class:"animate-spin"}),G(r),b((N,T)=>{g(r,"title",N),g(r,"aria-label",T)},[()=>o.t("ADMIN_NEXT.UNSAVED_INDICATOR.SAVING_CHANGES"),()=>o.t("ADMIN_NEXT.UNSAVED_INDICATOR.SAVING_CHANGES_2")]),p(u,r)},s=u=>{var r=Q(),f=y(r);K(f,{size:12,strokeWidth:2.5}),G(r),b((N,T)=>{g(r,"title",N),g(r,"aria-label",T)},[()=>o.t("ADMIN_NEXT.ALL_CHANGES_SAVED"),()=>o.t("ADMIN_NEXT.ALL_CHANGES_SAVED")]),p(u,r)},d=u=>{var r=Z();b((f,N)=>{g(r,"title",f),g(r,"aria-label",N)},[()=>o.t("ADMIN_NEXT.UNSAVED_INDICATOR.UNSAVED_CHANGES_CLICK_SAVE_TO_PERSIST"),()=>o.t("ADMIN_NEXT.UNSAVED_INDICATOR.YOU_HAVE_UNSAVED_CHANGES")]),p(u,r)};P(D,u=>{e(v)?u(l):e(t)?u(s,1):e(E)&&u(d,2)})}p(m,A),k()}function fe(m){const{save:a,getValue:_,applyChange:h,canSave:c}=m,v=m.formName??o.t("ADMIN_NEXT.TOASTS.FORM_LABEL.FORM");let t=I(B([])),E=I(!1),A=I(null),D=0,l=null,s=null,d=[];const u=S(()=>e(t).length>0);async function r(){if(!e(E)){i(E,!0);try{await a(),i(A,Date.now(),!0),i(t,e(t).map(n=>n.savedToServer?n:{...n,savedToServer:!0}),!0)}catch{x.error(o.t("ADMIN_NEXT.TOASTS.SAVE_FORM_FAILED",{form:v}))}finally{i(E,!1)}}}function f(){c&&!c()||(l&&clearTimeout(l),l=setTimeout(()=>{l=null,r()},500))}function N(n,M,O){if(!X.autoSaveEnabled)return;const w=O!==void 0?O:_(n);if(JSON.stringify(w)===JSON.stringify(M))return;const U={id:D++,path:n,oldValue:JSON.parse(JSON.stringify(w??null)),newValue:JSON.parse(JSON.stringify(M??null)),timestamp:Date.now(),label:`Changed ${$(n)}`,savedToServer:!1},L=X.autoSaveBatchWindowMs;L>0?(d.push(U),s&&clearTimeout(s),s=setTimeout(()=>{i(t,[...e(t),...d].slice(-50),!0),d=[],s=null,f()},L)):(i(t,[...e(t),U].slice(-50),!0),f())}function T(){if(e(t).length===0)return;const n=e(t)[e(t).length-1];i(t,e(t).slice(0,-1),!0),h(n.path,n.oldValue),n.savedToServer&&f(),x.info(`Undone: ${n.label}`,{duration:2e3})}async function R(){c&&!c()||(l&&(clearTimeout(l),l=null),d.length>0&&(i(t,[...e(t),...d].slice(-50),!0),d=[],s&&(clearTimeout(s),s=null)),await r())}function J(){i(t,[],!0),l&&(clearTimeout(l),l=null),s&&(clearTimeout(s),s=null),d=[],i(A,null)}function j(){i(t,e(t).map(n=>n.savedToServer?n:{...n,savedToServer:!0}),!0),i(A,Date.now(),!0)}return{get undoStack(){return e(t)},get canUndo(){return e(u)},get saving(){return e(E)},get lastSavedAt(){return e(A)},oncommit:N,undo:T,forceSave:R,reset:J,markAllSaved:j}}function $(m){const a=m.split(".");return a[0]==="header"&&a.length>1&&a.shift(),a.join(" > ")}export{de as U,ce as a,fe as c};
