import{proxyCustomElement as t,HTMLElement as i,getElement as e,h as s,transformTag as h}from"@stencil/core/internal/client";import{g as o}from"./limetype.js";import{f as a}from"./dom.js";import{j as l,b as r}from"./index.esm.js";const n=l("object-resolver");function c(t,i){return`object/${t}/${i}`}class d{constructor(t){this.onStateChange=t,this.state={loadFailed:!1},this.hasPendingLoad=!1,this.isDisconnected=!1}get resolvedPlatform(){return this._platform}connect(t,i,e,s){this.limetype=t,this.objectid=i,this.isDisconnected=!1,this.hasPendingLoad=!1,this._platform=s||this.findPlatform(e),this._platform?(this.resolveLimeType(),this.loadLimeObject()):n.error(`No platform found. Pass "platform" prop or place the component inside a parent that exposes "platform". limetype=${t}, objectid=${i}`)}update(t,i){this.limetype=t,this.objectid=i,this.updateState({limeobject:void 0,limetypeData:void 0,loadFailed:!1}),this.hasPendingLoad||(this.hasPendingLoad=!0,queueMicrotask((()=>{if(this.hasPendingLoad=!1,!this.isDisconnected){if(!this._platform)return this.updateState({loadFailed:!0}),void n.warn(`Cannot update: no platform available. limetype=${this.limetype}, objectid=${this.objectid}`);this.resolveLimeType(),this.loadLimeObject()}})))}disconnect(){this.isDisconnected=!0}findPlatform(t){const i=a(t,this.hasPlatformProp);return null==i?void 0:i.platform}hasPlatformProp(t){return"platform"in t&&!!t.platform&&"object"==typeof t.platform}resolveLimeType(){const t=this._platform.get(r.LimeTypeRepository);this.updateState({limetypeData:t.getLimeType(this.limetype)})}async loadLimeObject(){const t=this.limetype,i=this.objectid,e=this._platform.get(r.LimeObjectRepository),s=e.getObject(t,i);if(s)this.updateState({limeobject:s});else{try{await e.loadObject(t,i)}catch(e){if(this.isStale(t,i))return;return this.updateState({loadFailed:!0}),void n.error(`Failed to load ${t}/${i}`,e)}this.isStale(t,i)||this.updateState({limeobject:e.getObject(t,i)})}}isStale(t,i){return this.isDisconnected||this.limetype!==t||this.objectid!==i}updateState(t){this.state=Object.assign(Object.assign({},this.state),t),this.isDisconnected||this.onStateChange(this.state)}}const m=t(class extends i{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.size="default",this.data={loadFailed:!1},this.resolver=new d((t=>{this.data=t}))}connectedCallback(){this.resolver.connect(this.limetype,this.objectid,e(this),this.platform)}disconnectedCallback(){this.resolver.disconnect()}handlePropChange(){this.resolver.update(this.limetype,this.objectid)}render(){if(!this.resolver.resolvedPlatform)return;const t=o(this.data.limetypeData);return s("limel-chip",{text:this.getChipText(),icon:t,size:this.size,link:{href:c(this.limetype,this.objectid)},loading:!this.data.limeobject&&!this.data.loadFailed})}getChipText(){var t;return(null===(t=this.data.limeobject)||void 0===t?void 0:t.descriptive)?this.data.limeobject.descriptive:this.data.loadFailed?this.getErrorText():""}getErrorText(){if(!this.resolver.resolvedPlatform)return"loading failed";try{return this.resolver.resolvedPlatform.get(r.Translate).get("webclient.limeobject.errors.load-failed")}catch(t){return"loading failed"}}static get watchers(){return{limetype:[{handlePropChange:0}],objectid:[{handlePropChange:0}]}}static get style(){return":host{display:inline-block;vertical-align:baseline}"}},[1,"limebb-object-chip",{limetype:[1],objectid:[2],size:[1],platform:[16],data:[32]},void 0,{limetype:[{handlePropChange:0}],objectid:[{handlePropChange:0}]}]),p=m,u=function(){"undefined"!=typeof customElements&&["limebb-object-chip"].forEach((t=>{"limebb-object-chip"===t&&(customElements.get(h(t))||customElements.define(h(t),m))}))};export{p as LimebbObjectChip,u as defineCustomElement}