var __decorate=this&&this.__decorate||function(e,r,t,o){var i,a=arguments.length,n=a<3?r:null===o?o=Object.getOwnPropertyDescriptor(r,t):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,r,t,o);else for(var u=e.length-1;u>=0;u--)(i=e[u])&&(n=(a<3?i(n):a>3?i(r,t,n):i(r,t))||n);return a>3&&n&&Object.defineProperty(r,t,n),n};import{i18next as e,translate as r}from"@openremote/or-translate";import{html as t,LitElement as o}from"lit";import{customElement as i,property as a,query as n,state as u}from"lit/decorators.js";import{style as p}from"./or-rule-viewer";import{InputType as l}from"@openremote/or-mwc-components/or-mwc-input";import{OrRulesGroupNameChangeEvent as s}from"./index";import{showSnackbar as d}from"@openremote/or-mwc-components/or-mwc-snackbar";let OrRuleGroupViewer=class extends r(e)(o){constructor(){super(...arguments),this.readonly=!1}static get styles(){return[p]}willUpdate(e){var r;return e.has("group")&&(null==(r=this._groupNameInput)||r.focus()),super.willUpdate(e)}render(){return t`
            <div id="main-wrapper" class="wrapper">
                <div id="rule-header">
                    <or-mwc-input id="rule-name" outlined .type="${l.TEXT}" .label="${e.t("ruleGroupName")}" focused
                                  .value="${this.group}" ?disabled="${this.readonly}" required minlength="1" maxlength="255" @input="${this._onGroupNameInput}"
                    ></or-mwc-input>
                    <div id="rule-header-controls">
                        <or-mwc-input .type="${l.BUTTON}" id="save-btn" label="save" raised ?disabled="${this._cannotSave()}" @or-mwc-input-changed="${this._onSaveClicked}"></or-mwc-input>
                    </div>
                </div>
            </div>
        `}_onGroupNameInput(e){var r;let t=null==(r=this._groupNameInput)?void 0:r.nativeValue;t&&this._changeName(t),this.requestUpdate()}_changeName(e){e.length>=3&&e.length<=255&&(this.group=e)}_cannotSave(){var e,r,t;return this.readonly||!(null==(e=this._groupNameInput)?void 0:e.nativeValue)||!(null==(r=this._groupNameInput)?void 0:r.valid)||((null==(t=this._groupNameInput)?void 0:t.nativeValue)||"")===this._lastSaved}_onSaveClicked(){if(this.group&&!this._cannotSave()){let e=new s(this.group);this.dispatchEvent(e)?this._lastSaved=this.group:"exists"!==e.detail.reason&&e.detail.reason||d(void 0,"ruleGroupExistsError")}}};__decorate([a({type:String})],OrRuleGroupViewer.prototype,"group",void 0),__decorate([a({type:Boolean})],OrRuleGroupViewer.prototype,"readonly",void 0),__decorate([u()],OrRuleGroupViewer.prototype,"_lastSaved",void 0),__decorate([n("#rule-name")],OrRuleGroupViewer.prototype,"_groupNameInput",void 0),OrRuleGroupViewer=__decorate([i("or-rule-group-viewer")],OrRuleGroupViewer);export{OrRuleGroupViewer};