var __decorate=this&&this.__decorate||function(e,t,o,r){var i,s=arguments.length,n=s<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,t,o,r);else for(var p=e.length-1;p>=0;p--)(i=e[p])&&(n=(s<3?i(n):s>3?i(t,o,n):i(t,o))||n);return s>3&&n&&Object.defineProperty(t,o,n),n};import{css as e,html as t,LitElement as o}from"lit";import{customElement as r,property as i,query as s}from"lit/decorators.js";import{AssetModelUtil as n}from"@openremote/model";import{getAssetTypeFromQuery as p}from"../index";import"./or-rule-asset-query";import"./or-rule-trigger-query";import{getContentWithMenuTemplate as l}from"@openremote/or-mwc-components/or-mwc-menu";import"@openremote/or-icon";import{i18next as c,translate as a}from"@openremote/or-translate";import{InputType as u}from"@openremote/or-mwc-components/or-mwc-input";import{Util as d}from"@openremote/core";import{OrRulesJsonRuleChangedEvent as y}from"./or-rule-json-viewer";let TIMER_COLOR="4b87ea",DATE_TIME_COLOR="6AEAA4";export function getWhenTypesMenu(e,t){let o=!0,r=!0,i=!0;e&&e.controls&&e.controls.allowedConditionTypes&&(o=e.controls.allowedConditionTypes.indexOf("assetQuery")>=0,r=e.controls.allowedConditionTypes.indexOf("agentQuery")>=0,i=e.controls.allowedConditionTypes.indexOf("time")>=0);let s=[];if(t){if(o){let e=t.filter(e=>"agent"!==e.assetDescriptor.descriptorType).map(e=>{let t=n.getAssetDescriptorColour(e),o=n.getAssetDescriptorIcon(e);return{text:d.getAssetTypeLabel(e.assetDescriptor),value:e.assetDescriptor.name,icon:o||n.getAssetDescriptorIcon("ThingAsset"),styleMap:t?{"--or-icon-fill":"#"+t}:void 0}});s.push(...e.sort(d.sortByString(e=>e.text)))}if(o&&r&&s.push(null),r){let e=t.filter(e=>"agent"===e.assetDescriptor.descriptorType).map(e=>{let t=n.getAssetDescriptorColour(e),o=n.getAssetDescriptorIcon(e);return{text:d.getAssetTypeLabel(e.assetDescriptor),value:e.assetDescriptor.name,icon:o||n.getAssetDescriptorIcon("ThingAsset"),styleMap:t?{"--or-icon-fill":"#"+t}:void 0}});s.push(...e.sort(d.sortByString(e=>e.text)))}}return i&&(s.push(null),s.push({text:c.t("time"),icon:"timer",value:"time",styleMap:{"--or-icon-fill":"#"+TIMER_COLOR}})),s}export function updateRuleConditionType(e,t,o){if(t)if("time"===t){e.assets=void 0;let t=new Date;e.cron=d.formatCronString(void 0,void 0,void 0,t.getUTCHours().toString(),t.getUTCMinutes().toString())}else e.cron=void 0,o&&o.json&&o.json.whenAssetQuery?e.assets=JSON.parse(JSON.stringify(o.json.whenAssetQuery)):e.assets={},e.assets.types=[t];else e.assets=void 0,e.cron=void 0,e.sun=void 0}let style=e`
    :host {
        display: flex;
        flex-direction: row;
    }
    
    or-rule-asset-query {
        flex-grow: 1;
    }

    #type {
        white-space: nowrap;
        text-transform: capitalize;
        margin: 14px 3px auto 0;
    }
`,OrRuleCondition=class extends a(c)(o){constructor(){super(...arguments),this.readonly=!1}static get styles(){return style}render(){let e=!this.config||!this.config.controls||!0!==this.config.controls.hideConditionTypeOptions,o=this.type;if(!o&&!e)return t`<span>INVALID CONFIG - NO TYPE SPECIFIED AND TYPE SELECTOR DISABLED</span>`;let r="",i="";if(e){let e,i="inherit";if(o)if("time"===o)e="timer",i=TIMER_COLOR;else{let t=n.getAssetDescriptor(o);e=n.getAssetDescriptorIcon(t)||e,i=n.getAssetDescriptorColour(t)||i}r=this.readonly?t`
                <div id="type" style="--or-mwc-input-color: #${i}">
                    <or-mwc-input readonly type="${u.BUTTON}" .icon="${e||""}"></or-mwc-input>
                </div>
                `:t`
                <div id="type" style="--or-mwc-input-color: #${i}">
                    ${l(t`<or-mwc-input type="${u.BUTTON}" .icon="${e||""}"></or-mwc-input>`,getWhenTypesMenu(this.config,this.assetInfos),o,e=>this.type=e)}
                </div>
            `}return o&&(i="time"===o?t`<or-rule-trigger-query id="asset-query" .condition="${this.ruleCondition}"></or-rule-trigger-query>`:t`<or-rule-asset-query id="asset-query" .config="${this.config}" .assetInfos="${this.assetInfos}" .readonly="${this.readonly}"
                                                         .condition="${this.ruleCondition}" .assetProvider="${this.assetProvider}"
                    ></or-rule-asset-query>`),t`
            ${r}        
            ${i}
        `}get type(){let e=p(this.ruleCondition.assets);return e||(this.ruleCondition.cron||this.ruleCondition.sun?"time":void 0)}set type(e){updateRuleConditionType(this.ruleCondition,e,this.config),this._assetQuery&&this._assetQuery.refresh(),this.dispatchEvent(new y),this.requestUpdate()}};__decorate([i({type:Object,attribute:!1})],OrRuleCondition.prototype,"ruleCondition",void 0),__decorate([i({type:Object})],OrRuleCondition.prototype,"config",void 0),__decorate([i({type:Object})],OrRuleCondition.prototype,"assetInfos",void 0),__decorate([i({type:Object})],OrRuleCondition.prototype,"assetProvider",void 0),__decorate([s("#asset-query")],OrRuleCondition.prototype,"_assetQuery",void 0),OrRuleCondition=__decorate([r("or-rule-condition")],OrRuleCondition);