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;
};
/*
 * Copyright 2024, OpenRemote Inc.
 *
 * See the CONTRIBUTORS.txt file in the distribution for a
 * full listing of individual contributors.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
import { css, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { OrMwcTable } from "@openremote/or-mwc-components/or-mwc-table";
import { i18next } from "@openremote/or-translate";
import { classMap } from "lit/directives/class-map.js";
const styling = css `
    .alarm-text {
        padding: 4px 6px;
        border-radius: 5px;
    }

    .alarm-status-text__closed {
        color: var(--or-alarm-status-color__closed, grey);
        background-color: var(--or-alarm-status-background__closed);
        border: var(--or-alarm-status-border__closed, 1px #8590A2 solid);
    }

    .alarm-status-text__resolved {
        color: var(--or-alarm-status-color__resolved, white);
        background-color: var(--or-alarm-status-background__resolved, #22A06B);
        border: var(--or-alarm-status-border__resolved);
    }

    .alarm-status-text__acknowledged {
        color: var(--or-alarm-status-color__acknowledged);
        background-color: var(--or-alarm-status-background__acknowledged);
        border: var(--or-alarm-status-border__acknowledged, 1px #8F7EE7 solid);
    }

    .alarm-status-text__in-progress {
        color: var(--or-alarm-status-color__in-progress, white);
        background-color: var(--or-alarm-status-background__in-progress, #388BFF);
        border: var(--or-alarm-status-border__in-progress);
    }

    .alarm-status-text__open {
        color: var(--or-alarm-status-color__open, white);
        background-color: var(--or-alarm-status-background__open, #8F7EE7);
        border: var(--or-alarm-status-border__open);
    }

    .alarm-severity-text__low {
        color: white;
        background-color: #388BFF;
    }

    .alarm-severity-text__medium {
        color: white;
        background-color: #ffab00;
    }

    .alarm-severity-text__high {
        color: white;
        background-color: #ff5630;
    }
`;
/**
 * ## or-alarms-table
 * <p>
 * It extends on {@link OrMwcTable}, and applies both additional styling and helper functions to it.
 * </p>
 * Usage;
 * <pre>
 *   <or-alarms-table .alarms="${alarms}"></or-alarms-table>
 * </pre>
 * */
