var __decorate=this&&this.__decorate||function(t,e,r,o){var i,s=arguments.length,n=s<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(t,e,r,o);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(n=(s<3?i(n):s>3?i(e,r,n):i(e,r))||n);return s>3&&n&&Object.defineProperty(e,r,n),n},__awaiter=this&&this.__awaiter||function(t,e,r,o){return new(r||(r=Promise))(function(i,s){function n(t){try{l(o.next(t))}catch(t){s(t)}}function a(t){try{l(o.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?i(t.value):((e=t.value)instanceof r?e:new r(function(t){t(e)})).then(n,a)}l((o=o.apply(t,e||[])).next())})};import{LitElement as t,css as e,html as r}from"lit";import{customElement as o,property as i}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 r=e.data,o=c.jsonToFlow(r.rules);d.fromNodeCollection(o),d.setCurrentProject(t.id,t.name,o.description),this.dispatchEvent(new CustomEvent("ruleloaded"))}),this.getButton=t=>r`<div class="list-button" @click="${()=>{this.loadRule(t)}}">${l.ellipsis(t.name,50)} 
        ${t.error?r`<or-icon title="${l.humanLike(t.status)}" icon="alert-outline"></or-icon>`:null}
        ${t.enabled?null:r`<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=r``;switch(this.status){case u.Loading:t=r`<span style="text-align: center;"><or-icon icon="loading"></or-icon></span>`;break;case u.Success:t=r`${0===this.retrievedRules.length?r`<span>No rules to display</span>`:this.retrievedRules.map(t=>this.getButton(t))}`;break;case u.Failure:t=r`<span>Failed to load rules</span>`}return r`
        <div style="display: flex; flex-direction: column; width: auto; align-items: stretch;">
        ${t}
        </div>`}};__decorate([i({type:Number})],RuleBrowser.prototype,"status",void 0),RuleBrowser=__decorate([o("rule-browser")],RuleBrowser);export{RuleBrowser};