var OrRuleWhen_1,ResetOption,__decorate=this&&this.__decorate||function(e,t,o,r){var i,n=arguments.length,s=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,o,r);else for(var l=e.length-1;l>=0;l--)(i=e[l])&&(s=(n<3?i(s):n>3?i(t,o,s):i(t,o))||s);return n>3&&s&&Object.defineProperty(t,o,s),s};import{css as e,html as t,LitElement as o}from"lit";import{customElement as r,property as i}from"lit/decorators.js";import{OrRulesRuleUnsupportedEvent as n}from"../index";import{buttonStyle as s}from"../style";import{getWhenTypesMenu as l,updateRuleConditionType as p}from"./or-rule-condition";import{i18next as a,translate as u}from"@openremote/or-translate";import{InputType as h}from"@openremote/or-mwc-components/or-mwc-input";import{OrRulesJsonRuleChangedEvent as c}from"./or-rule-json-viewer";import{getContentWithMenuTemplate as d}from"@openremote/or-mwc-components/or-mwc-menu";!function(e){e.NO_LONGER_MATCHES="noLongerMatches",e.VALUE_CHANGES="valueChanges"}(ResetOption||(ResetOption={}));let style=e`
    
    :host {
        display: block;
    }
    
    ${s}
    
    .rule-group-items {
        display: flex;
        flex-direction: column;
    }
    
    .rule-group .rule-group-items > div {
        margin: 10px 0;
    }
    
    .rule-condition {
        display: flex;
        padding-right: 5px;
    }
     
    .rule-condition > * {
        flex-grow: 0;
    }
    
    .rule-condition > or-rule-condition {
        flex-grow: 1;
    }
    
    or-rule-condition {
        margin-bottom: 10px;
    }

    .rule-group-item > or-icon {
        padding-left: 17px;
    }
    
    or-icon.small {
        --or-icon-width: 14px;
        --or-icon-height: 14px;
    }
    
    or-panel {
        margin: 10px 10px 20px 10px;
        position: relative;
    }
    
    or-panel > .remove-button {
        position: absolute;
        right: 0;
        top: 0;
        width: 24px;
        height: 24px;
        transform: translate(50%, -50%);
    }

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

    or-panel:hover .remove-button.button-clear {
        visibility: visible;
    }
    
    .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;
    }

    strong {
        margin: var(--internal-or-panel-heading-margin);
        font-size: var(--internal-or-panel-heading-font-size);
    }
`,OrRuleWhen=OrRuleWhen_1=class extends u(a)(o){static get styles(){return style}ruleGroupTemplate(e,o){let r;if(!e)return t``;let i=!o,n=!this.readonly&&(!this.config||!this.config.controls||!0!==this.config.controls.hideWhenAddCondition),s=!this.readonly,p=!this.readonly;r=i?(e,o,r,i,n)=>t`
                    <or-panel .heading="${a.t(n?"when":"orWhen")}...">
                        ${p?t`
                            <button class="button-clear remove-button" @click="${()=>this.removeItem(o,r,i)}">
                                <or-icon icon="close-circle"></or-icon>  
                            </button>
                        `:""}
                        ${e}
                    </or-panel>`:(e,o,r,i,n)=>t`
                    ${!n?t`
                        <or-icon class="small" icon="ampersand"></or-icon>
                    `:""}
                    ${e}
                `;let u="",c="",m="",g=!0;return e.groups&&e.groups.length>0&&(u=t`
                ${e.groups.map((o,i)=>r(t`
                        <div class="rule-group-item">
                            ${this.ruleGroupTemplate(o,e)}
                        </div>
                    `,o,e,!0,0==i))}
            `,g=!1),e.items&&e.items.length>0&&(c=t`
                ${e.items.map((o,i)=>r(t`
                        <div class="rule-group-item">
                            <div class="rule-condition">
                                <or-rule-condition .config="${this.config}" .assetInfos="${this.assetInfos}" .ruleCondition="${o}" .readonly="${this.readonly}" .assetProvider="${this.assetProvider}"></or-rule-condition>
                                ${p?t`
                                    <button class="button-clear ${s?"":"hidden"}" @click="${()=>this.removeItem(o,e,!1)}"><or-icon icon="close-circle"></or-icon></input>
                                `:""}
                            </div>
                        </div>
                    `,o,e,!0,g&&0===i))}
            `,g=!1),!i&&n&&(m=t`
                <span class="add-button-wrapper">
                    ${d(t`<or-mwc-input class="plus-button" type="${h.BUTTON}" icon="plus"
                                           .label="${a.t("rulesEditorAddCondition")}"></or-mwc-input>`,l(this.config,this.assetInfos),void 0,t=>this.addCondition(e,t))}
                </span>
            `),t`
            ${u}
            ${c}
            ${m}
        `}dateTimePredicateTemplate(){return t`<span>DATE TIME PREDICATE NOT IMPLEMENTED</span>`}shouldUpdate(e){return e.has("rule")&&this.rule&&(this.rule.when?OrRuleWhen_1._isRuleWhenCompatible(this.rule.when)||(this.rule=void 0,this.dispatchEvent(new n)):this.rule.when={operator:"OR"}),super.shouldUpdate(e)}render(){if(!this.rule||!this.rule.when)return t``;let e=!this.readonly&&(!this.config||!this.config.controls||!0!==this.config.controls.hideWhenAddGroup);return t`
            <div>
                ${this.ruleGroupTemplate(this.rule.when)}
            </div>
            
            ${!e?"":t`
                <or-panel>
                    <strong>${a.t(!this.rule.when.groups||0===this.rule.when.groups.length?"when":"orWhen")}...</strong>
                    <span class="add-button-wrapper">
                        ${d(t`<or-mwc-input class="plus-button" type="${h.BUTTON}" icon="plus"></or-mwc-input>`,l(this.config,this.assetInfos),void 0,e=>this.addGroup(this.rule.when,e))}
                    </span>
                </or-panel>
            `}
        `}static _isRuleWhenCompatible(e){return"OR"!==e.operator?(console.warn("Incompatible rule: when operator not set to 'OR'"),!1):e.items&&e.items.length>0?(console.warn("Incompatible rule: when items not supported"),!1):!(e.groups&&e.groups.find(e=>!this._isWhenGroupCompatible(e)))||(console.warn("Incompatible rule: when groups incompatible"),!1)}static _isWhenGroupCompatible(e){return"OR"===e.operator?(console.warn("Incompatible rule: when group operator not set to 'AND'"),!1):!e.groups||!(e.groups.length>0)||(console.warn("Incompatible rule: when group nested groups not supported"),!1)}addGroup(e,t){if(!this.rule||!this.rule.when||e!==this.rule.when)return;let o={operator:"AND"};if(this.config&&this.config.json&&this.config.json.whenGroup&&(o=JSON.parse(JSON.stringify(this.config.json.whenGroup))),"AND"!==o.operator){console.warn("JSON rules editor doesn't support top level logic group with type OR"),this.dispatchEvent(new n);return}if(o.groups&&o.groups.length>0){console.warn("JSON rules editor doesn't support multiple top level logic groups"),this.dispatchEvent(new n);return}o.groups=void 0,o.items&&0!==o.items.length||this.addCondition(o,t,!0),e.groups||(e.groups=[]),e.groups.push(o),this.dispatchEvent(new c),this.requestUpdate()}removeItem(e,t,o){let r=!1;if(t){if(o){let o=t.groups.indexOf(e);t.groups.splice(o,1),r=o>=0}else{let o=t.items.indexOf(e);t.items.splice(o,1),r=o>=0}r&&(this.dispatchEvent(new c),this.requestUpdate())}}addCondition(e,t,o){if(!e)return;e.items||(e.items=[]);let r={};this.config&&this.config.json&&this.config.json.whenCondition?r=JSON.parse(JSON.stringify(this.config.json.whenCondition)):p(r,t||"ThingAsset",this.config),e.items.push(r),o||(this.dispatchEvent(new c),this.requestUpdate())}};__decorate([i({type:Object})],OrRuleWhen.prototype,"rule",void 0),__decorate([i({type:Boolean})],OrRuleWhen.prototype,"readonly",void 0),__decorate([i({type:Object})],OrRuleWhen.prototype,"assetProvider",void 0),__decorate([i({type:Object,attribute:!1})],OrRuleWhen.prototype,"assetInfos",void 0),OrRuleWhen=OrRuleWhen_1=__decorate([r("or-rule-when")],OrRuleWhen);