/**
 * Copyright 2022 The Pennsylvania State University
 * @license Apache-2.0, see License.md for full text.
 */
const t=["endpoint","name","title","description","params","callback","method"];export class MicroFrontend{constructor(e={}){t.map((t=>this[t]="params"===t?e[t]||{}:e[t]||null))}}export const MicroFrontendRegCapabilities=function(e){return class extends e{constructor(){super(),this.list=[],this.MicroFrontend=MicroFrontend}add(t){this.define(new MicroFrontend(t))}define(e){if(e instanceof MicroFrontend||(console.warn("MicroFrontendRegistry: use class MicroFrontend instance but if keys match it will register still."),console.warn(e)),!Object.keys(e).every((e=>t.includes(e))))return!1;if(e.endpoint.startsWith("/api/")){var n="";n=globalThis.MicroFrontendRegistryConfig.base?globalThis.MicroFrontendRegistryConfig.base:globalThis.HAXCMSContext&&"nodejs"===globalThis.HAXCMSContext||!globalThis.location.origin.startsWith("http://127.0.0.1")&&!globalThis.location.origin.startsWith("http://localhost")?"https://open-apis.hax.cloud":globalThis.location.origin.replace(/127.0.0.1:8(.*)/,"localhost:3000").replace(/localhost:8(.*)/,"localhost:3000"),e.endpoint=`${n}${e.endpoint}`}return globalThis.MicroFrontendRegistryConfig[e.name]&&Object.keys(globalThis.MicroFrontendRegistryConfig[e.name]).map((t=>{e[t]=globalThis.MicroFrontendRegistryConfig[e.name][t]})),this.has(e.name)?void 0:(this.list.push(e),!0)}get(t,e=!1){if(t&&this.list.length>0){const e=this.list.find((e=>e.name===t));if(e)return e}return e||console.error(`call for ${t} but not found in micro-frontend-registry`),null}has(t){return null!==this.get(t,!0)}set(t,e={}){if(t&&this.list.length>0&&this.has(t)){const n=this.list.findIndex((e=>e.name===t));this.list[n]=e}return null}async call(t,e={},n=null,o=null,i=""){if(this.has(t)){const s=this.get(t);let r="POST";s.method&&(r=s.method),e.__method&&(r=e.__method,delete e.__method);let l=null;switch(r){case"GET":case"HEAD":const t=new URLSearchParams(e).toString();l=await fetch(t?`${s.endpoint}?${t}${i}`:s.endpoint+i,{method:r}).then((t=>t.ok?t.json():{status:t.status,data:null})).catch(((t,e)=>(console.warn("Request failed",t),{status:500,data:null})));break;default:l=await fetch(s.endpoint+i,{method:r,body:e instanceof FormData?e:JSON.stringify(e)}).then((t=>t.ok?t.json():{status:t.status,data:null})).catch(((t,e)=>(console.warn("Request failed",t),{status:500,data:null})))}return s.callback&&await s.callback(l,o),n&&await n(l,o),l}return null}url(t,e={}){if(this.has(t)){const o=this.get(t);for(var n in e)e.hasOwnProperty(n)&&null==e[n]&&delete e[n];return new URL(o.endpoint).toString()+`?${new URLSearchParams(e).toString()}`}return""}}};export class MicroFrontendRegistryNodeJS extends(MicroFrontendRegCapabilities(Object)){constructor(){super()}}class MicroFrontendRegistryEl extends(MicroFrontendRegCapabilities(HTMLElement)){static get tag(){return"micro-frontend-registry"}constructor(){super()}}globalThis.customElements.define(MicroFrontendRegistryEl.tag,MicroFrontendRegistryEl),globalThis.MicroFrontendRegistry=globalThis.MicroFrontendRegistry||{},globalThis.MicroFrontendRegistryConfig=globalThis.MicroFrontendRegistryConfig||{},globalThis.MicroFrontendRegistry.requestAvailability=()=>(globalThis.MicroFrontendRegistry.instance||(globalThis.document&&globalThis.document.body&&globalThis.document.body.appendChild?(globalThis.MicroFrontendRegistry.instance=globalThis.document.createElement(MicroFrontendRegistryEl.tag),globalThis.document.body.appendChild(globalThis.MicroFrontendRegistry.instance)):(console.log("NODE WHATS UP MAN!"),globalThis.MicroFrontendRegistry.instance=new MicroFrontendRegistryNodeJS)),globalThis.MicroFrontendRegistry.instance);export const MicroFrontendRegistry=globalThis.MicroFrontendRegistry.requestAvailability();