import{html as e,css as t,LitElement as i}from"../../../lit/index.js";import{HAXStore as o}from"./hax-store.js";import{autorun as s,toJS as a}from"../../../mobx/dist/mobx.esm.js";import{localStorageGet as r}from"../../utils/utils.js";class HaxAppSearch extends i{static get styles(){return[t`
        :host {
          display: none;
        }
      `]}constructor(){super(),this.auto=!1,this.headers={},this.method="GET",this.loading=!1,this.requestData={},this.media=[],this.resultMap={},s((()=>{o.editMode&&(this.activeApp=a(o.activeApp))})),o.appSearch=this}async loadAppData(){this.loading=!0;let e=this.requestUrl(this.requestEndPoint,this.requestParams);return await fetch(e,{headers:this.headers,method:this.method}).then((e=>{if(e.ok)return e.json()})).then((e=>this._requestDataChanged(e)))}updated(e){e.forEach(((e,t)=>{["auto","method","headers","requestEndPoint","requestParams"].includes(t)&&(clearTimeout(this.__debounce),this.__debounce=setTimeout((()=>{this.requestEndPoint&&this.loadAppData()}),100)),"activeApp"==t&&(this.requestParams={},this.searchSchema={},setTimeout((()=>{this.searchSchema={properties:{}},this._resetAppSearch(this.activeApp)}),10))}))}requestUrl(e="",t={}){var i="";if(null!=o.connectionRewrites.appendUploadEndPoint&&t.__HAXAPPENDUPLOADENDPOINT__&&(i=o.connectionRewrites.appendUploadEndPoint+"&"),null!=o.connectionRewrites.appendJwt&&t.__HAXJWT__&&(t[o.connectionRewrites.appendJwt]=r(o.connectionRewrites.appendJwt)),i+=this.queryStringData(t)){var s=e.indexOf("?")>=0?"&":"?";return e+s+i}return e}queryStringData(e){var t,i,o=[];for(t in e)if(i=e[t],"__HAXJWT__"==t||"__HAXAPPENDUPLOADENDPOINT__"==t);else if(Array.isArray(i))for(var s=0;s<i.length;s++)o.push(t+"="+globalThis.encodeURIComponent(i[s]));else null!==i?o.push(t+"="+globalThis.encodeURIComponent(i)):o.push(t);return o.join("&")}static get tag(){return"hax-app-search"}static get properties(){return{activeApp:{type:Object},tos:{type:Array},auto:{type:Boolean},searchSchema:{type:Object},headers:{type:Object},method:{type:String},loading:{type:Boolean},requestData:{type:Object},media:{type:Array},requestEndPoint:{type:String},requestParams:{type:Object},resultMap:{type:Object}}}updateSearchValues(e){let t=this.requestParams;for(let i in e)""!=e[i]&&(t[i]=e[i]);this.requestParams={...this.requestParams}}_resetAppSearch(e){if(e&&e.details){let s=e;var t={};this.auto=!1,this.media=[],void 0!==s.connection.data&&(t=s.connection.data),void 0!==s.connection.operations.browse.data&&(t=Object.assign(t,s.connection.operations.browse.data)),this.method=s.connection.operations.browse.method,this.headers={},void 0!==s.connection.headers&&(this.headers=s.connection.headers),this.requestParams={...t};var i=s.connection.protocol+"://"+s.connection.url;"/"!=i.substr(i.length-1)&&(i+="/"),void 0!==s.connection.operations.browse.endPoint&&(i+=s.connection.operations.browse.endPoint),this.requestEndPoint=i;var o={properties:{}};void 0!==s.connection.operations.browse.search&&(o.properties=s.connection.operations.browse.search,this.searchSchema={...o}),this.resultMap=s.connection.operations.browse.resultMap,this.pagination={},void 0!==s.connection.operations.browse.pagination&&(this.pagination=s.connection.operations.browse.pagination),void 0!==s.connection.auto?this.auto=s.connection.auto:this.auto=!0}}_requestDataChanged(e){if(this.resultMap&&typeof e!={}){let s=[],a=this.resultMap,r=[];if(this.resultMap.items?void 0!==this._resolveObjectPath(a.items,e)?r=this._resolveObjectPath(a.items,e):null!=e&&(r=e):r=e,null!=r){for(var t=0;t<r.length;t++){if(s[t]={title:this._resolveObjectPath(a.preview.title,r[t]),details:this._resolveObjectPath(a.preview.details,r[t]),type:a.defaultGizmoType,map:{}},void 0!==s[t].details&&null!=s[t].details&&(s[t].details=s[t].details.replace(/(<([^>]+)>)/gi,"")),a.preview.id.constructor===Object){let e=this._resolveObjectPath(a.preview.id.property,r[t]);"split"===a.preview.id.op&&(e=e.split(a.preview.id.delimiter),s[t].id=e[a.preview.id.position])}else s[t].id=this._resolveObjectPath(a.preview.id,r[t]);for(var i in void 0!==a.preview.image?s[t].image=this._resolveObjectPath(a.preview.image,r[t]):void 0!==a.image?s[t].image=a.image:s[t].image="",a.gizmo)if("_url_source"===i){let e="";e=void 0!==s[t].map.__id?s[t].map.__id:this._resolveObjectPath(a.gizmo.id,r[t]),s[t].map.source=a.gizmo._url_source.replace("<%= id %>",e),s[t].map.url=s[t].map.source}else if(a.gizmo[i].constructor===Object){let e=this._resolveObjectPath(a.gizmo[i].property,r[t]);"split"===a.gizmo[i].op&&(e=e.split(a.gizmo[i].delimiter),s[t].map[i]=e[a.gizmo[i].position],"id"===i&&(s[t].map.__id=s[t].map[i]))}else s[t].map[i]=this._resolveObjectPath(a.gizmo[i],r[t]);void 0===s[t].map.url&&void 0!==s[t].map.source&&(s[t].map.url=s[t].map.source),void 0!==a.gizmo.type?s[t].type=this._resolveObjectPath(a.gizmo.type,r[t]):void 0!==a.gizmo.mimetype?s[t].type=o.mimeTypeToGizmoType(this._resolveObjectPath(a.gizmo.mimetype,r[t])):"*"!=o.guessGizmoType(a.gizmo)&&(s[t].type=o.guessGizmoType(a.gizmo))}this.media=[...s]}}return this.loading=!1,this.media}_resolveObjectPath(e,t){return e.split(".").reduce((function(e,t){return e?e[t]:null}),t||self)}}customElements.define(HaxAppSearch.tag,HaxAppSearch);export{HaxAppSearch};