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{c(s.next(t))}catch(t){a(t)}}function n(t){try{c(s.throw(t))}catch(t){a(t)}}function c(t){var e;t.done?r(t.value):((e=t.value)instanceof i?e:new i(function(t){t(e)})).then(o,n)}c((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,state as a}from"lit/decorators.js";import{getAssetIdsFromQuery as o,getAssetTypeFromQuery as n}from"../index";import{AssetModelUtil as c}from"@openremote/model";import{Util as l}from"@openremote/core";import"@openremote/or-attribute-input";import{InputType as u}from"@openremote/or-mwc-components/or-mwc-input";import{i18next as d,translate as h}from"@openremote/or-translate";import{OrRulesJsonRuleChangedEvent as p}from"./or-rule-json-viewer";import{ifDefined as m}from"lit/directives/if-defined.js";import{when as v}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 h(d)(i){constructor(){super(...arguments),this._loading=!1}static get styles(){return style}shouldUpdate(t){return t.has("action")&&(this._cache=void 0),super.shouldUpdate(t)}refresh(){this._cache=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?n(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._cache||this._loading||this.loadAssets(i);let a=o(s),n=a&&a.length>0?a[0]:"*",h=new Map([["*",d.t("matched")]]),p=this._cache?this._cache.assets:[],f=p.length>25;return f&&this._selected&&h.set(this._selected.id,this._selected.name),e`
            
            <!-- Show SELECT input with 'loading' until the assets are retrieved -->
            ${v(!this._cache||this._loading,()=>e`
                <or-mwc-input id="matchSelect" class="min-width" type="${u.SELECT}" .readonly="${!0}" .label="${d.t("loading")}"></or-mwc-input>
            `,()=>{var s,a,o;let v;f?t=t=>__awaiter(this,void 0,void 0,function*(){if(yield this.loadAssets(i,t,n),t)return p.filter(e=>{var i;return null==(i=e.name)?void 0:i.toLowerCase().includes(t.toLowerCase())}).map(t=>[t.id,t.name]);if(p.length<=100)return p.forEach(t=>h.set(t.id,t.name)),[...h];{let t=p.find(t=>t.id===n);return t&&!Array.from(h.keys()).includes(t.id)&&h.set(t.id,t.name),[...h]}}):p.forEach(t=>h.set(t.id,t.name));let b=n&&"*"!==n?this._cache.assets.find(t=>t.id===n):void 0,_=c.getAttributeAndValueDescriptors(i,this.action.attributeName,b&&b.attributes&&this.action.attributeName?b.attributes[this.action.attributeName]:void 0),y=[];if(b&&b.attributes)y=Object.values(b.attributes).map(t=>{let e=l.getAttributeLabel(t,_[0],i,!1);return[t.name,e]});else if(r){let t=c.getAssetTypeInfo(r);y=t&&t.attributeDescriptors?t.attributeDescriptors.map(t=>{let e=l.getAttributeLabel(t,_[0],i,!1);return[t.name,e]}):[]}if(y.sort(l.sortByString(t=>t[1])),this.action.attributeName){let t,r=_[1]&&"boolean"===_[1].name?"":d.t("value");(null==(a=null==(s=_[0])?void 0:s.format)?void 0:a.asSlider)&&(t=u.NUMBER),v=e`<or-attribute-input ?compact=${_[1]&&"GEO_JSONPoint"===_[1].name} .inputType="${m(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="${_[0]}" .attributeValueDescriptor="${_[1]}" .value="${this.action.value}" .readonly="${this.readonly||!1}"></or-attribute-input>`}return e`
                    <or-mwc-input id="matchSelect" class="min-width" .label="${d.t("asset")}" .type="${u.SELECT}"
                                  .options="${[...h]}" .searchProvider="${t}" .value="${n}" .readonly="${this.readonly||!1}"
                                  @or-mwc-input-changed="${t=>{this._assetId=t.detail.value}}"
                    ></or-mwc-input>
                    ${y.length>0?e`
                        <or-mwc-input id="attributeSelect" class="min-width" .label="${d.t("attribute")}" .type="${u.SELECT}" @or-mwc-input-changed="${t=>this.setActionAttributeName(t.detail.value)}" .readonly="${this.readonly||!1}" ?searchable="${y.length>=25}" .options="${y}" .value="${this.action.attributeName}"></or-mwc-input>
                        ${v}
                    `:e`
                        <or-translate value="No attributes with write permission"></or-translate>
                    `}
                `})}
        `}set _assetId(t){var e,i;let s=this._getAssetType();t&&"*"!==t?(this._selected=null==(i=null==(e=this._cache)?void 0:e.assets)?void 0:i.find(e=>e.id===t),this.action.target.matchedAssets=void 0,this.action.target={assets:{ids:[t],types:[s||""]}}):(this.action.target.assets=void 0,this.action.target={matchedAssets:{types:[s||""]}}),this.dispatchEvent(new p),this.requestUpdate()}setActionAttributeName(t){this.action.attributeName=t,this.action.value=void 0,this.dispatchEvent(new p),this.requestUpdate()}setActionAttributeValue(t){this.action.value=t,this.dispatchEvent(new p),this.requestUpdate()}loadAssets(t,e,i){return __awaiter(this,void 0,void 0,function*(){var s,r,a,o,n,c,u;let d=[],h={limit:100};e&&(null!=h.names||(h.names=[]),h.names.push({predicateType:"string",value:e}));let p=(null==(s=this._cache)?void 0:s.query)&&l.objectsEqual(this._cache.query,h,!0);if(!this._loading&&!p){this._loading=!0,d.push(this.assetProvider(t,Object.assign({},h))),i&&"*"!==i&&d.push(this.assetProvider(t,{ids:[i]}));let e=yield Promise.all(d);this._loading=!1;let s=e.filter(t=>!!t).flat(),l=null!=(a=null==(r=this._cache)?void 0:r.assets.map(t=>t.id))?a:[];return this._cache={query:h,assets:[...null!=(n=null==(o=this._cache)?void 0:o.assets)?n:[],...null!=(c=null==s?void 0:s.filter(t=>!l.includes(t.id)))?c:[]]},s}return null==(u=this._cache)?void 0:u.assets})}};__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([a()],OrRuleActionAttribute.prototype,"_cache",void 0),__decorate([a()],OrRuleActionAttribute.prototype,"_selected",void 0),OrRuleActionAttribute=__decorate([s("or-rule-action-attribute")],OrRuleActionAttribute);export{OrRuleActionAttribute};