let OrAlarmsTable = class OrAlarmsTable extends OrMwcTable {
    constructor() {
        super(...arguments);
        this.alarms = [];
        this.readonly = true;
        // Setup columns
        this.columns = [
            { title: i18next.t("alarm.severity"), widthWeight: 3, isSortable: true },
            { title: i18next.t("alarm.title"), widthWeight: 25, isSortable: true },
            { title: i18next.t("alarm.status"), widthWeight: 3, isSortable: true },
            { title: i18next.t("alarm.linkedAssets"), widthWeight: 6, hideMobile: true },
            { title: i18next.t("alarm.assignee"), widthWeight: 4, isSortable: true },
            { title: i18next.t("alarm.lastModified"), widthWeight: 6, isSortable: true }
        ];
        // Config override
        this.config = {
            columnFilter: [],
            stickyFirstColumn: false,
            pagination: {
                enable: true
            },
            multiSelect: !this.readonly
        };
        this.sortIndex = 5;
        this.sortDirection = "DESC";
    }
    static get styles() {
        return [...super.styles, styling];
    }
    willUpdate(changedProps) {
        // If alarms changes, update content of the table accordingly
        if (changedProps.has("alarms") && this.alarms) {
            this.selectedRows = [];
            this.rows = this.getTableRows(this.alarms);
        }
        // If readonly state changes, update multi select
        if (changedProps.has("readonly")) {
            this.config.multiSelect = !this.readonly;
        }
        return super.willUpdate(changedProps);
    }
    getTableRows(alarms) {
        return alarms === null || alarms === void 0 ? void 0 : alarms.map(a => {
            return {
                content: [
                    this.getSeverityContent(a.severity),
                    this.getDisplayNameContent(a.title),
                    this.getAlarmStatusContent(a.status),
                    this.getLinkedAssetsContent(a.asset),
                    this.getAssigneeContent(a.assigneeUsername),
                    this.getLastModifiedContent(a.lastModified)
                ],
                clickable: true
            };
        });
    }
    /* --------------------------------------- */
    /* TEMPLATING FUNCTIONS */
    getSeverityContent(severity) {
        const classes = {
            "alarm-text": true,
            "alarm-severity-text__low": severity === "LOW" /* AlarmSeverity.LOW */,
            "alarm-severity-text__medium": severity === "MEDIUM" /* AlarmSeverity.MEDIUM */,
            "alarm-severity-text__high": severity === "HIGH" /* AlarmSeverity.HIGH */
        };
        return html `
            <or-translate class=${classMap(classes)}
                          value="${severity ? `alarm.severity_${severity}` : "error"}"></or-translate>
        `;
    }
    getDisplayNameContent(title) {
        return title;
    }
    getAlarmStatusContent(status) {
        const classes = {
            "alarm-text": true,
            "alarm-status-text__closed": status === "CLOSED" /* AlarmStatus.CLOSED */,
            "alarm-status-text__resolved": status === "RESOLVED" /* AlarmStatus.RESOLVED */,
            "alarm-status-text__acknowledged": status === "ACKNOWLEDGED" /* AlarmStatus.ACKNOWLEDGED */,
            "alarm-status-text__in-progress": status === "IN_PROGRESS" /* AlarmStatus.IN_PROGRESS */,
            "alarm-status-text__open": status === "OPEN" /* AlarmStatus.OPEN */
        };
        return html `
            <or-translate class=${classMap(classes)}
                          value="${status ? `alarm.status_${status}` : "error"}"></or-translate>
        `;
    }
    getLinkedAssetsContent(assets) {
        return html `${assets === null || assets === void 0 ? void 0 : assets.map(a => a.name).join(', ')}`;
    }
    getAssigneeContent(assigneeName) {
        return assigneeName;
    }
    getLastModifiedContent(lastModified) {
        return new Date(lastModified);
    }
    sortTemplateRows(cellA, cellB, cIndex, sortDirection) {
        var _a, _b;
        const valueA = (_a = cellA.values.filter(v => typeof v === 'string' || typeof v === 'number').map(v => v.toString())) === null || _a === void 0 ? void 0 : _a[0];
        const valueB = (_b = cellB.values.filter(v => typeof v === 'string' || typeof v === 'number').map(v => v.toString())) === null || _b === void 0 ? void 0 : _b[0];
        if (valueA !== undefined && valueB !== undefined) {
            if (valueA.includes("alarm.status") && valueB.includes("alarm.status")) {
                const sortingArr = ["alarm.status_OPEN", "alarm.status_ACKNOWLEDGED", "alarm.status_IN_PROGRESS", "alarm.status_RESOLVED", "alarm.status_CLOSED"];
                if (sortDirection === 'DESC') {
                    return sortingArr.indexOf(valueB) - sortingArr.indexOf(valueA);
                }
                else {
                    return sortingArr.indexOf(valueA) - sortingArr.indexOf(valueB);
                }
            }
            if (valueA.includes("alarm.severity") && valueB.includes("alarm.severity")) {
                const sortingArr = ["alarm.severity_LOW", "alarm.severity_MEDIUM", "alarm.severity_HIGH"];
                if (sortDirection === 'DESC') {
                    return sortingArr.indexOf(valueB) - sortingArr.indexOf(valueA);
                }
                else {
                    return sortingArr.indexOf(valueA) - sortingArr.indexOf(valueB);
                }
            }
            return this.sortPrimitiveRows([valueA], [valueB], 0, sortDirection);
        }
        else {
            return 1;
        }
    }
    /* --------------------------------------- */
    /* STYLING MODIFICATIONS */
    /** Applies the custom 'weighted width' field of AlarmTableColumn, to make columns wider/smaller relative to others. */
    getColumnWidth(index, columns, tableWidthPx) {
        const column = columns[index];
        const totalWeight = this.getTotalColumnWidthWeight(columns);
        if (typeof column !== "string" && totalWeight && column.widthWeight) {
            return `${column.widthWeight / totalWeight * 100}%`;
        }
        return super.getColumnWidth(index, columns, tableWidthPx);
    }
    /** In AlarmTableColumn, columns can have a 'weighted width' which makes them wider/smaller relative to others.
     * This function calculates the total of these values. */
    getTotalColumnWidthWeight(columns) {
        let totalWeight = 0;
        columns.forEach(c => {
            if (typeof c !== "string") {
                totalWeight = totalWeight + c.widthWeight;
            }
        });
        return totalWeight;
    }
};
__decorate([
    property()
], OrAlarmsTable.prototype, "alarms", void 0);
__decorate([
    property({ type: Boolean })
], OrAlarmsTable.prototype, "readonly", void 0);
OrAlarmsTable = __decorate([
    customElement("or-alarms-table")
], OrAlarmsTable);
export { OrAlarmsTable };
//# sourceMappingURL=or-alarms-table.js.map