import{LitElement as t,html as e}from"../../../../lit/index.js";import{localStorageGet as s}from"../../../utils/utils.js";import"../../../jwt-login/jwt-login.js";import{toJS as i,autorun as a}from"../../../../mobx/dist/mobx.esm.js";import{store as o}from"./AppHaxStore.js";import{SimpleColorsSharedStylesGlobal as l}from"../../../simple-colors-shared-styles/simple-colors-shared-styles.js";import{SimpleIconIconsetsManifest as n}from"../../../simple-icon/lib/simple-iconset-manifest.js";export class AppHaxBackendAPI extends t{static get tag(){return"app-hax-backend-api"}constructor(){super(),this.jwt=s("jwt",null),this.method=window&&globalThis.appSettings&&globalThis.appSettings.demo?"GET":"POST",this.basePath="/",this.lastResponse={},this.appSettings={},a((()=>{this.appSettings=i(o.appSettings),this.appSettings.method&&(this.method=this.appSettings.method),this.appSettings.jwt&&(this.jwt=this.appSettings.jwt)})),a((()=>{this.token=i(o.token)}))}static get properties(){return{jwt:{type:String},basePath:{type:String,attribute:"base-path"},appSettings:{type:Object},method:{type:String},token:{type:String}}}render(){return e`<jwt-login
      jwt="${this.jwt}"
      url="${this.appSettings.login}"
      method="${this.method}"
      refresh-url="${this.appSettings.refreshUrl}"
      redirect-url="${this.appSettings.redirectUrl}"
      logout-url="${this.appSettings.logout}"
      id="jwt"
      @jwt-changed="${this.jwtChanged}"
      @jwt-login-login-failed="${this.jwtFailed}"
    ></jwt-login>`}jwtFailed(t){this.jwt=null,this.token=null}async jwtChanged(t){if(this.jwt=t.detail.value,!this.__loopBlock&&this.jwt){this.__loopBlock=!0;const t=await this.makeCall("getUserDataPath");t&&t.data&&(o.user={name:t.data.userName},this.__loopBlock=!1)}}async makeCall(t,e={},s=!1,i=!1){if(this.appSettings&&this.appSettings[t]){var a=`${this.basePath}${this.appSettings[t]}`,o={method:this.method};this.jwt&&(e.jwt=this.jwt),this.token&&(e.token=this.token),"GET"===this.method?a+="?"+new URLSearchParams(e).toString():o.body=JSON.stringify(e);const l=await fetch(`${a}`,o).then((a=>a.ok?a.json():(401===a.status||404===a.status?globalThis.dispatchEvent(new CustomEvent("jwt-login-logout",{composed:!0,bubbles:!0,cancelable:!1,detail:!0})):403===a.status&&globalThis.dispatchEvent(new CustomEvent("jwt-login-refresh-token",{composed:!0,bubbles:!0,cancelable:!1,detail:{element:{obj:this,callback:"refreshRequest",params:[t,e,s,i]}}})),{})));return s&&(this.lastResponse[t]=l),i&&i(),l}}refreshRequest(t,e){const{call:s,data:i,save:a,callback:o}=e;t&&(this.jwt=t,this.makeCall(s,i,a,o))}firstUpdated(t){super.firstUpdated&&super.firstUpdated(t),o.AppHaxAPI=this,o.newSitePromiseList=[...o.newSitePromiseList,async()=>await this.makeCall("createSite",this._formatSitePostData(),!0)]}_formatSitePostData(){const t=i(o.site),e=i(o.items),s=i(o.itemFiles),a=Object.keys(l.colors);return{site:{name:t.name,description:`${t.type} ${t.structure}`,theme:t.theme},build:{type:t.type,structure:t.structure,items:e,files:s},theme:{color:a[Math.floor(Math.random()*a.length)],icon:`${n[0].name}:${n[0].icons[Math.floor(Math.random()*n[0].icons.length)]}`}}}updated(t){super.updated&&super.updated(t),t.forEach(((t,e)=>{"jwt"===e&&(o.jwt=this[e]),"token"===e&&(o.token=this[e])}))}}globalThis.AppHaxAPI=globalThis.AppHaxAPI||{},globalThis.AppHaxAPI.requestAvailability=()=>(globalThis.AppHaxAPI.instance||(globalThis.AppHaxAPI.instance=globalThis.document.createElement(AppHaxBackendAPI.tag),globalThis.document.body.appendChild(globalThis.AppHaxAPI.instance)),globalThis.AppHaxAPI.instance);export const AppHaxAPI=globalThis.AppHaxAPI.requestAvailability();customElements.define(AppHaxBackendAPI.tag,AppHaxBackendAPI);