// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '/strings.m.js';
import './policy_precedence_row.js';
import './policy_row.js';
import './policy_table_header_cell.js';
import { CrLitElement } from 'chrome://resources/lit/v3_0/lit.rollup.js';
import { getCss } from './policy_table.css.js';
import { getHtml } from './policy_table.html.js';
export class PolicyTableElement extends CrLitElement {
    static get is() {
        return 'policy-table';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            dataModel: { type: Object },
            filterPattern: { type: String },
            showUnset: { type: Boolean },
            sortAscending: { type: Boolean },
            mostRecentSortedColumn: { type: String },
            sortedPolicies: { type: Array },
        };
    }
    #dataModel_accessor_storage = null;
    get dataModel() { return this.#dataModel_accessor_storage; }
    set dataModel(value) { this.#dataModel_accessor_storage = value; }
    #filterPattern_accessor_storage = '';
    get filterPattern() { return this.#filterPattern_accessor_storage; }
    set filterPattern(value) { this.#filterPattern_accessor_storage = value; }
    #showUnset_accessor_storage = false;
    get showUnset() { return this.#showUnset_accessor_storage; }
    set showUnset(value) { this.#showUnset_accessor_storage = value; }
    #sortAscending_accessor_storage = true;
    get sortAscending() { return this.#sortAscending_accessor_storage; }
    set sortAscending(value) { this.#sortAscending_accessor_storage = value; }
    #mostRecentSortedColumn_accessor_storage = 'name';
    get mostRecentSortedColumn() { return this.#mostRecentSortedColumn_accessor_storage; }
    set mostRecentSortedColumn(value) { this.#mostRecentSortedColumn_accessor_storage = value; }
    #sortedPolicies_accessor_storage = [];
    get sortedPolicies() { return this.#sortedPolicies_accessor_storage; }
    set sortedPolicies(value) { this.#sortedPolicies_accessor_storage = value; }
    updateDataModel(dataModel) {
        this.dataModel = dataModel;
    }
    setFilterPattern(pattern) {
        this.filterPattern = pattern.toLowerCase();
    }
    willUpdate(changedProperties) {
        super.willUpdate(changedProperties);
        if (changedProperties.has('dataModel') ||
            changedProperties.has('sortAscending') ||
            changedProperties.has('mostRecentSortedColumn')) {
            this.sortedPolicies = this.getSortedPolicies();
        }
    }
    sortColumn(ascending, field) {
        this.sortAscending = ascending;
        this.mostRecentSortedColumn = field;
    }
    onSortChanged(e) {
        this.sortColumn(e.detail.ascending, e.detail.field);
    }
    getSortedPolicies() {
        if (!this.dataModel || !this.dataModel.policies) {
            return [];
        }
        const sorted = [...this.dataModel.policies];
        const orderMultiplier = this.sortAscending ? 1 : -1;
        const field = this.mostRecentSortedColumn;
        sorted.sort((a, b) => {
            if ((a.value !== undefined && b.value !== undefined) ||
                a.value === b.value) {
                if (a.link !== undefined && b.link !== undefined) {
                    if (field !== 'name' && a[field] === b[field]) {
                        return orderMultiplier * (a.name > b.name ? 1 : -1);
                    }
                    return orderMultiplier *
                        (a[field] > b[field] ? 1 : -1);
                }
                return a.link !== undefined ? -1 : 1;
            }
            return a.value !== undefined ? -1 : 1;
        });
        return sorted;
    }
    isPolicyHidden(policy) {
        const matchesSearch = policy.name.toLowerCase().includes(this.filterPattern);
        const isSet = policy.value !== undefined;
        return !(matchesSearch && (isSet || this.showUnset));
    }
    hasVisiblePolicies() {
        return this.sortedPolicies.some(policy => !this.isPolicyHidden(policy));
    }
}
customElements.define(PolicyTableElement.is, PolicyTableElement);
