var __decorate=this&&this.__decorate||function(t,e,i,s){var r,a=arguments.length,o=a<3?e:null===s?s=Object.getOwnPropertyDescriptor(e,i):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(t,e,i,s);else for(var n=t.length-1;n>=0;n--)(r=t[n])&&(o=(a<3?r(o):a>3?r(e,i,o):r(e,i))||o);return a>3&&o&&Object.defineProperty(e,i,o),o},__awaiter=this&&this.__awaiter||function(t,e,i,s){return new(i||(i=Promise))(function(r,a){function o(t){try{u(s.next(t))}catch(t){a(t)}}function n(t){try{u(s.throw(t))}catch(t){a(t)}}function u(t){var e;t.done?r(t.value):((e=t.value)instanceof i?e:new i(function(t){t(e)})).then(o,n)}u((s=s.apply(t,e||[])).next())})};import{css as t,html as e,LitElement as i}from"lit";import{customElement as s,property as r}from"lit/decorators.js";import{getAssetIdsFromQuery as a,getAssetTypeFromQuery as o}from"../index";import{AssetModelUtil as n}from"@openremote/model";import{Util as u}from"@openremote/core";import"@openremote/or-attribute-input";import{InputType as c}from"@openremote/or-mwc-components/or-mwc-input";import{i18next as l,translate as d}from"@openremote/or-translate";import{OrRulesJsonRuleChangedEvent as h}from"./or-rule-json-viewer";import{ifDefined as p}from"lit/directives/if-defined.js";import{when as m}from"lit/directives/when.js";let style=t`
    :host {
        display: flex;
        align-items: center;

        flex-wrap: wrap;
    }

    :host > * {
        margin: 0 3px 6px;
    }

    .min-width {
        min-width: 200px;
    }
`,OrRuleActionAttribute=class extends d(l)(i){static get styles(){return style}shouldUpdate(t){return t.has("action")&&(this._assets=void 0),super.shouldUpdate(t)}refresh(){this._assets=void 0}_getAssetType(){if(!this.action.target)return;let t=this.action.target.assets?this.action.target.assets:this.action.target.matchedAssets?this.action.target.matchedAssets:void 0;return t?o(t):void 0}render(){let t;if(!this.action.target)return e``;let i=this._getAssetType();if(!i)return e``;let s=this.action.target.assets?this.action.target.assets:this.action.target.matchedAssets?this.action.target.matchedAssets:void 0,r=this.assetInfos?this.assetInfos.find(t=>t.assetDescriptor.name===i):void 0;if(!r)return e``;this._assets||this.loadAssets(i);let o=a(s),d=o&&o.length>0?o[0]:"*",h=[["*",l.t("matched")]];return e`
            
            <!-- Show SELECT input with 'loading' until the assets are retrieved -->
            ${m(!this._assets,()=>e`
                <or-mwc-input id="matchSelect" class="min-width" type="${c.SELECT}" .readonly="${!0}" .label="${l.t("loading")}"></or-mwc-input>
            `,()=>{var s,a,o;let m;this._assets.length<=25?h.push(...this._assets.map(t=>[t.id,t.name])):t=t=>__awaiter(this,void 0,void 0,function*(){var e;if(t)return this._assets.filter(e=>{var i;return null==(i=e.name)?void 0:i.toLowerCase().includes(t.toLowerCase())}).map(t=>[t.id,t.name]);if(this._assets.length<=100)return h.push(...this._assets.map(t=>[t.id,t.name])),h;{let t=null==(e=this._assets)?void 0:e.find(t=>t.id==d);return t&&void 0==h.find(([e,i])=>e==t.id)&&h.push([t.id,t.name]),h}});let b=d&&"*"!==d?this._assets.find(t=>t.id===d):void 0,v=n.getAttributeAndValueDescriptors(i,this.action.attributeName,b&&b.attributes&&this.action.attributeName?b.attributes[this.action.attributeName]:void 0),f=[];if(b&&b.attributes)f=Object.values(b.attributes).map(t=>{let e=u.getAttributeLabel(t,v[0],i,!1);return[t.name,e]});else if(r){let t=n.getAssetTypeInfo(r);f=t&&t.attributeDescriptors?t.attributeDescriptors.map(t=>{let e=u.getAttributeLabel(t,v[0],i,!1);return[t.name,e]}):[]}if(f.sort(u.sortByString(t=>t[1])),this.action.attributeName){let t,r=v[1]&&"boolean"===v[1].name?"":l.t("value");(null==(a=null==(s=v[0])?void 0:s.format)?void 0:a.asSlider)&&(t=c.NUMBER),m=e`<or-attribute-input ?compact=${v[1]&&"GEO_JSONPoint"===v[1].name} .inputType="${p(t)}" @or-attribute-input-changed="${t=>this.setActionAttributeValue(t.detail.value)}" .customProvider="${null==(o=this.config)?void 0:o.inputProvider}" .label="${r}" .assetType="${i}" .attributeDescriptor="${v[0]}" .attributeValueDescriptor="${v[1]}" .value="${this.action.value}" .readonly="${this.readonly||!1}"></or-attribute-input>`}return e`
                    <or-mwc-input id="matchSelect" class="min-width" .label="${l.t("asset")}" .type="${c.SELECT}"
                                  .options="${h}" .searchProvider="${t}" .value="${d}" .readonly="${this.readonly||!1}"
                                  @or-mwc-input-changed="${t=>{this._assetId=t.detail.value,this.refresh()}}"
                    ></or-mwc-input>
                    ${f.length>0?e`
                        <or-mwc-input id="attributeSelect" class="min-width" .label="${l.t("attribute")}" .type="${c.SELECT}" @or-mwc-input-changed="${t=>this.setActionAttributeName(t.detail.value)}" .readonly="${this.readonly||!1}" ?searchable="${f.length>=25}" .options="${f}" .value="${this.action.attributeName}"></or-mwc-input>
                        ${m}
                    `:e`
                        <or-translate value="No attributes with write permission"></or-translate>
                    `}
                `})}
        `}set _assetId(t){let e=this._getAssetType();"*"===t?(this.action.target.assets=void 0,this.action.target={matchedAssets:{types:[e||""]}}):(this.action.target.matchedAssets=void 0,this.action.target={assets:{ids:[t],types:[e||""]}}),this.dispatchEvent(new h),this.requestUpdate()}setActionAttributeName(t){this.action.attributeName=t,this.action.value=void 0,this.dispatchEvent(new h),this.requestUpdate()}setActionAttributeValue(t){this.action.value=t,this.dispatchEvent(new h),this.requestUpdate()}loadAssets(t){this.assetProvider(t).then(t=>{this._assets=t})}};__decorate([r({type:Object,attribute:!1})],OrRuleActionAttribute.prototype,"action",void 0),__decorate([r({type:Object,attribute:!1})],OrRuleActionAttribute.prototype,"targetTypeMap",void 0),__decorate([r({type:Object})],OrRuleActionAttribute.prototype,"config",void 0),__decorate([r({type:Object})],OrRuleActionAttribute.prototype,"assetInfos",void 0),__decorate([r({type:Object})],OrRuleActionAttribute.prototype,"assetProvider",void 0),__decorate([r({type:Array,attribute:!1})],OrRuleActionAttribute.prototype,"_assets",void 0),OrRuleActionAttribute=__decorate([s("or-rule-action-attribute")],OrRuleActionAttribute);export{OrRuleActionAttribute};