import{r as e}from"./rolldown-runtime.WmLfqUcM.js";var t=e({plugin_exports:()=>n});(function(){var e=hugerte.util.Tools.resolve(`hugerte.PluginManager`);let t=(e,t,n)=>n(e,t.prototype)?!0:e.constructor?.name===t.name,n=e=>{let n=typeof e;return e===null?`null`:n===`object`&&Array.isArray(e)?`array`:n===`object`&&t(e,String,(e,t)=>t.isPrototypeOf(e))?`string`:n},r=e=>t=>n(t)===e,i=e=>t=>e===t,a=r(`string`),o=i(void 0);var s=hugerte.util.Tools.resolve(`hugerte.util.Delay`),c=hugerte.util.Tools.resolve(`hugerte.util.LocalStorage`),l=hugerte.util.Tools.resolve(`hugerte.util.Tools`);let u=e=>e.dispatch(`RestoreDraft`),d=e=>e.dispatch(`StoreDraft`),f=e=>e.dispatch(`RemoveDraft`),p=e=>{let t={s:1e3,m:6e4},n=/^(\d+)([ms]?)$/.exec(e);return(n&&n[2]?t[n[2]]:1)*parseInt(e,10)},m=e=>t=>t.options.get(e),h=e=>{let t=e.options.register,n=e=>{let t=a(e);return t?{value:p(e),valid:t}:{valid:!1,message:`Must be a string.`}};t(`autosave_ask_before_unload`,{processor:`boolean`,default:!0}),t(`autosave_prefix`,{processor:`string`,default:`hugerte-autosave-{path}{query}{hash}-{id}-`}),t(`autosave_restore_when_empty`,{processor:`boolean`,default:!1}),t(`autosave_interval`,{processor:n,default:`30s`}),t(`autosave_retention`,{processor:n,default:`20m`})},g=m(`autosave_ask_before_unload`),_=m(`autosave_restore_when_empty`),v=m(`autosave_interval`),y=m(`autosave_retention`),b=e=>{let t=document.location;return e.options.get(`autosave_prefix`).replace(/{path}/g,t.pathname).replace(/{query}/g,t.search).replace(/{hash}/g,t.hash).replace(/{id}/g,e.id)},x=(e,t)=>{if(o(t))return e.dom.isEmpty(e.getBody());{let n=l.trim(t);if(n===``)return!0;{let t=new DOMParser().parseFromString(n,`text/html`);return e.dom.isEmpty(t)}}},S=e=>{let t=parseInt(c.getItem(b(e)+`time`)??`0`,10)||0;return new Date().getTime()-t>y(e)?(C(e,!1),!1):!0},C=(e,t)=>{let n=b(e);c.removeItem(n+`draft`),c.removeItem(n+`time`),t!==!1&&f(e)},w=e=>{let t=b(e);!x(e)&&e.isDirty()&&(c.setItem(t+`draft`,e.getContent({format:`raw`,no_events:!0})),c.setItem(t+`time`,new Date().getTime().toString()),d(e))},T=e=>{let t=b(e);S(e)&&(e.setContent(c.getItem(t+`draft`)??``,{format:`raw`}),u(e))},E=e=>{let t=v(e);s.setEditorInterval(e,()=>{w(e)},t)},D=e=>{e.undoManager.transact(()=>{T(e),C(e)}),e.focus()},O=e=>({hasDraft:()=>S(e),storeDraft:()=>w(e),restoreDraft:()=>T(e),removeDraft:t=>C(e,t),isEmpty:t=>x(e,t)});var k=hugerte.util.Tools.resolve(`hugerte.EditorManager`);let A=e=>{e.editorManager.on(`BeforeUnload`,e=>{let t;l.each(k.get(),e=>{e.plugins.autosave&&e.plugins.autosave.storeDraft(),!t&&e.isDirty()&&g(e)&&(t=e.translate(`You have unsaved changes are you sure you want to navigate away?`))}),t&&(e.preventDefault(),e.returnValue=t)})},j=e=>t=>{t.setEnabled(S(e));let n=()=>t.setEnabled(S(e));return e.on(`StoreDraft RestoreDraft RemoveDraft`,n),()=>e.off(`StoreDraft RestoreDraft RemoveDraft`,n)},M=e=>{E(e);let t=()=>{D(e)};e.ui.registry.addButton(`restoredraft`,{tooltip:`Restore last draft`,icon:`restore-draft`,onAction:t,onSetup:j(e)}),e.ui.registry.addMenuItem(`restoredraft`,{text:`Restore last draft`,icon:`restore-draft`,onAction:t,onSetup:j(e)})};e.add(`autosave`,e=>(h(e),A(e),M(e),e.on(`init`,()=>{_(e)&&e.dom.isEmpty(e.getBody())&&T(e)}),O(e)))})();var n={};export{t};