var __decorate=this&&this.__decorate||function(e,t,r,i){var l,s=arguments.length,a=s<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,r):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,i);else for(var o=e.length-1;o>=0;o--)(l=e[o])&&(a=(s<3?l(a):s>3?l(t,r,a):l(t,r))||a);return s>3&&a&&Object.defineProperty(t,r,a),a},__awaiter=this&&this.__awaiter||function(e,t,r,i){return new(r||(r=Promise))(function(l,s){function a(e){try{n(i.next(e))}catch(e){s(e)}}function o(e){try{n(i.throw(e))}catch(e){s(e)}}function n(e){var t;e.done?l(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(a,o)}n((i=i.apply(e,t||[])).next())})};import{css as e,html as t,LitElement as r}from"lit";import{customElement as i,property as l,query as s}from"lit/decorators.js";import{OrRulesRequestSaveEvent as a,OrRulesRuleChangedEvent as o,OrRulesRuleUnsupportedEvent as n,OrRulesSaveEvent as d,RuleViewInfoMap as u}from"./index";import c,{Util as h}from"@openremote/core";import"./json-viewer/or-rule-json-viewer";import"./or-rule-text-viewer";import{project as p}from"./flow-viewer/components/flow-editor";import"@openremote/or-scheduler";import{InputType as m}from"@openremote/or-mwc-components/or-mwc-input";import{i18next as v,translate as f}from"@openremote/or-translate";import{showErrorDialog as y}from"@openremote/or-mwc-components/or-mwc-dialog";let DISABLED_RRULE_PARTS=["interval","bymonth","byweekno","byyearday","bymonthday","byhour","byminute","bysecond","count"];export const style=e`

    :host {
        display: block;
        height: 100%;
        width: 100%;
        overflow-y: auto;
    }

    or-scheduler {
        margin-left: 10px;
        margin-right: 20px;
    }

    .wrapper {
        display: flex;
        flex-direction: column;
        align-items: center;
        height: 100%;
    }

    #main-wrapper.saving {
        opacity: 0.5;
        pointer-events: none;
    }
    
    #rule-name {
        max-width: 400px;
        flex: 1 1 0;
        display: flex;
    }
    
    #rule-header {
        display: flex;
        align-items: center;
        width: 100%;
        box-sizing: border-box;
        min-height: var(--internal-or-rules-header-height);
        height: var(--internal-or-rules-header-height);
        z-index: 7;
        padding: 15px 20px;
        --or-icon-fill: var(--internal-or-rules-panel-color);
    }
    
    #rule-header-controls {
        margin-left: auto;
        display: flex;
        align-items: center;
    }
    
    #rule-id {
        margin-left: 10px;
        font-size: small;
    }
    
    #active-wrapper {
        display: flex;
        align-items: center;
    }
    
    #rule-view {
        flex-grow: 1;
        background-color: var(--internal-or-rules-background-color);
    }
    
    .iconfill-gray {
        margin-left: 10px;
        --or-icon-fill: var(--internal-or-rules-list-icon-color-ok);
    }

    .iconfill-red {
        margin-left: 10px;
        --or-icon-fill: var(--internal-or-rules-list-icon-color-error);
    }
`;let OrRuleViewer=class extends f(v)(r){static get styles(){return[style]}constructor(){super(),this.readonly=!1,this.disabled=!1,this.modified=!1,this._ruleValid=!1,this._supported=!0,this._focusName=!1,this.addEventListener(o.NAME,this._onRuleChanged),this.addEventListener(n.NAME,this._onRuleUnsupported)}get valid(){return this.ruleset&&this.view&&this._ruleValid&&this.ruleset.name&&this.ruleset.name.length>=1&&this.ruleset.name.length<255}shouldUpdate(e){return e.has("ruleset")&&(this._supported=!0,this.modified=!1,this._ruleValid=!1,this.ruleset&&(this._focusName=!0,this.modified=!this.ruleset.id)),super.shouldUpdate(e)}render(){var e,r;if(!this.ruleset)return t`<div class="wrapper" style="justify-content: center"><or-translate value="noRuleSelected"></or-translate></div>`;let i=u[this.ruleset.lang].viewTemplateProvider(this.ruleset,this.config,this.readonly),l="help",s="iconfill-gray",a="NOSTATUS";switch(this.ruleset.status&&(a=this.ruleset.status),this.ruleset.status){case"DEPLOYED":l="play",s="iconfill-gray";break;case"READY":l="check",s="iconfill-gray";break;case"COMPILATION_ERROR":case"LOOP_ERROR":case"VALIDITY_PERIOD_ERROR":case"EXECUTION_ERROR":l="alert-octagon",s="iconfill-red",a=this.ruleset.error;break;case"DISABLED":l="minus-circle",s="iconfill-gray";break;case"PAUSED":l="calendar-arrow-right",s="iconfill-gray";break;case"EXPIRED":l="calendar-remove",s="iconfill-gray";break;case"REMOVED":l="close",s="iconfill-gray";break;default:l="stop",s="iconfill-gray",a="NOSTATUS"}return t`
            <div id="main-wrapper" class="wrapper">
                <div id="rule-header">
                    <or-mwc-input id="rule-name" outlined .type="${m.TEXT}" .label="${v.t("ruleName")}" ?focused="${this._focusName}" .value="${this.ruleset?this.ruleset.name:null}" ?disabled="${this._isReadonly()}" required minlength="1" maxlength="255" @or-mwc-input-changed="${e=>this._changeName(e.detail.value)}"></or-mwc-input>
                    <or-icon class="${s}" title="${v.t("rulesetStatus."+a)}" icon="${l}"></or-icon>
                    <span id="rule-id">${this.ruleset.id?"ID: "+this.ruleset.id:""}</span>
                    <div id="rule-header-controls">
                        <span id="active-wrapper">
                            <or-translate value="enabled"></or-translate>
                            <or-mwc-input .type="${m.CHECKBOX}" .value="${this.ruleset&&this.ruleset.enabled}" ?disabled="${!this.ruleset.id}" @or-mwc-input-changed="${this._toggleEnabled}"></or-mwc-input>
                        </span>
                        <or-scheduler
                            id="rule-header-validity"
                            header="scheduleRuleActivity"
                            defaultEventTypeLabel="validityAlways"
                            .disabledRRuleParts="${DISABLED_RRULE_PARTS}"
                            .schedule="${null==(r=null==(e=this.ruleset)?void 0:e.meta)?void 0:r.validity}"
                            @or-scheduler-changed="${this._onSchedulerChanged}"
                        ></or-scheduler>
                        <or-mwc-input .type="${m.BUTTON}" id="save-btn" label="save" raised ?disabled="${this._cannotSave()}" @or-mwc-input-changed="${this._onSaveClicked}"></or-mwc-input>
                    </div>
                </div>

                ${i}
            </div>
        `}updated(e){(e.has("ruleset")||e.has("modified"))&&this.ruleset&&this.view&&(this._ruleValid=this.view.validate())}_isReadonly(){return this.readonly||!c.hasRole("write:rules")}_cannotSave(){return this._isReadonly()||!this.ruleset||!this.modified||!this.valid}_changeName(e){this.ruleset&&this.ruleset.name!==e&&(this.ruleset.name=e,this.modified=!0,this.requestUpdate())}_onSchedulerChanged(e){var t;this.ruleset&&(null!=(t=this.ruleset).meta||(t.meta={}),this.ruleset.meta.validity=null==e?void 0:e.detail.value,this.requestUpdate("ruleset"))}_onRuleChanged(e){this.modified=!0,this._ruleValid=e.detail}_onSaveClicked(){this.ruleset&&this.view&&(p.emit("fitview"),(!this.config||!this.config.rulesetSaveHandler||this.config.rulesetSaveHandler(this.ruleset))&&h.dispatchCancellableEvent(this,new a(this.ruleset)).then(e=>{e.allow&&this._doSave()}))}_doSave(){return __awaiter(this,void 0,void 0,function*(){let e,t=this.ruleset;if(!t||!this.view)return;let r=!1,i=!t.id;this.saveBtnElem.disabled=!0,this.wrapperElem.classList.add("saving"),this.view.beforeSave();try{switch(t.type){case"asset":e=i?yield c.rest.api.RulesResource.createAssetRuleset(t):yield c.rest.api.RulesResource.updateAssetRuleset(t.id,t);break;case"realm":e=i?yield c.rest.api.RulesResource.createRealmRuleset(t):yield c.rest.api.RulesResource.updateRealmRuleset(t.id,t);break;case"global":e=i?yield c.rest.api.RulesResource.createGlobalRuleset(t):yield c.rest.api.RulesResource.updateGlobalRuleset(t.id,t)}if(e.status!==(i?200:204)){r=!0,y("Create ruleset returned unexpected status: "+e.status);return}e.data&&(t.id=e.data)}catch(e){r=!0,console.error("Failed to save ruleset",e)}this.wrapperElem.classList.remove("saving"),this.saveBtnElem.disabled=!1,r&&y(v.t("saveRulesetFailed")),this.dispatchEvent(new d({ruleset:t,isNew:i,success:!r}))})}_onRuleUnsupported(){this._supported=!1}_toggleEnabled(){this.ruleset&&(this.ruleset.enabled=!this.ruleset.enabled,this.modified=!0,this.requestUpdate())}};__decorate([l({type:Object})],OrRuleViewer.prototype,"ruleset",void 0),__decorate([l({type:Boolean})],OrRuleViewer.prototype,"readonly",void 0),__decorate([l({type:Boolean})],OrRuleViewer.prototype,"disabled",void 0),__decorate([l({type:Object})],OrRuleViewer.prototype,"config",void 0),__decorate([l({attribute:!1})],OrRuleViewer.prototype,"modified",void 0),__decorate([l({attribute:!1})],OrRuleViewer.prototype,"_ruleValid",void 0),__decorate([l({attribute:!1})],OrRuleViewer.prototype,"_supported",void 0),__decorate([s("#rule-view")],OrRuleViewer.prototype,"view",void 0),__decorate([s("#main-wrapper")],OrRuleViewer.prototype,"wrapperElem",void 0),__decorate([s("#save-btn")],OrRuleViewer.prototype,"saveBtnElem",void 0),OrRuleViewer=__decorate([i("or-rule-viewer")],OrRuleViewer);export{OrRuleViewer};