var __decorate=this&&this.__decorate||function(e,t,a,r){var i,n=arguments.length,o=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,a):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(e,t,a,r);else for(var s=e.length-1;s>=0;s--)(i=e[s])&&(o=(n<3?i(o):n>3?i(t,a,o):i(t,a))||o);return n>3&&o&&Object.defineProperty(t,a,o),o},__awaiter=this&&this.__awaiter||function(e,t,a,r){return new(a||(a=Promise))(function(i,n){function o(e){try{l(r.next(e))}catch(e){n(e)}}function s(e){try{l(r.throw(e))}catch(e){n(e)}}function l(e){var t;e.done?i(e.value):((t=e.value)instanceof a?t:new a(function(e){e(t)})).then(o,s)}l((r=r.apply(e,t||[])).next())})};import{i18next as e,translate as t}from"@openremote/or-translate";import{LitElement as a,css as r,html as i}from"lit";import{customElement as n,property as o,state as s}from"lit/decorators.js";import{InputType as l}from"@openremote/or-mwc-components/or-mwc-input";import{showSnackbar as g}from"@openremote/or-mwc-components/or-mwc-snackbar";import{OrRulesJsonRuleChangedEvent as u}from"../or-rule-json-viewer";import{when as c}from"lit/directives/when.js";import{until as d}from"lit/directives/until.js";import{guard as m}from"lit/directives/guard.js";import"./or-rule-form-email-message";import"./or-rule-form-push-notification";import h from"iso-639-1";import{DefaultColor6 as p}from"@openremote/core";let OrRuleFormLocalized=class extends t(e)(a){constructor(){super(...arguments),this.type="push",this.languages=["en"],this.defaultLang="en",this.wrongLanguage=!1,this._selectedLanguage="en"}static get styles(){return r`
            or-mwc-input {
                margin-bottom: 20px;
                min-width: 420px;
                width: 100%;
            }

            .divider {
                margin-bottom: 20px;
                border-top: 1px solid rgba(0, 0, 0, 12%);
            }
        `}connectedCallback(){super.connectedCallback(),this.defaultLang&&this.languages.includes(this.defaultLang)||(this.defaultLang=this.languages[0]),this._selectedLanguage=this.defaultLang}render(){var e;return i`
            <div>
                ${c(this.wrongLanguage,()=>d(this._getWrongLanguageTemplate()))}
                ${m([this.message,this._selectedLanguage,this.languages,this.type],()=>i`
                    ${d(this._getLanguageSelectForm(this._selectedLanguage,this.languages),i`Loading...`)}
                    ${d(this._getNotificationForm(this.message,this._selectedLanguage),i`Loading...`)}
                `)}
                ${c((null==(e=this.languages)?void 0:e.length)&&this._validLanguages&&this._validLanguages.length<this.languages.length,()=>d(this._getLanguageErrorTemplate(this.languages,this._validLanguages)))}
            </div>
        `}_getWrongLanguageTemplate(){return __awaiter(this,void 0,void 0,function*(){return i`
            <or-mwc-input .type="${l.BUTTON}" fullWidth outlined
                          label="defaultLanguageChangedError" style="margin-top: 10px;"
                          @or-mwc-input-changed="${this._fixDefaultLanguage}"
            ></or-mwc-input>
        `})}_fixDefaultLanguage(){this.message?(console.debug("Updating default language from "+this.message.defaultLanguage+" to "+this.defaultLang),this.message.defaultLanguage=this.defaultLang,this.wrongLanguage=!1,this.dispatchEvent(new u)):(console.warn("Could not fix language, because the message could not be found."),g(void 0,"Could not fix language, because the message could not be found."))}_getLanguageSelectForm(e){return __awaiter(this,arguments,void 0,function*(e,t=[this._selectedLanguage],a=!0){let r=t.map(e=>[e,h.getName(e)]);return i`
            <div style="display: flex; justify-content: space-between;">
                <or-mwc-input .type="${l.SELECT}" .options="${r}" .value="${e}"
                              @or-mwc-input-changed="${this._onLanguageChange}"
                ></or-mwc-input>
            </div>
            ${c(a,()=>i`
                <div class="divider"></div>`)}
        `})}_onLanguageChange(e){this._selectedLanguage=e.detail.value}_getNotificationForm(){return __awaiter(this,arguments,void 0,function*(e=this.message,t=this._selectedLanguage){if(!(null==e?void 0:e.languages))return i`<or-translate .value="${"errorOccurred"}"></or-translate>`;e.languages[t]||(e.languages[t]={type:this.type});let a=e.languages[t];return"push"===a.type?i`
                <or-rule-form-push-notification .message="${a}"></or-rule-form-push-notification>
            `:"email"===a.type?i`
                <or-rule-form-email-message .message="${a}"></or-rule-form-email-message>
            `:i`
                <or-translate .value="${"errorOccurred"}"></or-translate>
            `})}_getLanguageErrorTemplate(e,t){return __awaiter(this,void 0,void 0,function*(){let a=e.filter(e=>!t.includes(e)).map(e=>h.getName(e)).join(", ");return i`
            <div style="margin-top: 10px; display: flex; justify-content: end; color: ${p};">
                <or-translate value="languagesInvalidError"></or-translate>
                <span style="padding-left: 4px;">${a}</span>
            </div>
        `})}isValid(){var e,t,a;if((null==(e=this.message)?void 0:e.languages)&&(null==(t=this.languages)?void 0:t.length)){this._cleanNotificationMessage();let e=null==(a=this.languages)?void 0:a.filter(e=>{var t,a;if(!(null==(a=null==(t=this.message)?void 0:t.languages)?void 0:a[e]))return!0;let r=this.message.languages[e];switch(r.type){case"email":return r.subject&&r.html;case"push":return r.title&&r.body;case"localized":return!1;default:return!0}});return JSON.stringify(this._validLanguages)!==JSON.stringify(e)&&(this._validLanguages=e),e.length===this.languages.length}return!1}_cleanNotificationMessage(){var e;if(null==(e=this.message)?void 0:e.languages){let e=Object.entries(this.message.languages).filter(([e,t])=>0!==Object.entries(t).filter(e=>{if("type"===e[0]||null==e[1]||"string"==typeof e[1]&&0===e[1].length)return!1;if(Array.isArray(e[1])){if(!e[1].some(e=>Object.keys(e).length>0&&Object.values(e).some(e=>e)))return!1}else if("object"==typeof e[1]&&(0===Object.keys(e[1]).length||0===Object.values(e[1]).filter(e=>e).length))return!1;return!0}).length||(console.debug(`Removing fields of notification language '${e}', as they were all empty.`),!1));this.message.languages=Object.fromEntries(e)}}};__decorate([o({type:Object})],OrRuleFormLocalized.prototype,"message",void 0),__decorate([o({type:String})],OrRuleFormLocalized.prototype,"type",void 0),__decorate([o()],OrRuleFormLocalized.prototype,"languages",void 0),__decorate([o()],OrRuleFormLocalized.prototype,"defaultLang",void 0),__decorate([o()],OrRuleFormLocalized.prototype,"wrongLanguage",void 0),__decorate([s()],OrRuleFormLocalized.prototype,"_selectedLanguage",void 0),__decorate([s()],OrRuleFormLocalized.prototype,"_validLanguages",void 0),OrRuleFormLocalized=__decorate([n("or-rule-form-localized")],OrRuleFormLocalized);export{OrRuleFormLocalized};