var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { css, html, unsafeCSS } from "lit";
import { customElement, property, query, state } from "lit/decorators.js";
import { Page, router } from "@openremote/or-app";
import manager, { DefaultColor3, DefaultColor4 } from "@openremote/core";
import { i18next } from "@openremote/or-translate";
import { showSnackbar } from "@openremote/or-mwc-components/or-mwc-snackbar";
import { isAxiosError } from "@openremote/rest";
import { getAlarmsRoute } from "../routes";
import { when } from "lit/directives/when.js";
import { until } from "lit/directives/until.js";
import { guard } from "lit/directives/guard.js";
import { InputType, OrInputChangedEvent, OrMwcInput } from "@openremote/or-mwc-components/or-mwc-input";
import { OrMwcDialog, showDialog, showOkCancelDialog } from "@openremote/or-mwc-components/or-mwc-dialog";
import "../components/alarms/or-alarms-table";
export function pageAlarmsProvider(store, config) {
    return {
        name: "alarms",
        routes: ["alarms", "alarms/:id"],
        pageCreator: () => {
            return new PageAlarms(store);
        }
    };
}
let PageAlarms = class PageAlarms extends Page {
    static get styles() {
        // language=CSS
        return [
            css `
                :host {
                    flex: 1;
                    width: 100%;
                }

                #wrapper {
                    height: 100%;
                    width: 100%;
                    display: flex;
                    flex-direction: column;
                    overflow: auto;
                }

                #title {
                    padding: 0 20px;
                    font-size: 18px;
                    font-weight: bold;
                    margin: 10px 0;
                    display: flex;
                    align-items: center;
                    color: var(--or-app-color3, ${unsafeCSS(DefaultColor3)});
                    justify-content: space-between;
                }

                #title-alarm {
                    display: flex;
                    align-items: center;
                    justify-content: flex-start;
                    width: calc(100% - 52px);
                    max-width: 1000px;
                    margin: 20px auto;
                    font-size: 18px;
                    font-weight: bold;
                }

                .breadcrumb-container {
                    width: calc(100% - 52px);
                    max-width: 1000px;
                    margin: 10px 20px 0 34px;
                    display: flex;
                    align-items: center;
                }

                .breadcrumb-clickable {
                    cursor: pointer;
                    color: var(--or-app-color4, ${unsafeCSS(DefaultColor4)});
                }

                .breadcrumb-arrow {
                    margin: 0 5px -3px 5px;
                    --or-icon-width: 16px;
                    --or-icon-height: 16px;
                }

                .panel {
                    width: calc(100% - 100px);
                    max-width: 1000px;
                    background-color: white;
                    border: 1px solid #e5e5e5;
                    border-radius: 5px;
                    position: relative;
                    margin: auto;
                    padding: 12px 24px 24px;
                }

                .panel-title {
                    text-transform: uppercase;
                    font-weight: bolder;
                    color: var(--or-app-color3, ${unsafeCSS(DefaultColor3)});
                    line-height: 1em;
                    /*margin-bottom: 10px;*/
                    margin-top: 0;
                    flex: 0 0 auto;
                    letter-spacing: 0.025em;
                    display: flex;
                    align-items: center;
                    min-height: 36px;
                }

                .alarm-input {
                    margin-bottom: 12px;
                }

                #status-select, #severity-select {
                    width: 180px;
                }

                #controls {
                    flex: 0;
                    display: flex;
                    flex-direction: row;
                    justify-content: space-between;
                    align-items: center;
                }

                .controls-left {
                    display: flex;
                    align-items: center;
                    width: 100%;
                }

                .controls-left > * {
                    padding-right: 20px;
                }

                h5 {
                    margin-top: 12px;
                    margin-bottom: 6px;
                }

                or-icon {
                    vertical-align: middle;
                    --or-icon-width: 20px;
                    --or-icon-height: 20px;
                }

                #table-container {
                    margin-left: 20px;
                    margin-right: 20px;
                }

                .row {
                    display: flex;
                    flex-direction: row;
                    flex-wrap: wrap;
                    margin: auto;
                    flex: 1 1 0;
                    gap: 16px;
                    width: 100%;
                }

                .column {
                    display: flex;
                    flex-direction: column;
                    margin: 0px;
                    flex: 1 1 0;
                }

                #details-panel {
                    min-width: 66%;
                    height: max-content;
                }

                #prop-panel {
                    min-width: 32%;
                    height: fit-content;
                }

                .hidden {
                    display: none;
                    margin: 0 !important;
                }

                #title.hidden {
                    display: none;
                    margin: 0 !important;
                }
            `
        ];
    }
    get statusValue() {
        const statusOptions = this._getStatusOptions();
        const matchingOption = statusOptions.find(option => option.value === this.status ||
            (this.allActive && option.value === 'All-active') ||
            (!this.status && option.value === 'All'));
        return matchingOption ? matchingOption.label : '';
    }
    get severityValue() {
        const severityOptions = this._getSeverityOptions();
        const matchingOption = severityOptions.find(option => option.value === this.severity ||
            (!this.severity && option.value === 'All'));
        return matchingOption ? matchingOption.label : '';
    }
    get name() {
        return "alarm.alarm_plural";
    }
    constructor(store) {
        super(store);
        this._alarms = [];
        this._linkedAssets = [];
        this._linkedUsers = [];
        this._loadedUsers = [];
        this._loading = false;
        this.assign = false;
        this.allActive = true;
    }
    connectedCallback() {
        super.connectedCallback();
        this.realm = this.getState().app.realm;
        this._loadData();
    }
    shouldUpdate(changedProperties) {
        if (changedProperties.has("realm") && changedProperties.get("realm") != undefined) {
            this.reset();
        }
        if (changedProperties.has("alarm")) {
            this._updateRoute();
        }
        if (changedProperties.has("severity")
            || changedProperties.has("status")
            || changedProperties.has("allActive")) {
            this._data = undefined;
        }
        if (!this._data) {
            this._loadData();
        }
        return super.shouldUpdate(changedProperties);
    }
    responseAndStateOK(stateChecker, response, errorMsg) {
        if (!stateChecker()) {
            return false;
        }
        if (!response.data) {
            showSnackbar(undefined, errorMsg, i18next.t("dismiss"));
            console.error(errorMsg + ": response = " + response.statusText);
            return false;
        }
        return true;
    }
    _createUpdateAlarm(alarm, action) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!alarm.title || alarm.title === "") {
                return;
            }
            const isUpdate = !!alarm.id;
            let assetsIds;
            if (!isUpdate) {
                alarm.realm = manager.displayRealm;
                assetsIds = alarm.alarmAssetLinks.map(l => l.id.assetId);
            }
            try {
                action == "update"
                    ? yield manager.rest.api.AlarmResource.updateAlarm(alarm.id, alarm)
                    : yield manager.rest.api.AlarmResource.createAlarm(alarm, { assetIds: assetsIds }).then((response) => __awaiter(this, void 0, void 0, function* () {
                        if (alarm.alarmAssetLinks.length > 0) {
                            alarm.alarmAssetLinks.forEach((link) => {
                                link.id.sentalarmId = response.data.id;
                                link.id.realm = response.data.realm;
                            });
                            yield manager.rest.api.AlarmResource.setAssetLinks(alarm.alarmAssetLinks);
                        }
                    }));
            }
            catch (e) {
                if (isAxiosError(e)) {
                    console.error((isUpdate ? "alarm.saveAlarmFailed" : "alarm.createAlarmFailed") + ": response = " + e.response.statusText);
                    if (e.response.status === 400) {
                        showSnackbar(undefined, i18next.t(isUpdate ? "alarm.saveAlarmFailed" : "alarm.createAlarmFailed"), i18next.t("dismiss"));
                    }
                    else if (e.response.status === 403) {
                        showSnackbar(undefined, i18next.t("alarm.alarmAlreadyExists"));
                    }
                }
                throw e; // Throw exception anyhow to handle individual cases
            }
            finally {
                this.reset();
                yield this._loadData();
            }
        });
    }
    render() {
        var _a, _b;
        if (!manager.authenticated) {
            return html `
                <or-translate value="notAuthenticated"/>`;
        }
        const disabled = false;
        const readAlarms = manager.hasRole("read:alarms");
        const writeAlarms = manager.hasRole("write:alarms");
        const readonly = readAlarms && !writeAlarms;
        const assignOnly = !readAlarms && !writeAlarms;
        this.config = { assignOnly: assignOnly };
        return html `
            <div id="wrapper">
                <!-- Alarm Specific page -->
                <div id="title" class="${this.creationState || this.alarm ? "hidden" : ""}">
                    <div class="${this.creationState || this.alarm ? "hidden" : ""}"
                         style="display: flex; align-items: center;">
                        <or-icon icon="bell-outline" style="padding: 0 10px 0 4px;"></or-icon>
                        <span>${this.alarm != undefined ? this.alarm.title : html `
                            <or-translate value="alarm.alarm_plural"/>`}</span>
                    </div>
                    <div>
                        <div id="controls">
                            <div class="${this.creationState || this.alarm || assignOnly || readonly ? "hidden" : "panel-title"}"
                                 style="justify-content: flex-end;">
                                <or-mwc-input
                                        outlined
                                        class="${((_a = this._selectedIds) === null || _a === void 0 ? void 0 : _a.length) > 0 ? "" : "hidden"}"
                                        style="margin: 0;"
                                        type="${InputType.BUTTON}"
                                        icon="delete"
                                        @or-mwc-input-changed="${() => (this._deleteAlarms())}"
                                ></or-mwc-input>
                            </div>
                            <div class="${((_b = this._selectedIds) === null || _b === void 0 ? void 0 : _b.length) > 0 ? "" : "hidden"}"
                                 style="height:40px; border-right: 1px solid #e0e0e0; margin-right: 4px;"></div>
                            <div class="${this.creationState || this.alarm ? "hidden" : "controls-left"}">
                                <or-mwc-input .type="${InputType.CHECKBOX}" id="assign-check"
                                              ?disabled="${disabled}" .label="${i18next.t("alarm.assignedToMe")}"
                                              @or-mwc-input-changed="${(evt) => this._onAssignCheckChanged(evt.detail.value)}"
                                              .value="${this.assign}"></or-mwc-input>
                                <or-mwc-input .type="${InputType.SELECT}" id="severity-select" comfortable
                                              ?disabled="${disabled}" .label="${i18next.t("alarm.severity")}"
                                              @or-mwc-input-changed="${(evt) => this._onSeverityChanged(evt.detail.value)}"
                                              .value="${this.severityValue}"
                                              .options="${this._getSeverityOptions().map(s => s.label)}"></or-mwc-input>
                                <or-mwc-input .type="${InputType.SELECT}" id="status-select" comfortable
                                              ?disabled="${disabled}" .label="${i18next.t("alarm.status")}"
                                              @or-mwc-input-changed="${(evt) => this._onStatusChanged(evt.detail.value)}"
                                              .value="${this.statusValue}"
                                              .options="${this._getStatusOptions().map(s => s.label)}"></or-mwc-input>

                            </div>
                            <div class="${this.creationState || this.alarm || assignOnly || readonly ? "hidden" : "panel-title"}"
                                 style="justify-content: flex-end;">
                                <or-mwc-input
                                        type="${InputType.BUTTON}"
                                        icon="plus"
                                        label="${i18next.t("alarm.add")}"
                                        @or-mwc-input-changed="${() => (this.creationState = { alarmModel: this.getNewAlarmModel() })}"
                                ></or-mwc-input>
                            </div>
                        </div>
                    </div>
                </div>
                ${when(this.alarm || this.creationState, () => {
            var _a;
            const alarm = (_a = this.alarm) !== null && _a !== void 0 ? _a : this.creationState.alarmModel;
            return this.getSingleAlarmView(alarm, readonly);
        }, () => {
            return html `
                        <!-- List of Alarms page -->
                        <div id="container">
                            ${disabled ? html `
                                <div id="msg">
                                    <or-translate value="loading"/>
                                </div>
                            ` : html `
                                <div id="table-container">
                                    ${when(this._data, () => guard([this._data], () => this.getAlarmsTable(writeAlarms)))}
                                </div>
                            `}
                        </div>
                    `;
        })}
            </div>
        `;
    }
    getNewAlarmModel() {
        return {
            alarmAssetLinks: [],
            previousAssetLinks: [],
            loaded: true,
            source: "MANUAL" /* AlarmSource.MANUAL */,
            severity: "MEDIUM" /* AlarmSeverity.MEDIUM */,
            status: "OPEN" /* AlarmStatus.OPEN */,
            realm: manager.displayRealm
        };
    }
    loadAlarm(alarm) {
        return __awaiter(this, void 0, void 0, function* () {
            if (alarm.loaded) {
                return;
            }
            const stateChecker = () => {
                return this.getState().app.realm === this.realm && this.isConnected;
            };
            const alarmAssetLinksResponse = yield manager.rest.api.AlarmResource.getAssetLinks(alarm.id, { realm: alarm.realm });
            if (!this.responseAndStateOK(stateChecker, alarmAssetLinksResponse, i18next.t("loadFailedUsers"))) {
                return;
            }
            if (manager.hasRole("read:users") || manager.hasRole("read:admin")) {
                const usersResponse = yield manager.rest.api.UserResource.query({
                    realmPredicate: { name: manager.displayRealm },
                });
                if (!this.responseAndStateOK(stateChecker, usersResponse, i18next.t("loadFailedUsers"))) {
                    return;
                }
                this._loadedUsers = usersResponse.data.filter((user) => user.enabled && !user.serviceAccount);
            }
            this.alarm.alarmAssetLinks = alarmAssetLinksResponse.data;
            this.alarm.loaded = true;
            this.alarm.loading = false;
            // Update the dom
            this.requestUpdate();
        });
    }
    getAlarmsTable(writeAlarms) {
        return html `
            <or-alarms-table .alarms=${this._data} .readonly=${!writeAlarms}
                             @or-mwc-table-row-select="${(e) => this._onRowSelect(e)}"
                             @or-mwc-table-row-click="${(e) => this._onRowClick(e)}"
            ></or-alarms-table>
        `;
    }
    _loadData() {
        return __awaiter(this, void 0, void 0, function* () {
            var _a;
            if (this._loading || (!manager.hasRole("read:alarms") && !manager.hasRole("write:alarms"))) {
                return;
            }
            this._loading = true;
            const response = yield manager.rest.api.AlarmResource.getAlarms({ realm: manager.displayRealm });
            if (manager.hasRole("read:users") || manager.hasRole("read:admin")) {
                const usersResponse = yield manager.rest.api.UserResource.query({
                    realmPredicate: { name: manager.displayRealm },
                });
                if (usersResponse.status === 200) {
                    this._loadedUsers = usersResponse.data.filter((user) => user.enabled && !user.serviceAccount);
                }
            }
            if (response.status === 200) {
                // Get page count
                this._data = response.data;
                this._data = this._data.filter((e) => e.realm === manager.displayRealm);
                this._selectedIds = [];
                if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.assignOnly) {
                    const userResponse = yield manager.rest.api.UserResource.getCurrent();
                    if (userResponse.status === 200) {
                        this._data = this._data.filter((e) => e.assigneeId === userResponse.data.id);
                    }
                }
                if (this.severity) {
                    this._data = this._data.filter((e) => e.severity === this.severity);
                }
                if (this.status) {
                    this._data = this._data.filter((e) => e.status === this.status);
                }
                if (!this.status && this.allActive) {
                    this._data = this._data.filter((e) => e.status !== "RESOLVED" /* AlarmStatus.RESOLVED */ && e.status !== "CLOSED" /* AlarmStatus.CLOSED */);
                }
                if (this.assign) {
                    yield this._onAssignCheckChanged(this.assign);
                }
            }
            this._loading = false;
        });
    }
    _deleteAlarm(alarm) {
        showOkCancelDialog(i18next.t("alarm.deleteAlarm"), i18next.t("alarm.deleteAlarmConfirm", { alarm: alarm.title }), i18next.t("delete"))
            .then((ok) => {
            if (ok) {
                this.doDelete(alarm.id);
            }
        });
    }
    _deleteAlarms() {
        showOkCancelDialog(i18next.t("alarm.deleteAlarms"), i18next.t("alarm.deleteAlarmsConfirm", { count: this._selectedIds.length }), i18next.t("delete"))
            .then((ok) => {
            if (ok) {
                this.doMultipleDelete(this._selectedIds);
            }
        });
    }
    doDelete(alarmId) {
        manager.rest.api.AlarmResource.removeAlarm(alarmId).then(response => {
            this.reset();
        });
    }
    doMultipleDelete(alarmIds) {
        manager.rest.api.AlarmResource.removeAlarms(alarmIds).then(response => {
            this.reset();
        });
    }
    getSingleAlarmView(alarm, readonly = true) {
        return html `
            ${when(alarm.loaded, () => {
            return this.getSingleAlarmTemplate(alarm, readonly);
        }, () => {
            const getTemplate = () => __awaiter(this, void 0, void 0, function* () {
                yield this.loadAlarm(alarm);
                return this.getSingleAlarmTemplate(alarm, readonly);
            });
            const content = getTemplate();
            return html `${until(content, html `
                            <or-translate value="loading"/>`)} `;
        })}
        `;
    }
    getSingleAlarmTemplate(alarm, readonly = true) {
        var _a;
        const write = manager.hasRole("write:alarms");
        return html `
            <!-- Breadcrumb on top of the page-->
            <div style="margin: 0 auto auto auto;
                        width: calc(100% - 100px);
                        max-width: 1000px;">
                <div class="breadcrumb-container">
                    <span class="breadcrumb-clickable" @click="${() => this.reset()}"
                    ><or-translate value="alarm.alarm_plural"/></span>
                    <or-icon class="breadcrumb-arrow" icon="chevron-right"></or-icon>
                    <span style="margin-left: 2px;"
                    >${this.alarm != undefined ? this.alarm.id : html `
                        <or-translate value="alarm.creatingAlarm"/>`}</span>
                </div>
                <div id="title-alarm">
                    <or-icon icon="bell-outline" style="margin-right: 10px; margin-left: 14px;"></or-icon>
                    <span>${this.alarm != undefined ? this.alarm.title : html `
                        <or-translate value="alarm.alarm_plural"/>`}</span>
                </div>

                <div class="panel" style="margin-top: 0">
                    <div class="row">
                        <div class="column" id="details-panel">
                            <h5>
                                <or-translate value="details"/>
                            </h5>
                            <!-- alarm details -->
                            <or-mwc-input class="alarm-input" ?disabled="${!write}"
                                          .label="${i18next.t("alarm.title")}" required
                                          .type="${InputType.TEXT}"
                                          .value="${alarm.title}"
                                          @or-mwc-input-changed="${(e) => {
            alarm.title = e.detail.value;
            this.onAlarmChanged(e);
        }}"
                            ></or-mwc-input>
                            <or-mwc-input class="alarm-input" ?disabled="${!write}"
                                          .label="${i18next.t("alarm.content")}"
                                          .type="${InputType.TEXTAREA}"
                                          .value="${alarm.content}"
                                          rows="12"
                                          @or-mwc-input-changed="${(e) => {
            alarm.content = e.detail.value;
            this.onAlarmChanged(e);
        }}"
                            ></or-mwc-input>
                        </div>
                        <div class="column" id="prop-panel">
                            <h5>
                                <or-translate value="properties"/>
                            </h5>
                            <or-mwc-input class="alarm-input hidden" ?disabled="${true}"
                                          ?comfortable=${!this.creationState}
                                          .label="${i18next.t("createdOn")}"
                                          .type="${InputType.DATETIME}"
                                          .value="${new Date(alarm.createdOn)}"
                            ></or-mwc-input>
                            <or-mwc-input class="${this.creationState ? "hidden" : "alarm-input"}" ?disabled="${true}"
                                          .label="${i18next.t("alarm.lastModified")}"
                                          .type="${InputType.DATETIME}"
                                          .value="${new Date(alarm.lastModified)}"
                            ></or-mwc-input>
                            <or-mwc-input class="alarm-input" ?disabled="${!write || this.creationState || this.alarm}"
                                          .label="${i18next.t("alarm.source")}"
                                          .type="${InputType.TEXT}"
                                          .value="${this._getSourceText()}"
                            ></or-mwc-input>
                            <or-mwc-input class="alarm-input" ?disabled="${!write}"
                                          .label="${i18next.t("alarm.severity")}"
                                          .type="${InputType.SELECT}"
                                          .options="${this._getAddSeverityOptions().map(s => s.label)}"
                                          .value="${this._getAddSeverityOptions().filter((obj) => obj.value === alarm.severity).map((obj) => obj.label)[0]}"
                                          @or-mwc-input-changed="${(e) => {
            alarm.severity = this._getAddSeverityOptions().filter((obj) => obj.label === e.detail.value).map((obj) => obj.value)[0];
            this.onAlarmChanged(e);
        }}"
                            ></or-mwc-input>
                            <or-mwc-input class="alarm-input" ?disabled="${!write}"
                                          .label="${i18next.t("alarm.status")}"
                                          .type="${InputType.SELECT}"
                                          .options="${this._getAddStatusOptions().map(s => s.label)}"
                                          .value="${this._getAddStatusOptions().filter((obj) => obj.value === alarm.status).map((obj) => obj.label)[0]}"
                                          @or-mwc-input-changed="${(e) => {
            alarm.status = this._getAddStatusOptions().filter((obj) => obj.label === e.detail.value).map((obj) => obj.value)[0];
            this.onAlarmChanged(e);
        }}"
                            ></or-mwc-input>
                            <or-mwc-input class="alarm-input"
                                          ?disabled="${!manager.hasRole("read:users") && !manager.hasRole("read:admin")}"
                                          .label="${i18next.t("alarm.assignee")}"
                                          .type="${InputType.SELECT}"
                                          .options="${this._getUsers().map((obj) => obj.label)}"
                                          .value="${this._getUsers().filter((obj) => obj.label === alarm.assigneeUsername).map((obj) => obj.label)[0]}"
                                          @or-mwc-input-changed="${(e) => {
            alarm.assigneeId = this._getUsers().filter((obj) => obj.label === e.detail.value).map((obj) => obj.value)[0];
            this.onAlarmChanged(e);
        }}"
                            ></or-mwc-input>
                            <div>
                                <span style="margin: 0px auto 10px;"><or-translate value="linkedAssets"/>:</span>
                                <or-mwc-input outlined ?disabled="${!manager.hasRole("write:alarms")}"
                                              style="margin-left: 4px;"
                                              .type="${InputType.BUTTON}"
                                              .label="${i18next.t("selectRestrictedAssets", {
            number: (_a = alarm.alarmAssetLinks) === null || _a === void 0 ? void 0 : _a.length,
        })}"
                                              @or-mwc-input-changed="${(ev) => this._openAssetSelector(ev, alarm, readonly)}"
                                ></or-mwc-input>
                            </div>
                        </div>
                    </div>
                    <!-- Bottom controls (save/update and delete button) -->
                    ${when(!(readonly && !this._saveAlarmPromise), () => html `
                        <div class="row" style="justify-content: space-between; margin-top: 10px;">
                            ${when((manager.hasRole("write:alarms")), () => html `
                                <or-mwc-input class="alarm-input" style="margin: 0;" outlined
                                              ?disabled="${!manager.hasRole("write:alarms") || !alarm.id}"
                                              .label="${i18next.t("delete")}"
                                              .type="${InputType.BUTTON}"
                                              @or-mwc-input-changed="${() => this._deleteAlarm(this.alarm)}"
                                ></or-mwc-input>
                            `)}
                            <div>
                                <or-mwc-input class="alarm-input" style="margin: 0;" outlined
                                              .label="${i18next.t("cancel")}"
                                              .type="${InputType.BUTTON}"
                                              @or-mwc-input-changed="${() => this.reset()}"
                                ></or-mwc-input>
                                <or-mwc-input id="savebtn"
                                              style="margin: 0;"
                                              raised class="alarm-input" disabled
                                              .label="${i18next.t(alarm.id ? "save" : "create")}"
                                              .type="${InputType.BUTTON}"
                                              @or-mwc-input-changed="${(e) => {
            let error;
            this._saveAlarmPromise = this._createUpdateAlarm(alarm, alarm.id ? "update" : "create")
                .then(() => {
                showSnackbar(undefined, i18next.t("alarm.saveAlarmSucceeded"));
                this.reset();
            }).catch((ex) => {
                if (isAxiosError(ex)) {
                    error = {
                        status: ex.response.status,
                        text: ex.response.status === 403 ? i18next.t("alarm.alarmAlreadyExists") : i18next.t("errorOccurred"),
                    };
                }
            }).finally(() => {
                this._saveAlarmPromise = undefined;
            });
        }}"
                                ></or-mwc-input>
                            </div>
                        </div>
                    `)}
                </div>
            </div>
            </div>`;
    }
    _getStatusOptions() {
        return [{ label: 'alarm.allActive', value: 'All-active' }, {
                label: 'alarm.all',
                value: 'All'
            }].concat(this._getAddStatusOptions());
    }
    _getSeverityOptions() {
        return [{ label: 'alarm.all', value: 'All' }].concat(this._getAddSeverityOptions());
    }
    _getAddStatusOptions() {
        const statuses = ["OPEN" /* AlarmStatus.OPEN */, "ACKNOWLEDGED" /* AlarmStatus.ACKNOWLEDGED */, "IN_PROGRESS" /* AlarmStatus.IN_PROGRESS */, "RESOLVED" /* AlarmStatus.RESOLVED */, "CLOSED" /* AlarmStatus.CLOSED */];
        return statuses.map(status => ({ label: 'alarm.status_' + status, value: status }));
    }
    _getAddSeverityOptions() {
        const severities = ["LOW" /* AlarmSeverity.LOW */, "MEDIUM" /* AlarmSeverity.MEDIUM */, "HIGH" /* AlarmSeverity.HIGH */];
        return severities.map(status => ({ label: 'alarm.severity_' + status, value: status }));
    }
    _getSourceText() {
        var _a;
        const alarm = (_a = this.alarm) !== null && _a !== void 0 ? _a : this.creationState.alarmModel;
        const alarmSource = alarm.source;
        const alarmSourceId = alarm.sourceId;
        const sourceText = i18next.t('alarm.source_' + alarmSource);
        if (alarmSourceId) {
            let sourceIdText;
            if (["REALM_RULESET" /* AlarmSource.REALM_RULESET */, "ASSET_RULESET" /* AlarmSource.ASSET_RULESET */, "GLOBAL_RULESET" /* AlarmSource.GLOBAL_RULESET */].includes(alarmSource)) {
                sourceIdText = "ID: " + alarmSourceId;
            }
            else if (alarmSource === "MANUAL" /* AlarmSource.MANUAL */) {
                sourceIdText = alarm.sourceUsername;
            }
            return sourceIdText ? sourceText + " (" + sourceIdText + ")" : sourceText;
        }
        return sourceText;
    }
    _onSeverityChanged(severity) {
        if (severity == 'alarm.all') {
            this.severity = undefined;
            this._loadData();
            return;
        }
        this.severity = this._getSeverityOptions().filter((obj) => obj.label === severity).map((obj) => obj.value)[0];
        if (!this.severity) {
            return;
        }
        this._data = this._data.filter((e) => e.severity === this.severity);
    }
    _onAssignCheckChanged(assign) {
        return __awaiter(this, void 0, void 0, function* () {
            this.assign = assign;
            if (this.assign === undefined) {
                return;
            }
            const response = yield manager.rest.api.UserResource.getCurrent();
            if (response.status === 200 && this.assign) {
                this._data = this._data.filter((e) => e.assigneeId === response.data.id);
            }
            else if (!this.assign) {
                this._loadData();
            }
        });
    }
    _onStatusChanged(status) {
        if (status == 'alarm.all') {
            this.status = undefined;
            this.allActive = undefined;
            this.requestUpdate();
            return;
        }
        if (status == 'alarm.allActive') {
            this.status = undefined;
            this.allActive = true;
            this.requestUpdate();
            return;
        }
        this.allActive = undefined;
        this.status = this._getStatusOptions().filter((obj) => obj.label === status).map((obj) => obj.value)[0];
        if (!this.status) {
            return;
        }
        this._data = this._data.filter((e) => e.status === this.status);
    }
    _onRowSelect(ev) {
        const alarm = this._data[ev.detail.index];
        if (alarm) {
            if (ev.detail.state) {
                if (this._selectedIds === undefined) {
                    this._selectedIds = [alarm.id];
                }
                else {
                    this._selectedIds.push(alarm.id);
                    this.requestUpdate('_selectedIds');
                }
            }
            else {
                this._selectedIds = this._selectedIds.filter(id => id !== alarm.id);
            }
        }
        else {
            console.warn("Tried selecting an alarm that does not exist?");
        }
    }
    _onRowClick(ev) {
        if (!ev.detail.index && ev.detail.index != 0) {
            return;
        }
        this.alarm = this._data[ev.detail.index];
        this.alarm.loaded = false;
        this.alarm.loading = false;
        this.alarm.alarmAssetLinks = [];
        this.loadAlarm(this.alarm);
        this.requestUpdate();
    }
    _getUsers() {
        let options = this._loadedUsers.filter((u) => u.username != 'manager-keycloak').map((u) => {
            return { value: u.id, label: u.username };
        });
        options.unshift({ value: null, label: i18next.t("none") });
        return options;
    }
    _openAssetSelector(ev, alarm, readonly) {
        var _a;
        const openBtn = ev.target;
        openBtn.disabled = true;
        alarm.previousAssetLinks = alarm.alarmAssetLinks ? [...alarm.alarmAssetLinks] : [];
        const onAssetSelectionChanged = (e) => {
            alarm.alarmAssetLinks = e.detail.newNodes.map(node => {
                const alarmAssetLink = {
                    id: {
                        sentalarmId: alarm.id,
                        realm: alarm.realm,
                        assetId: node.asset.id
                    }
                };
                return alarmAssetLink;
            });
        };
        const dialog = showDialog(new OrMwcDialog()
            .setHeading(i18next.t("linkedAssets"))
            .setContent(html `
                        <or-asset-tree
                                id="chart-asset-tree"
                                readonly="true"
                                .selectedIds="${(_a = alarm.alarmAssetLinks) === null || _a === void 0 ? void 0 : _a.map((al) => al.id.assetId)}"
                                .showSortBtn="${false}"
                                expandNodes
                                checkboxes
                                @or-asset-tree-request-selection="${(e) => {
            this.creationState ? e.detail.allow = true : e.detail.allow = false;
        }}"
                                @or-asset-tree-selection="${(e) => {
            if (!readonly) {
                onAssetSelectionChanged(e);
            }
        }}"
                        ></or-asset-tree>
                    `)
            .setActions([
            {
                default: true,
                actionName: "cancel",
                content: i18next.t("cancel"),
                action: () => {
                    openBtn.disabled = false;
                },
            },
            {
                actionName: "ok",
                content: "ok",
                action: () => {
                    openBtn.disabled = false;
                    this.onAlarmChanged(this.shadowRoot.querySelector('or-mwc-input'));
                    this.requestUpdate();
                }
            }
        ])
            .setDismissAction({
            actionName: "cancel",
            action: () => {
                openBtn.disabled = false;
            },
        }));
    }
    // Reset selected alarm and go back to the alarm overview
    reset() {
        this._selectedIds = [];
        this.alarm = undefined;
        this.creationState = undefined;
        this._data = undefined;
        this._selectedIds = undefined;
        this.assign = false;
        if (this._table) {
            this._table = undefined;
        }
        if (this._refresh) {
            window.clearTimeout(this._refresh);
        }
    }
    stateChanged(state) {
        if (state.app.page == "alarms") {
            if (this.realm === undefined || this.realm == state.app.realm) {
                if (state.app.params && state.app.params.id) {
                    const parsedId = Number(state.app.params.id);
                    manager.rest.api.AlarmResource.getAlarm(parsedId).then((alarm) => {
                        this.alarm = alarm.data;
                        this.alarm.loaded = false;
                        this.alarm.loading = false;
                        this.alarm.alarmAssetLinks = [];
                        this.loadAlarm(this.alarm);
                        this.requestUpdate();
                    }).catch((ex) => {
                        if (isAxiosError(ex)) {
                            this.reset();
                        }
                    });
                }
                else if (!this.creationState) {
                    this.reset();
                }
            }
            else {
                this.realm = state.app.realm;
                this.requestUpdate('realm');
            }
        }
    }
    _updateRoute(silent = false) {
        var _a;
        router.navigate(getAlarmsRoute((_a = this.alarm) === null || _a === void 0 ? void 0 : _a.id.toString()), {
            callHooks: !silent,
            callHandler: !silent,
        });
    }
    onAlarmChanged(e) {
        const formElement = e instanceof OrInputChangedEvent ? e.target.parentElement : e.parentElement;
        const saveBtn = this.shadowRoot.getElementById("savebtn");
        if (formElement) {
            saveBtn.disabled = Array.from(formElement.children)
                .filter((e) => e instanceof OrMwcInput)
                .some((input) => !input.valid);
        }
        if (this.alarm && !this.alarm.title) {
            saveBtn.disabled = true;
        }
    }
};
__decorate([
    property()
], PageAlarms.prototype, "config", void 0);
__decorate([
    property()
], PageAlarms.prototype, "realm", void 0);
__decorate([
    state()
], PageAlarms.prototype, "alarm", void 0);
__decorate([
    state()
], PageAlarms.prototype, "creationState", void 0);
__decorate([
    state()
], PageAlarms.prototype, "_alarms", void 0);
__decorate([
    state()
], PageAlarms.prototype, "_linkedAssets", void 0);
__decorate([
    state()
], PageAlarms.prototype, "_linkedUsers", void 0);
__decorate([
    state()
], PageAlarms.prototype, "_loadedUsers", void 0);
__decorate([
    state()
], PageAlarms.prototype, "severity", void 0);
__decorate([
    state()
], PageAlarms.prototype, "status", void 0);
__decorate([
    state()
], PageAlarms.prototype, "assign", void 0);
__decorate([
    state()
], PageAlarms.prototype, "allActive", void 0);
__decorate([
    state()
], PageAlarms.prototype, "_data", void 0);
__decorate([
    state()
], PageAlarms.prototype, "_loadAlarmsPromise", void 0);
__decorate([
    state()
], PageAlarms.prototype, "_saveAlarmPromise", void 0);
__decorate([
    state()
], PageAlarms.prototype, "_loadUsersPromise", void 0);
__decorate([
    query("#table")
], PageAlarms.prototype, "_tableElem", void 0);
__decorate([
    state()
], PageAlarms.prototype, "_selectedIds", void 0);
PageAlarms = __decorate([
    customElement("page-alarms")
], PageAlarms);
export { PageAlarms };
//# sourceMappingURL=page-alarms.js.map