var __decorate=this&&this.__decorate||function(t,o,e,i){var n,r=arguments.length,a=r<3?o:null===i?i=Object.getOwnPropertyDescriptor(o,e):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,o,e,i);else for(var l=t.length-1;l>=0;l--)(n=t[l])&&(a=(r<3?n(a):r>3?n(o,e,a):n(o,e))||a);return r>3&&a&&Object.defineProperty(o,e,a),a};import{html as t,LitElement as o}from"lit";import{customElement as e,property as i,query as n}from"lit/decorators.js";import{InputType as r}from"@openremote/or-mwc-components/or-mwc-input";import{i18next as a,translate as l}from"@openremote/or-translate";import{OrMwcDialogOpenedEvent as c}from"@openremote/or-mwc-components/or-mwc-dialog";import{OrRulesJsonRuleChangedEvent as s}from"../or-rule-json-viewer";let checkValidity=t=>{if(t){let o=t.querySelectorAll("or-mwc-input");return Array.prototype.slice.call(o).every(t=>{if(!t.shadowRoot)return!1;{let o=t.shadowRoot.querySelector("input, textarea, select");return!!(o&&o.checkValidity())||(t._mdcComponent&&(t._mdcComponent.valid=!1,t._mdcComponent.helperTextContent="required"),!1)}})}};export class OrRulesNotificationModalCancelEvent extends CustomEvent{constructor(){super(OrRulesNotificationModalCancelEvent.NAME,{bubbles:!0,composed:!0})}}OrRulesNotificationModalCancelEvent.NAME="or-rules-notification-modal-cancel";export class OrRulesNotificationModalOkEvent extends CustomEvent{constructor(){super(OrRulesNotificationModalOkEvent.NAME,{bubbles:!0,composed:!0})}}OrRulesNotificationModalOkEvent.NAME="or-rules-notification-modal-ok";let OrRuleNotificationModal=class extends l(a)(o){constructor(){super(),this.title="message",this.addEventListener(c.NAME,this.initDialog)}connectedCallback(){return this.addEventListener(s.NAME,this._onJsonRuleChanged),super.connectedCallback()}disconnectedCallback(){return this.removeEventListener(s.NAME,this._onJsonRuleChanged),super.disconnectedCallback()}_onJsonRuleChanged(){this.validateForm()}initDialog(){if(!this._orMwcDialog)return}renderDialogHTML(o){let e=this.shadowRoot.getElementById("notification-modal");if(!this.shadowRoot)return;let i=this.shadowRoot.querySelector(".notification-form-slot");if(e&&i){let o=document.createElement("div");i.assignedNodes({flatten:!0}).forEach(t=>{t instanceof HTMLElement&&o.appendChild(t)}),e.content=t`${o}`,e.dismissAction=null,this.requestUpdate()}}firstUpdated(t){t.has("action")&&this.renderDialogHTML(this.action),this.getUpdateComplete().finally(()=>{this.validateForm()})}validateForm(){var t,o,e;let i=this.checkForm();null==(t=this._orMwcDialog)||t.setActions(null==(e=null==(o=this._orMwcDialog)?void 0:o.actions)?void 0:e.map(t=>("ok"===t.actionName&&(t.disabled=!i),t)))}checkForm(){if(this.shadowRoot){let t=this._orMwcDialog,o=null==t?void 0:t.shadowRoot;if(t&&o){let t=o.querySelector("or-rule-form-email-message"),e=o.querySelector("or-rule-form-push-notification"),i=o.querySelector("or-rule-form-localized");if(null==e?void 0:e.shadowRoot)return checkValidity(e.shadowRoot.querySelector("form"));if(null==t?void 0:t.shadowRoot)return checkValidity(t.shadowRoot.querySelector("form"));if(null==i?void 0:i.shadowRoot)return i.isValid()}}}render(){let o=()=>{this.dispatchEvent(new OrRulesNotificationModalCancelEvent)},e=()=>{this.dispatchEvent(new OrRulesNotificationModalOkEvent)},i=[{actionName:"cancel",content:t`<or-mwc-input class="button" .type="${r.BUTTON}" label="cancel"></or-mwc-input>`,action:o},{actionName:"ok",content:"ok",disabled:!0,action:e}],n=t`
            <style>
                .mdc-dialog__actions {
                    justify-content: space-between !important;
                }
            </style>
        `,a=()=>{let t=this.shadowRoot.getElementById("notification-modal");t&&t.open()};return t`
            <or-mwc-input .type="${r.BUTTON}" label="message" @or-mwc-input-changed="${a}"></or-mwc-input>
            <or-mwc-dialog id="notification-modal" .heading="${this.title}" .dismissAction="${{actionName:"cancel",action:o}}" .actions="${i}" .styles="${n}"></or-mwc-dialog>
            <slot class="notification-form-slot"></slot>
        `}};__decorate([i({type:Object})],OrRuleNotificationModal.prototype,"action",void 0),__decorate([i({type:String})],OrRuleNotificationModal.prototype,"title",void 0),__decorate([i({type:Object})],OrRuleNotificationModal.prototype,"query",void 0),__decorate([n("#notification-modal")],OrRuleNotificationModal.prototype,"_orMwcDialog",void 0),OrRuleNotificationModal=__decorate([e("or-rule-notification-modal")],OrRuleNotificationModal);export{OrRuleNotificationModal};