/**
 * Copyright 2020 The Pennsylvania State University
 * @license Apache-2.0, see License.md for full text.
 */
import{LitElement as e,html as t,css as s}from"../../../lit/index.js";import"../../simple-icon/simple-icon.js";import"../../simple-icon/lib/simple-icons.js";import"../../simple-icon/lib/simple-icon-button.js";import"../../hax-iconset/lib/simple-hax-iconset.js";import{HaxSchematizer as i,HaxElementizer as a}from"../../hax-body-behaviors/lib/HAXFields.js";import"../../simple-fields/simple-fields.js";import{SimpleFieldsForm as r}from"../../simple-fields/lib/simple-fields-form.js";import"./hax-element-card-list.js";class HaxElementListSelector extends e{static get tag(){return"hax-element-list-selector"}static get styles(){return[s`
        :host {
          display: block;
        }
      `]}constructor(){super(),this.loading=!1,this.cols=3,this.showCardList=!1,this.imports=[],this.haxData=[],this.noSchema={},this.method="GET",this.autoload=!0,this.HaxSchematizer=i,this.HaxElementizer=a,this.fieldsEndpoint=new URL("./fields.json",import.meta.url).href,globalThis.WCGlobalBasePath?this.basePath=globalThis.WCGlobalBasePath:this.basePath=new URL("./hax-element-list-selector.js",import.meta.url).href+"/../../../../",globalThis.WCGlobalRegistryFileName?this.__regFile=globalThis.WCGlobalRegistryFileName:this.__regFile="wc-registry.json",setTimeout((()=>{globalThis.addEventListener("a11y-tabs-active-changed",this._activeTabChanged.bind(this))}),0),this.addEventListener("response",this._response),this.addEventListener("value-changed",this._valueChanged)}static get properties(){return{...r.properties,HaxSchematizer:{type:Object},HaxElementizer:{type:Object},showCardList:{type:Boolean},imports:{type:Object},haxData:{type:Array},noSchema:{type:Object},filteredHaxData:{type:Array},cols:{type:Number},fieldsEndpoint:{type:String,attribute:"fields-endpoint"},wcRegistryEndpoint:{type:String,attribute:"wc-registry-endpoint"},method:{type:String},loading:{type:Boolean,reflect:!0}}}render(){return t`
      <simple-fields-form
        id="form"
        autoload
        load-endpoint="${this.fieldsEndpoint}"
        method="${this.method}"
        .schematizer="${this.HaxSchematizer}"
        .elementizer="${this.HaxElementizer}"
        @response="${this._response}"
        @haxcore.search.haxcore-search-columns-value-changed="${e=>this.cols=e.detail.value}"
        @value-changed="${this._valueChanged}"
      >
      </simple-fields-form>
    `}_activeTabChanged(e){"haxcore.search"==e.detail.activeTab?this.showCardList=!0:this.showCardList=!1}_getElement(e){return this.form&&this.form.formElements&&this.form.formElements[e]?this.form.formElements[e].element:void 0}get form(){return this.shadowRoot&&this.shadowRoot.querySelector("#form")?this.shadowRoot.querySelector("#form"):void 0}get cardList(){return this._getElement("haxcore.search.haxcore-search-autoloader")}get searchColumns(){this._getElement("haxcore.search.haxcore-search-columns")}get haxTags(){this._getElement("haxcore.providerdetails.haxcore-providerdetails-haxtags")}get otherTags(){this._getElement("haxcore.providerdetails.haxcore-providerdetails-othertags")}updated(e){e.forEach((async(e,t)=>{if("loading"==t&&this.cardList&&(this.cardList.loading=this.loading),"showCardList"==t&&this.cardList&&(this.cardList.showCardList=this.showCardList),"cols"==t&&this.cardList&&(this.cardList.cols=this.cols),"cols"==t&&this.searchColumns&&(this.searchColumns.value=this.cols),"wcRegistryEndpoint"==t&&(this.haxData=[],this.imports=[],fetch(this.wcRegistryEndpoint).then((e=>(this.loading=!0,e.json()))).then((e=>{this.imports=e}))),"imports"==t){let e=this.haxData,i=this.noSchema;for(var s in this[t]){let a=this[t][s];try{await import(`${this.basePath}${a}`).then((t=>{if(t&&Object.keys(t)&&Object.keys(t)[0]&&t[Object.keys(t)[0]]&&t[Object.keys(t)[0]].haxProperties&&t[Object.keys(t)[0]].haxProperties.gizmo&&t[Object.keys(t)[0]].haxProperties.gizmo.title){let i={tag:s,file:a,showDemo:!1,schema:t[Object.keys(t)[0]].haxProperties};e.push(i),this.haxData=[...e]}else i[s]=a}))}catch(e){console.warn(e)}}this.noSchema={},this.noSchema=i,this.loading=!1}if("haxData"==t&&(this.filteredHaxData=[...this.haxData],this.haxData.length>0)){let e={};for(var i in this.haxData)e[this.haxData[i].tag]=this.haxData[i].file;if(this.cardList){let e=this.form&&this.form.value&&this.form.value.haxcore&&this.form.value.haxcore.search?this.form.value.haxcore.search:void 0;this.cardList.list=this.filteredHaxData,e&&this.applyFilters(e),this.cardList.requestUpdate()}this.haxTags&&(this.haxTags.editorValue=JSON.stringify(e,null,2))}"noSchema"==t&&Object.keys(this.noSchema).length>0&&this.otherTags&&(this.otherTags.editorValue=JSON.stringify(this.noSchema,null,2))}))}applyFilters(e){if(this.cardList){let t=[...this.haxData];Object.keys(e||{}).forEach((s=>{if(""!=e[s])switch(s){case"haxcore-search-search":t=t.filter((t=>!!t.schema.gizmo.title.toLowerCase().includes(e[s].toLowerCase())));break;case"haxcore-search-tags":t=t.filter((t=>!!t.schema.gizmo.tags.includes(e[s])));break;case"haxcore-search-hasdemo":e[s]&&(t=t.filter((e=>!!e.schema.demoSchema)))}})),this.cardList.filteredTags=[...t].map((e=>e.tag)),this.cardList.requestUpdate()}}_response(e){this.searchColumns&&(this.searchColumns.value=this.cols),this.cardList&&(this.cardList.cols=this.cols)}_valueChanged(){clearTimeout(this.__valueDebounce),this.__valueDebounce=setTimeout((()=>{let e=this.form&&this.form.value&&this.form.value.haxcore?this.form.value.haxcore:void 0;if(e){let t=e.search&&e.search["haxcore-search-columns"]?e.search["haxcore-search-columns"]:void 0;t&&(this.cols=t),"other"==e.providers["haxcore-providers-cdn"]?this.wcRegistryEndpoint=e.providers["haxcore-providers-other"]+this.__regFile:this.wcRegistryEndpoint=e.providers["haxcore-providers-cdn"]+this.__regFile,this.applyFilters(e.search),this.cardList&&this.dispatchEvent(new CustomEvent("appstore-changed",{detail:{value:this.getAppstoreValues()}}))}}),50)}getAppstoreValues(){let e=this.shadowRoot.querySelector("#form").submit();e.haxcore=e.haxcore||{templates:{},providers:{}};let t={providers:{cdn:e.haxcore.providers["haxcore-providers-cdn"],other:e.haxcore.providers["haxcore-providers-other"],pk:e.haxcore.providers["haxcore-providers-pk"]},apps:{},stax:e.haxcore.templates["haxcore-templates-layouts"],autoloader:e.haxcore.search["haxcore-search-autoloader"]};for(var s in e.haxcore.integrations)t.apps[s.replace("haxcore-integrations-","")]=e.haxcore.integrations[s];return t}getAutoloader(e){let t={};for(var s in e)e[s].status&&(t[e[s].tag]=e[s].file);return t}}customElements.define(HaxElementListSelector.tag,HaxElementListSelector);