/*!
 App Connect Datastore
 Version: 2.0.1
 (c) 2024 Wappler.io
 @build 2024-11-11 18:23:12
 */
dmx.Component("datastore",{initialData:{batch:!1,data:[]},attributes:{session:{type:Boolean,default:!1},columns:{type:Object,default:null}},methods:{insert(e){this._insert(e)},update(e,t){this._update(e,t)},upsert(e,t){this._upsert(e,t)},delete(e){this._delete(e)},clear(){this._clear()},get(e){return this._filter(e)},startBatch(){this.set("batch",!0)},endBatch(){this.set("batch",!1),this._updateData()}},events:{inserted:Event,updated:Event,deleted:Event},render:!1,init(){this._records=[],this._lastid=0,this._save=this._save.bind(this),this._updateData=this._updateData.bind(this),this._storageHandler=this._storageHandler.bind(this),window.addEventListener("storage",this._storageHandler),this._read()},destroy(){window.removeEventListener("storage",this._storageHandler)},performUpdate(e){e.has("columns")&&this._updateData()},_read(){try{const e=JSON.parse(this._store().getItem("datastore_"+this.name));e&&(e.records&&(this._records=e.records),e.lastid&&(this._lastid=e.lastid),this._updateData())}catch(e){console.warn("Error parsing datastore",e)}},_filter(e){return"number"==typeof e&&(e={$id:e}),this._records.filter((t=>{if(Array.isArray(e)){for(const s=0;s<e.length;s++)for(const i in e[s])if(t[i]===e[s][i])return!0}else for(const s in e)if(t[s]===e[s])return!0;return!1}))},_insert(e){const t={inserted:[],deleted:[]};dmx.debug&&(console.debug("_insert method"),console.time("_insert"+this.name)),this._array(e).forEach((e=>{const s=this._mergeData({$id:++this._lastid},e);this._records.push(s),t.inserted.push(dmx.clone(s))})),dmx.debug&&console.timeEnd("_insert"+this.name),this._save(),this.dispatchEvent("inserted",null,t)},_update(e,t){if(!this._validData(t))return void console.warn("Invalid data!",t);const s={inserted:[],deleted:[]};dmx.debug&&(console.debug("_update method"),console.time("_update"+this.name)),this._filter(e).forEach((e=>{const i=this._mergeData(e,t);dmx.equal(e,i)||(s.deleted.push(dmx.clone(e)),s.inserted.push(dmx.clone(i)),Object.assign(e,i))})),dmx.debug&&console.timeEnd("_update"+this.name),this._save(),this.dispatchEvent("updated",null,s)},_upsert(e,t){this._filter(e).length?this._update(e,t):this._insert(t)},_delete(e){const t={inserted:[],deleted:[]};"number"==typeof e&&(e={$id:e}),dmx.debug&&(console.debug("_delete method called with filter",e),console.time("_delete"+this.name)),this._records=this._records.filter((s=>{for(const i in e)if(s[i]===e[i])return t.deleted.push(dmx.clone(s)),!1;return!0})),dmx.debug&&console.timeEnd("_delete"+this.name),this._save(),this.dispatchEvent("deleted",null,t)},_clear(){dmx.debug&&(console.debug("_clear method"),console.time("_clear"+this.name)),this._records=[],this._lastid=0,this._save(),dmx.debug&&console.timeEnd("_clear"+this.name)},_validData:e=>"object"==typeof e&&!Array.isArray(e),_mergeData(e,t){dmx.debug&&(console.debug("Merge Data"),console.time("merge"+this.name));const s=Object.assign({},e);for(const i in t){let d=t[i];this._isExpression(d)&&(d=dmx.parse(d,dmx.DataScope(e,this))),s[i]=d}return dmx.debug&&console.timeEnd("merge"+this.name),s},_updateData(){this.data.batch||(this.props.columns&&"object"==typeof this.props.columns?(dmx.debug&&(console.debug("Update data columns"),console.time("update"+this.name)),this.set("data",this._records.map(((e,t)=>{const s=dmx.clone(e),i=dmx.DataScope({$value:e,$index:t,$key:t,...e},this);for(const e in this.props.columns){let t=this.props.columns[e];this._isExpression(t)&&(t=dmx.parse(t,i)),s[e]=t}return s}))),dmx.debug&&console.timeEnd("update"+this.name)):(dmx.debug&&(console.debug("Update data records"),console.time("update"+this.name)),this.set("data",dmx.clone(this._records)),dmx.debug&&console.timeEnd("update"+this.name)))},_save(){this._updateData(),this.delay&&clearTimeout(this.delay),this.delay=setTimeout((()=>{dmx.debug&&(console.debug("Save data to storage"),console.time("store"+this.name));const e=JSON.stringify({records:this._records,lastid:this._lastid});this._store().setItem("datastore_"+this.name,e),dmx.debug&&console.timeEnd("store"+this.name)}))},_isExpression:e=>"string"==typeof e&&e.includes("{{"),_array:e=>Array.isArray(e)?e:[e],_store(){return window[(this.props.session?"session":"local")+"Storage"]},_storageHandler(e){dmx.debug&&console.debug("Storage Event",e),e.key=="datastore_"+this.name&&this._read()}});
//# sourceMappingURL=dmxDatastore.js.map
