!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).mar10={})}(this,(function(t){"use strict";
/*!
     * persisto.js - utils
     * Copyright (c) 2016-2021, Martin Wendt. Released under the MIT license.
     * v2.0.2, Sun, 21 Mar 2021 07:42:57 GMT (https://github.com/mar10/persisto)
     */const e=9007199254740991;function s(t,e,s,i){"string"==typeof t&&(t=document.querySelector(t)),e.split(" ").forEach((e=>{t.addEventListener(e,(function(t){if(!s)return i(t);if(t.target){let e=t.target;if(e.matches(s))return i(t);if(e=e.closest(s),e)return i(t)}}))}))}function i(t,e,s){switch("string"==typeof t&&(t=document.querySelector(t)),s){case!0:t.classList.add(e);break;case!1:t.classList.remove(e);break;default:t.classList.toggle(e)}}function o(t,e){if(null==t)return t;let s=t.length,i=0;if("number"==typeof s)for(;i<s&&!1!==e.call(t[i],i,t[i]);i++);else for(let s in t)if(!1===e.call(t[s],s,t[s]))break;return t}function h(...t){for(let e=1;e<t.length;e++){let s=t[e];for(let e in s)Object.prototype.hasOwnProperty.call(s,e)&&(t[0][e]=s[e])}return t[0]}function r(){}
/*!
     * persisto.js - deferred
     * Copyright (c) 2016-2021, Martin Wendt. Released under the MIT license.
     * v2.0.2, Sun, 21 Mar 2021 07:42:57 GMT (https://github.com/mar10/persisto)
     */class a{constructor(){this._promise=new Promise(((t,e)=>{this._resolve=t,this._reject=e}))}resolve(t){this._resolve(t)}reject(t){this._reject(t)}promise(){return this._promise}then(t){return this._promise.then(t)}catch(t){return this._promise.catch(t)}finally(t){return this._promise.finally(t)}}
/*!
     * persisto.js
     *
     * Persistent JavaScript objects and web forms using Web Storage.
     *
     * Copyright (c) 2016-2021, Martin Wendt (https://wwWendt.de).
     * Released under the MIT license.
     *
     * @version v2.0.2
     * @date Sun, 21 Mar 2021 07:42:57 GMT
     */var n,l;!function(t){t.Ok="ok",t.Modified="modified",t.Loading="loading",t.Saving="saving",t.Error="error"}(n||(n={})),function(t){t.Idle="",t.Push="push",t.Pull="pull"}(l||(l={}));t.PersistentObject=class{constructor(t,e){this.version="v2.0.2",this._checkTimer=null,this.offline=void 0,this.phase=l.Idle,this.status=n.Ok,this.uncommittedSince=0,this.unpushedSince=0,this.lastUpdate=0,this.lastPull=0,this.commitCount=0,this.pushCount=0,this.lastModified=0;let i=new a;if(this.ready=i.promise(),this.namespace=t,!t)throw new Error("Missing required argument: namespace");this.opts=h({remote:null,defaults:{},attachForm:null,statusElement:null,commitDelay:500,createParents:!0,maxCommitDelay:3e3,pushDelay:5e3,maxPushDelay:3e4,storage:window.localStorage,debugLevel:1,change:r,update:r,commit:r,conflict:r,error:r,pull:r,push:r,save:r,status:r},e),this.storage=this.opts.storage,this._data=this.opts.defaults,"string"==typeof this.opts.attachForm?this.form=document.querySelector(this.opts.attachForm):this.opts.attachForm instanceof HTMLElement&&(this.form=this.opts.attachForm),"string"==typeof this.opts.statusElement?this.statusElement=document.querySelector(this.opts.statusElement):this.opts.statusElement instanceof HTMLElement&&(this.statusElement=this.opts.statusElement),this._setStatus(n.Ok);let o=this.storage?this.storage.getItem(this.namespace):null;this.form&&(this.form.classList.add("persisto"),s(this.form,"input","input,textarea",(t=>{this.readFromForm(this.form)})),s(this.form,"change","select",(t=>{this.readFromForm(this.form)})),this.form.addEventListener("submit",(t=>{this.readFromForm(this.form),t.preventDefault()})),this.form.addEventListener("reset",(t=>{setTimeout((()=>{this.readFromForm(this.form)}),10)})),this.ready.then((t=>{this.writeToForm()}))),this.opts.remote?this.pull().then((()=>{this.debug("init from remote",this._data),this.offline=!1,i.resolve(this)})).catch((t=>{this.offline=!0,null==o?console.warn(this+": could not init from remote; falling back default.",arguments):(console.warn(this+": could not init from remote; falling back to storage.",arguments),this._data=h({},this.opts.defaults,JSON.parse(o))),i.reject(t)})):null!=o?(this.update(),this._data=h({},this.opts.defaults,this._data),i.resolve(this)):i.resolve(this)}_setStatus(t){function e(e){if(e)for(let s in n)s=s.toLocaleLowerCase(),i(e,"persisto-"+s,t===s)}this.debug("status "+this.status+" => "+t),this.status=t,e(this.form),e(this.statusElement),this.opts.status.call(this,t)}_invalidate(t,s){let i=Date.now(),o=this.lastModified||i,h=0,r=0,a=0;this._checkTimer&&(clearTimeout(this._checkTimer),this._checkTimer=null),s?this.debug("_invalidate() delayed"):(this.lastModified=i,this.uncommittedSince||(this.uncommittedSince=i),this.unpushedSince||(this.unpushedSince=i),this.opts.change.call(this,t),this._setStatus(n.Modified)),this.storage&&(i-o>=this.opts.commitDelay||i-this.uncommittedSince>=this.opts.maxCommitDelay?(this.debug("_invalidate(): force commit",i-o>=this.opts.commitDelay,i-this.uncommittedSince>=this.opts.maxCommitDelay),this.commit()):h=Math.min(i+this.opts.commitDelay+1,this.uncommittedSince+this.opts.maxCommitDelay+1)),this.opts.remote&&(i-o>=this.opts.pushDelay||i-this.unpushedSince>=this.opts.maxPushDelay?(this.debug("_invalidate(): force push",i-o>=this.opts.pushDelay,i-this.unpushedSince>=this.opts.maxPushDelay),this.push()):r=Math.min(i+this.opts.pushDelay+1,this.unpushedSince+this.opts.maxPushDelay+1)),(h||r)&&(a=Math.min(h||e,r||e),this.debug("_invalidate("+t+") defer by "+(a-i)+"ms"),this._checkTimer=setTimeout((()=>{this._checkTimer=null,this._invalidate.call(this,"deferred "+t,!0)}),a-i))}_update(t){this.uncommittedSince&&(console.warn("Updating an uncommitted object."),!1===this.opts.conflict.call(this,t,this._data))||(this._data=t,this.opts.update.call(this),this.lastUpdate=Date.now())}toString(){return"PersistentObject('"+this.namespace+"')"}debug(...t){this.opts.debugLevel>=2&&(Array.prototype.unshift.call(t,this.toString()),console.log.apply(console,t))}log(...t){this.opts.debugLevel>=1&&(Array.prototype.unshift.call(t,this.toString()),console.log.apply(console,t))}isDirty(){return!!(this.storage&&this.uncommittedSince||this.opts.remote&&this.unpushedSince)}isReady(){throw new Error("Not implemented")}get(t){let e,s=this._data,i=(""+t).replace(/\[(\w+)\]/g,".$1").replace(/^\./,"").split(".");for(e=0;e<i.length;e++)if(s=s[i[e]],void 0===s&&e<i.length-1)throw new Error(this+": Property '"+t+"' could not be accessed because parent '"+i.slice(0,e+1).join(".")+"' does not exist");return s}_setOrRemove(t,e,s){let i,o,h=this._data,r=(""+t).replace(/\[(\w+)\]/g,".$1").replace(/^\./,"").split("."),a=r.pop();for(i=0;i<r.length;i++)if(o=h,h=o[r[i]],void 0===h){if(!this.opts.createParents)throw new Error(this+": Property '"+t+"' could not be set because parent '"+r.slice(0,i+1).join(".")+"' does not exist");this.debug("Creating intermediate parent '"+r[i]+"'"),h=o[r[i]]={}}h[a]!==e&&(!0===s?(delete h[a],this._invalidate("remove")):(h[a]=e,this._invalidate("set")))}set(t,e){return this._setOrRemove(t,e,!1)}remove(t){return this._setOrRemove(t,void 0,!0)}reset(t){this._data=t||{},this._invalidate("reset")}setDirty(t){console.log("setDirty",t),!1!==t&&this._invalidate("explicit")}update(){if(this.phase)throw new Error(this+": Trying to update while '"+this.phase+"' is pending.");this.opts.debugLevel>=2&&console.time&&console.time(this+".update");let t=this.storage.getItem(this.namespace);t=JSON.parse(t),this._update(t),this.opts.debugLevel>=2&&console.time&&console.timeEnd(this+".update")}commit(){let t;if(this.phase)throw new Error(this+": Trying to commit while '"+this.phase+"' is pending.");return this.opts.debugLevel>=2&&console.time&&console.time(this+".commit"),t=JSON.stringify(this._data),this.storage.setItem(this.namespace,t),this.uncommittedSince=0,this.commitCount+=1,this.opts.remote||this._setStatus(n.Ok),this.opts.debugLevel>=2&&console.time&&console.timeEnd(this+".commit"),this.opts.commit.call(this),this.opts.remote||(this.opts.save.call(this),this.lastModified=0),t}pull(){if(this.phase)throw new Error(this+": Trying to pull while '"+this.phase+"' is pending.");return this.opts.debugLevel>=2&&console.time&&console.time(this+".pull"),this.phase=l.Pull,this._setStatus(n.Loading),fetch(this.opts.remote,{method:"GET"}).then((t=>{if(!t.ok)throw new Error("GET "+this.opts.remote+" returned "+t.status+", "+t);return t.json()})).then((t=>{this.storage.setItem(this.namespace,JSON.stringify(t)),this._update(t),this._setStatus(n.Ok),this.lastPull=Date.now(),this.opts.pull.call(this)})).catch((t=>{throw console.error(arguments),this._setStatus(n.Error),this.opts.error.call(this,t),t})).finally((()=>{this.phase=l.Idle,this.opts.debugLevel>=2&&console.time&&console.timeEnd(this+".pull")}))}push(){let t;if(this.uncommittedSince?(this.phase&&(console.error("push(): Resetting phase "+this.phase+" => idle"),this.phase=l.Idle),t=this.commit()):t=JSON.stringify(this._data),this.phase)throw new Error(this+": Trying to push while '"+this.phase+"' is pending.");if(this.opts.debugLevel>=2&&console.time&&console.time(this+".push"),this.phase=l.Push,!this.opts.remote)throw new Error(this+": Missing remote option");return this._setStatus(n.Saving),fetch(this.opts.remote,{method:"PUT",body:t,headers:{"Content-Type":"application/json"}}).then((t=>{if(!t.ok)throw new Error("PUT "+this.opts.remote+" returned "+t.status+", "+t);this.unpushedSince=0,this.pushCount+=1,this._setStatus(n.Ok),this.opts.push.call(this,t),this.opts.save.call(this)})).catch((t=>{throw this._setStatus(n.Error),this.opts.error.call(this,t),t})).finally((()=>{this.phase=l.Idle,this.opts.debugLevel>=2&&console.time&&console.timeEnd(this+".push")}))}readFromForm(t,e){let s=h({addNew:!1,coerce:!0,trim:!0},e);if("string"==typeof(t=t||this.form)&&(t=document.querySelector(t)),s.addNew){let e=t.querySelectorAll("[name]");for(let t=0;t<e.length;t++){let s=e[t].getAttribute("name");void 0===this._data[s]&&(this.debug("readFromForm: add field '"+s+"'"),this._data[s]=null)}}o(this._data,((e,i)=>{let o,h,r=t.querySelectorAll("[name='"+e+"']"),a=r[0];r.length?(h=a.getAttribute("type"),"radio"===h?o=t[e].value:"checkbox"===h&&1===r.length?o=!!a.checked:"checkbox"===h&&r.length>1?(o=[],r.forEach((t=>{t.checked&&o.push(t.value)}))):a.matches("select")?a.multiple?(o=[],Array.from(a.selectedOptions).forEach((t=>{o.push(t.value)}))):o=a.options[a.selectedIndex].value:(o=a.value,s.trim&&"string"==typeof o&&(o=o.trim())),this.set(e,o)):this.debug("readFromForm: field not found: '"+e+"'")}))}writeToForm(t,e){let s,i,h;"string"==typeof(t=t||this.form)&&(t=document.querySelector(t)),o(this._data,(e=>{let o=this.get(e),r=Array.isArray(o),a=t.querySelectorAll("[name='"+e+"']");if(!a.length)return;let n=a[0],l=n.getAttribute("type");if("radio"===l)a.forEach((t=>{t.checked=t.value===o}));else if("checkbox"===l)if(1===a.length)n.checked=!!o;else for(s=0;s<a.length;s++)i=a[s],h=r?o.indexOf(i.value)>=0:i.value===o,i.checked=h;else if(n.matches("select"))for(s=0;s<n.options.length;s++)i=n.options[s],h=r?o.indexOf(i.value)>=0:i.value===o,i.selected=h;else"file"===l||(n.value=o)}))}},Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=persisto.umd.min.js.map