var __decorate=this&&this.__decorate||function(t,e,o,i){var r,s=arguments.length,n=s<3?e:null===i?i=Object.getOwnPropertyDescriptor(e,o):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(t,e,o,i);else for(var a=t.length-1;a>=0;a--)(r=t[a])&&(n=(s<3?r(n):s>3?r(e,o,n):r(e,o))||n);return s>3&&n&&Object.defineProperty(e,o,n),n},__awaiter=this&&this.__awaiter||function(t,e,o,i){return new(o||(o=Promise))(function(r,s){function n(t){try{l(i.next(t))}catch(t){s(t)}}function a(t){try{l(i.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?r(t.value):((e=t.value)instanceof o?e:new o(function(t){t(e)})).then(n,a)}l((i=i.apply(t,e||[])).next())})};import{LitElement as t,css as e,html as o}from"lit";import{customElement as i,property as r}from"lit/decorators.js";import s from"@openremote/rest";import{i18next as n,translate as a}from"@openremote/or-translate";import{Utilities as l}from"../utils";import{Status as u}from"../models/status";import{exporter as c,project as d,modal as p}from"./flow-editor";let RuleBrowser=class extends a(n)(t){constructor(){super(...arguments),this.status=u.Idle,this.retrievedRules=[],this.loadRule=t=>__awaiter(this,void 0,void 0,function*(){let e;this.status=u.Loading;try{e=yield s.api.RulesResource.getGlobalRuleset(t.id)}catch(e){p.notification("Failure","Something went wrong loading "+t.name),this.status=u.Failure;return}let o=e.data,i=c.jsonToFlow(o.rules);d.fromNodeCollection(i),d.setCurrentProject(t.id,t.name,i.description),this.dispatchEvent(new CustomEvent("ruleloaded"))}),this.getButton=t=>o`<div class="list-button" @click="${()=>{this.loadRule(t)}}">${l.ellipsis(t.name,50)} 
        ${t.error?o`<or-icon title="${l.humanLike(t.status)}" icon="alert-outline"></or-icon>`:null}
        ${t.enabled?null:o`<or-icon title="${n.t("disabled")}" icon="sleep"></or-icon>`}
        </div>`}static get styles(){return e`
        .list-button {
            cursor: pointer;
            padding: 8px 0 8px 8px;
        }
        .list-button:hover {
            background: whitesmoke;
        }
        .list-button:active {
            background: none;
        }
        or-icon{
            width: 18px;
            vertical-align: text-top;
        }
        or-icon[icon=loading]{
            animation: spin 600ms infinite linear;
        }
        @keyframes spin{
            0%{
                transform: rotateZ(0deg);
            }
            100%{
                transform: rotateZ(360deg);
            }
        }`}firstUpdated(){return __awaiter(this,void 0,void 0,function*(){this.status=u.Loading;try{let t=yield s.api.RulesResource.getGlobalRulesets();this.retrievedRules=t.data,this.status=u.Success}catch(t){this.status=u.Failure}})}render(){let t=o``;switch(this.status){case u.Loading:t=o`<span style="text-align: center;"><or-icon icon="loading"></or-icon></span>`;break;case u.Success:t=o`${0===this.retrievedRules.length?o`<span>No rules to display</span>`:this.retrievedRules.map(t=>this.getButton(t))}`;break;case u.Failure:t=o`<span>Failed to load rules</span>`}return o`
        <div style="display: flex; flex-direction: column; width: auto; align-items: stretch;">
        ${t}
        </div>`}};__decorate([r({type:Number})],RuleBrowser.prototype,"status",void 0),RuleBrowser=__decorate([i("rule-browser")],RuleBrowser);export{RuleBrowser};