var __decorate=this&&this.__decorate||function(t,e,s,i){var r,o=arguments.length,l=o<3?e:null===i?i=Object.getOwnPropertyDescriptor(e,s):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)l=Reflect.decorate(t,e,s,i);else for(var a=t.length-1;a>=0;a--)(r=t[a])&&(l=(o<3?r(l):o>3?r(e,s,l):r(e,s))||l);return o>3&&l&&Object.defineProperty(e,s,l),l};import{customElement as t,property as e,state as s}from"lit/decorators.js";import{AttributePicker as i,AttributePickerPickedEvent as r}from"./attribute-picker";import{html as o,unsafeCSS as l}from"lit";import{DefaultColor5 as a,Util as d}from"@openremote/core";import{when as n}from"lit/directives/when.js";import{until as p}from"lit/directives/until.js";import{AssetModelUtil as c}from"@openremote/model";import{InputType as u}from"@openremote/or-mwc-components/or-mwc-input";import"./assettype-list";export class OrAssetTypeAttributePickerPickedEvent extends r{constructor(t){super(OrAssetTypeAttributePickerPickedEvent.NAME,{bubbles:!0,composed:!0,detail:t})}}OrAssetTypeAttributePickerPickedEvent.NAME="or-asset-type-attribute-picker-picked";let OrAssetTypeAttributePicker=class extends i{constructor(){super(...arguments),this.selectedAttributes=new Map}setSelectedAttributes(t){return this.selectedAttributes=t,this}willUpdate(t){if(this._loadedAssetTypes||(this._loadedAssetTypes=this._loadAssetTypes()),t.has("_selectedAssetType")){let t=this._getAssetDescriptorByName(this._selectedAssetType);t&&(this._loadedAttributeTypes=this._loadAttributeTypes(t))}return this._updateDialogContent(),this._updateDialogActions(),super.willUpdate(t)}_loadAssetTypes(){let t=c.getAssetDescriptors();return void 0!==this.assetTypeFilter&&(t=t.filter(this.assetTypeFilter)),this._loadedAssetTypes=t,t||[]}_loadAttributeTypes(t){let e=c.getAssetTypeInfo(t).attributeDescriptors;return void 0!==this.attributeFilter&&(e=null==e?void 0:e.filter(this.attributeFilter)),this.showOnlyDatapointAttrs&&(e=null==e?void 0:e.filter(t=>{var e;return null==(e=t.meta)?void 0:e.storeDataPoints})),this.showOnlyRuleStateAttrs&&(e=null==e?void 0:e.filter(t=>{var e;return null==(e=t.meta)?void 0:e.ruleState})),this._loadedAttributeTypes=e||[],e||[]}_setDialogContent(){var t;let e=this._loadedAssetTypes||this._loadAssetTypes(),s=this.selectedAttributes?Array.from(this.selectedAttributes.keys()):void 0,i=this._getAssetTypeDescriptors(e,e.filter(t=>!s||s.includes(t.name))),r=this._getAssetDescriptorByName(this._selectedAssetType),c=null==(t=this._loadedAttributeTypes||(r?this._loadAttributeTypes(r):void 0))?void 0:t.sort(d.sortByString(t=>t.name));this.content=()=>o`
            <div class="row" style="display: flex;height: 600px;width: 800px;border-top: 1px solid ${l(a)};">
                <div class="col" style="width: 320px;overflow: auto;border-right: 1px solid ${l(a)};">
                    <asset-type-list .listItems="${i}" style="--or-icon-fill: #000000;" @or-mwc-list-changed="${t=>{1===t.detail.length&&this._onAssetTypeItemClick(t.detail[0])}}"
                    ></asset-type-list>
                </div>
                <div class="col" style="flex: 1 1 auto;width: 320px;overflow: auto;">
                    ${n(c&&c.length>0,()=>{var t;let e=this._selectedAssetType?null==(t=this.selectedAttributes.get(this._selectedAssetType))?void 0:t.map(t=>t.name):void 0;return o`
                            <div class="attributes-header">
                                <or-translate value="attribute_plural"></or-translate>
                            </div>
                            ${p(this._getAttributesTemplate(void 0,c,e,this.multiSelect,t=>this._onAttributesSelect(t)),o`<or-loading></or-loading>`)}
                        `},()=>o`
                        <div style="display: flex;align-items: center;text-align: center;height: 100%;padding: 0 20px;">
                            <span style="width:100%">
                                <or-translate value="${c&&0===c.length?this.showOnlyDatapointAttrs&&this.showOnlyRuleStateAttrs?"noDatapointsOrRuleStateAttributes":this.showOnlyDatapointAttrs?"noDatapointsAttributes":this.showOnlyRuleStateAttrs?"noRuleStateAttributes":"noAttributesToShow":"selectAssetTypeOnTheLeft"}">
                                </or-translate>
                            </span>
                        </div>
                    `)}
                </div>
            </div>
        `}_setDialogActions(){this.actions=[{actionName:"cancel",content:"cancel"},{actionName:"add",content:o`
                    <or-mwc-input id="add-btn" class="button" label="add" .type="${u.BUTTON}"></or-mwc-input>`,action:()=>{this.addBtn.disabled||this.dispatchEvent(new OrAssetTypeAttributePickerPickedEvent(this.selectedAttributes))}}]}_onAssetTypeItemClick(t){this._selectedAssetType=t.data.name}_onAttributesSelect(t){var e;return this._selectedAssetType?this._loadedAttributeTypes?void this.selectedAttributes.set(this._selectedAssetType,null==(e=this._loadedAttributeTypes)?void 0:e.filter(e=>t.includes(e.name))):void console.warn("Could not select attribute, since the attribute list seems to be empty?"):void console.warn("Could not select attribute, since no asset type seemed to be selected.")}_getAssetTypeDescriptors(t,e,s){let i=null==t?void 0:t.map(t=>({styleMap:{"--or-icon-fill":t.colour?"#"+t.colour:"unset"},icon:t.icon,trailingIcon:(null==e?void 0:e.includes(t))?"cloud-upload-outline":void 0,text:d.getAssetTypeLabel(t),value:t.name,data:t})).sort((t,e)=>("agent"!==t.data.descriptorType)-("agent"!==e.data.descriptorType)||t.text.localeCompare(e.text));return s&&(null==i||i.splice(0,0,s)),i}_getAssetDescriptorByName(t){var e;return t?null==(e=this._loadedAssetTypes)?void 0:e.find(e=>e.name===t):void 0}};__decorate([e()],OrAssetTypeAttributePicker.prototype,"assetTypeFilter",void 0),__decorate([e()],OrAssetTypeAttributePicker.prototype,"attributeFilter",void 0),__decorate([s()],OrAssetTypeAttributePicker.prototype,"_selectedAssetType",void 0),__decorate([s()],OrAssetTypeAttributePicker.prototype,"_loadedAttributeTypes",void 0),__decorate([s()],OrAssetTypeAttributePicker.prototype,"_loadedAssetTypes",void 0),OrAssetTypeAttributePicker=__decorate([t("or-assettype-attribute-picker")],OrAssetTypeAttributePicker);export{OrAssetTypeAttributePicker};