var __decorate=this&&this.__decorate||function(e,t,i,o){var r,n=arguments.length,a=n<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,i):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,i,o);else for(var s=e.length-1;s>=0;s--)(r=e[s])&&(a=(n<3?r(a):n>3?r(t,i,a):r(t,i))||a);return n>3&&a&&Object.defineProperty(t,i,a),a};import{css as e,html as t,LitElement as i}from"lit";import{customElement as o,property as r}from"lit/decorators.js";import{buttonStyle as n}from"../style";import"./or-rule-asset-query";import{getAssetTypeFromQuery as a}from"../index";import{OrRulesJsonRuleChangedEvent as s}from"./or-rule-json-viewer";import{AssetModelUtil as l}from"@openremote/model";import{i18next as c,translate as u}from"@openremote/or-translate";import{InputType as p}from"@openremote/or-mwc-components/or-mwc-input";import{getContentWithMenuTemplate as h}from"@openremote/or-mwc-components/or-mwc-menu";import{manager as d,Util as m}from"@openremote/core";import"./or-rule-action-attribute";import"./or-rule-action-notification";import"./or-rule-action-webhook";import"./or-rule-action-alarm";let NOTIFICATION_COLOR="4B87EA",WAIT_COLOR="EACC54",ALARM_COLOR="FC2D2D";function getActionTypesMenu(e,t){let i=!0,o=!0,r=!0,n=!0,a=!0,s=!0,u=!0,p=!0,h=!1;e&&e.controls&&e.controls.allowedActionTypes&&(i=e.controls.allowedActionTypes.indexOf("attribute")>=0,o=e.controls.allowedActionTypes.indexOf("wait")>=0,r=e.controls.allowedActionTypes.indexOf("alarm")>=0,n=e.controls.allowedActionTypes.indexOf("email")>=0,a=e.controls.allowedActionTypes.indexOf("email_localized")>=0,s=e.controls.allowedActionTypes.indexOf("push")>=0,u=e.controls.allowedActionTypes.indexOf("push_localized")>=0,p=e.controls.allowedActionTypes.indexOf("webhook")>=0),(null==e?void 0:e.notifications)&&(h=Object.entries(e.notifications).filter(e=>(e[0]===d.displayRealm||"default"===e[0])&&void 0!==e[1]).length>0);let f=[];return i&&t&&f.push(...t.filter(e=>"agent"!==e.assetDescriptor.descriptorType).map(e=>{let t=l.getAssetDescriptorColour(e),i=l.getAssetDescriptorIcon(e);return{text:m.getAssetTypeLabel(e.assetDescriptor),value:e.assetDescriptor.name,icon:i||l.getAssetDescriptorIcon("ThingAsset"),styleMap:t?{"--or-icon-fill":"#"+t}:void 0}})),f.sort(m.sortByString(e=>null==e?void 0:e.value)),f.push(null),n&&f.push({text:c.t("email"),icon:"email",value:h&&a?"email_localized":"email",styleMap:{"--or-icon-fill":"#"+NOTIFICATION_COLOR}}),s&&f.push({text:c.t("push-notification"),icon:"cellphone-message",value:h&&u?"push_localized":"push",styleMap:{"--or-icon-fill":"#"+NOTIFICATION_COLOR}}),o&&f.push({text:c.t("wait"),icon:"timer",value:"wait",styleMap:{"--or-icon-fill":"#"+WAIT_COLOR}}),r&&f.push({text:c.t("alarm."),icon:"bell-outline",value:"alarm",styleMap:{"--or-icon-fill":"#"+ALARM_COLOR}}),p&&f.push({text:c.t("webhook"),icon:"webhook",value:"webhook",styleMap:{"--or-icon-fill":"#"+NOTIFICATION_COLOR}}),f}export var RecurrenceOption;function getRecurrenceMenu(e){return e&&e.controls&&e.controls.allowedRecurrenceOptions?e.controls.allowedRecurrenceOptions.map(e=>({text:c.t(e),value:e})):Object.values(RecurrenceOption).map(e=>({text:c.t(e),value:e}))}!function(e){e.ALWAYS="always",e.ONCE="once",e.ONCE_PER_HOUR="oncePerHour",e.ONCE_PER_DAY="oncePerDay",e.ONCE_PER_WEEK="oncePerWeek"}(RecurrenceOption||(RecurrenceOption={}));let style=e`

    :host {
        display: flex;
        width: 100%;
        flex-direction: column;
    }
        
    ${n}
    
    .rule-action {
        display: flex;
        margin: 10px 0;
    }
    
    .rule-action-wrapper {
        flex-grow: 1;
        display: flex;
        align-items: baseline;
    }
    
    .rule-action > button {
        flex-grow: 0;
    }

    .rule-action:hover .button-clear {
        visibility: visible;
    }

    or-panel:hover .remove-button.button-clear {
        visibility: visible;
    }
            
    or-panel {
        position: relative;
        margin: 10px 10px 20px 10px;
    }
    
    #type-selector {
        margin-top: 10px;
    }    
        
    .add-button-wrapper {
        display: flex;
        align-items: center;
        white-space: nowrap;
    }
    
    .add-button-wrapper > * {
        margin-right: 6px;
    }
    
    .add-button-wrapper or-mwc-menu {
        text-transform: capitalize;
    }
    
    #type {
        white-space: nowrap;
        margin: 4px 3px auto 0;
        text-transform: capitalize;
    }
    
    .rule-recurrence {
        position: absolute;
        top: 5px;
        right: 0;
    }
   
`,OrRuleThenOtherwise=class extends u(c)(i){static get styles(){return style}get thenAllowAdd(){return!this.config||!this.config.controls||!0!==this.config.controls.hideThenAddAction}ruleRecurrenceTemplate(e,i){let o="",r="inherit",n=RecurrenceOption.ONCE;return e&&(void 0===e.mins||null===e.mins?n=RecurrenceOption.ONCE:0===e.mins?n=RecurrenceOption.ALWAYS:60===e.mins?n=RecurrenceOption.ONCE_PER_HOUR:1440===e.mins?n=RecurrenceOption.ONCE_PER_DAY:10080===e.mins&&(n=RecurrenceOption.ONCE_PER_WEEK)),o=i?t`
                <div style="--or-mwc-input-color: ${r}; margin-right: 6px;">
                    <or-mwc-input .type="${p.BUTTON}" label="${n}"></or-mwc-input>
                </div>
            `:t`
                <div style="--or-mwc-input-color: ${r}; margin-right: 6px;">
                    ${h(t`<or-mwc-input .type="${p.BUTTON}" label="${n}"></or-mwc-input>`,getRecurrenceMenu(this.config),n,e=>this.setRecurrenceOption(e))}
                </div>
            `,t`
            <div class="rule-recurrence">
                ${o}
            </div>
        `}ruleActionTemplate(e,i,o){let r=!this.config||!this.config.controls||!0!==this.config.controls.hideActionTypeOptions,n=this.getActionType(i);if(!n&&!r)return t`<span>INVALID CONFIG - NO TYPE SPECIFIED AND TYPE SELECTOR DISABLED</span>`;let a="",s="",c=!this.readonly&&(this.thenAllowAdd||this.rule.then.length>1);if(r){let r,s="inherit";if(i.action)switch(i.action){case"wait":r="timer",s=WAIT_COLOR;break;case"webhook":r="webhook",s=NOTIFICATION_COLOR;break;case"notification":r="push"===n||"push_localized"===n?"cellphone-message":"email",s=NOTIFICATION_COLOR;break;case"alarm":r="bell-outline",s=ALARM_COLOR;break;default:let c=l.getAssetDescriptor(n);r=l.getAssetDescriptorIcon(c),s=l.getAssetDescriptorColour(c)||s}a=o?t`
                    <div id="type" style="--or-mwc-input-color: #${s}">
                        <or-mwc-input type="${p.BUTTON}" .icon="${r||""}"></or-mwc-input>
                    </div>
                `:t`
                    <div id="type" style="--or-mwc-input-color: #${s}">
                        ${h(t`<or-mwc-input type="${p.BUTTON}" .icon="${r||""}"></or-mwc-input>`,getActionTypesMenu(this.config,this.assetInfos),i.action,t=>this.setActionType(e,i,t))}
                    </div>
                `}if(n)switch(n){case"wait":s=t`<span>WAIT NOT IMPLEMENTED</span>`;break;case"push":case"push_localized":case"email":case"email_localized":let u=n+"-notification";s=t`<or-rule-action-notification id="${u}" .rule="${this.rule}" .action="${i}" .actionType="${n}" .config="${this.config}"
                                                                 .assetInfos="${this.assetInfos}" .readonly="${this.readonly}"></or-rule-action-notification>`;break;case"webhook":s=t`<or-rule-action-webhook .rule="${this.rule}" .action="${i}" .actionType="${"webhook"}"></or-rule-action-webhook>`;break;case"alarm":s=t`<or-rule-action-alarm .rule="${this.rule}" .action="${i}" .actionType="${"alarm"}"></or-rule-action-alarm>`;break;default:s=t`<or-rule-action-attribute .action="${i}" .targetTypeMap="${this.targetTypeMap}" .config="${this.config}" .assetProvider="${this.assetProvider}"
                                                              .assetInfos="${this.assetInfos}" .readonly="${this.readonly}"></or-rule-action-attribute>`}return t`
            <div class="rule-action">
                <div class="rule-action-wrapper">
                    ${a}
                    ${s}
                </div>
                    ${c?t`
                        <button class="button-clear" @click="${()=>this.removeAction(i)}"><or-icon icon="close-circle"></or-icon></input>
                    `:""}
            </div>
        `}render(){let e=!this.readonly&&(this.thenAllowAdd||this.rule.then.length>1);return t`
            <div>
                <or-panel .heading="${c.t("then")}...">
                    ${this.ruleRecurrenceTemplate(this.rule.recurrence,this.readonly)}

                    ${!this.rule.then?"":this.rule.then.map(e=>this.ruleActionTemplate(this.rule.then,e,this.readonly))}
                    ${e?t`
                        <span class="add-button-wrapper">
                            ${h(t`<or-mwc-input class="plus-button" type="${p.BUTTON}" icon="plus"
                                                   .label="${c.t("rulesEditorAddAction")}"></or-mwc-input>`,getActionTypesMenu(this.config,this.assetInfos),void 0,e=>this.addAction(e))}
                        </span>
                    `:""}
                </or-panel>
            </div>
        `}getActionType(e){switch(e.action){case"wait":break;case"webhook":case"alarm":return e.action;case"notification":let t=e.notification&&e.notification.message&&e.notification.message.type?e.notification.message.type:e.action;if("localized"===t){let t=Object.values(e.notification.message.languages);if(t&&t.length>0)return t[0].type+"_localized"}return t;case"write-attribute":case"update-attribute":if(!e.target)return;if(e.target.matchedAssets)return a(e.target.matchedAssets);if(e.target.assets)return a(e.target.assets)}}setRecurrenceOption(e){switch(e){case RecurrenceOption.ALWAYS:this.rule.recurrence={mins:0};break;case RecurrenceOption.ONCE_PER_HOUR:this.rule.recurrence={mins:60};break;case RecurrenceOption.ONCE_PER_DAY:this.rule.recurrence={mins:1440};break;case RecurrenceOption.ONCE_PER_WEEK:this.rule.recurrence={mins:10080};break;case RecurrenceOption.ONCE:default:delete this.rule.recurrence}this.dispatchEvent(new s),this.requestUpdate()}setActionType(e,t,i){var o,r,n,a,l,c;switch(t.target=void 0,t.action){case"wait":t.millis=void 0;break;case"webhook":break;case"write-attribute":t.value=void 0,t.attributeName=void 0;break;case"notification":t.notification=void 0;break;case"alarm":t.alarm=void 0,t.assigneeId=void 0;break;case"update-attribute":t.value=void 0,t.attributeName=void 0,t.index=void 0,t.key=void 0,t.updateAction=void 0}if("wait"===i)t.action="wait";else if("alarm"==i)t.action="alarm",t.alarm={title:"%RULESET_NAME%",content:"%TRIGGER_ASSETS%",severity:"LOW",status:"OPEN"},t.assigneeId=void 0;else if("webhook"==i)t.action="webhook",t.webhook={httpMethod:"POST",payload:JSON.stringify({rule:"%RULESET_NAME%",assets:"%TRIGGER_ASSETS%"},null,4)};else if("email"===i)t.action="notification",t.notification={name:this.rule.name,message:{type:"email",subject:"%RULESET_NAME%",html:"%TRIGGER_ASSETS%"}};else if("push"===i)t.action="notification",t.notification={name:this.rule.name,message:{type:"push",title:"%RULESET_NAME%",body:"%TRIGGER_ASSETS%",action:{openInBrowser:!1}}};else if("email_localized"===i||"push_localized"===i){t.action="notification";let e=(null==(n=null==(r=null==(o=this.config)?void 0:o.notifications)?void 0:r[d.displayRealm])?void 0:n.defaultLanguage)||(null==(c=null==(l=null==(a=this.config)?void 0:a.notifications)?void 0:l.default)?void 0:c.defaultLanguage)||d.config.defaultLanguage||"en",s={};"email_localized"===i?s[e]={type:"email",subject:"%RULESET_NAME%",html:"%TRIGGER_ASSETS%"}:s[e]={type:"push",title:"%RULESET_NAME%",body:"%TRIGGER_ASSETS%",action:{openInBrowser:!1}},t.notification={name:this.rule.name,message:{type:"localized",defaultLanguage:e,languages:s}}}else t.action="write-attribute",t.target={matchedAssets:{types:[i]}};let u=e.indexOf(t);e[u]=Object.assign({},t),this.dispatchEvent(new s),this.requestUpdate()}removeAction(e){if(!this.rule||!this.rule.then||!e)return;let t=this.rule.then.indexOf(e);t>=0&&(this.rule.then.splice(t,1),this.dispatchEvent(new s),this.requestUpdate())}addAction(e,t){let i,o;if(!this.rule)return;let r={action:"write-attribute"},n=this.config&&this.config.json?this.config.json:void 0;t?(this.rule.otherwise||(this.rule.otherwise=[]),i=this.rule.otherwise,o=n&&n.otherwise?n.otherwise:void 0):(this.rule.then||(this.rule.then=[]),i=this.rule.then,o=n&&n.then?n.then:void 0),o&&(r=JSON.parse(JSON.stringify(o))),e&&this.setActionType(i,r,e),i.push(r),this.dispatchEvent(new s),this.requestUpdate()}};__decorate([r({type:Array,attribute:!1})],OrRuleThenOtherwise.prototype,"targetTypeMap",void 0),__decorate([r({type:Object,attribute:!1})],OrRuleThenOtherwise.prototype,"rule",void 0),__decorate([r({type:Boolean})],OrRuleThenOtherwise.prototype,"readonly",void 0),__decorate([r({type:Object,attribute:!1})],OrRuleThenOtherwise.prototype,"assetInfos",void 0),__decorate([r({type:Object})],OrRuleThenOtherwise.prototype,"assetProvider",void 0),OrRuleThenOtherwise=__decorate([o("or-rule-then-otherwise")],OrRuleThenOtherwise);