// 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 { loadTimeData } from 'chrome://resources/js/load_time_data.js';
import { CrLitElement } from 'chrome://resources/lit/v3_0/lit.rollup.js';
import { getCss } from './policy_conflict.css.js';
import { getHtml } from './policy_conflict.html.js';
// Converts a policy value to a JSON string and optionally formats it.
export function stringifyPolicyValue(value, format) {
    // Guard against undefined values;
    // pass nulls, as they are a valid policy value.
    if (value === undefined) {
        return '';
    }
    // Skip 'string' policy to avoid unnecessary conversions.
    if (typeof value === 'string') {
        return value;
    }
    if (format) {
        return JSON.stringify(value, null, 2);
    }
    else {
        return JSON.stringify(value, null);
    }
}
// Copies the text content of an element to the clipboard.
export function copyValue(element) {
    const selection = window.getSelection();
    const range = window.document.createRange();
    range.selectNodeContents(element);
    selection.removeAllRanges();
    selection.addRange(range);
    navigator.clipboard.writeText(element.innerText).catch(error => {
        console.error('Unable to copy value to clipboard:', error);
    });
}
export class PolicyConflictElement extends CrLitElement {
    static get is() {
        return 'policy-conflict';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            conflict: { type: Object },
            rowLabel: { type: String },
            policyName: { type: String },
        };
    }
    #conflict_accessor_storage = null;
    get conflict() { return this.#conflict_accessor_storage; }
    set conflict(value) { this.#conflict_accessor_storage = value; }
    #rowLabel_accessor_storage = '';
    get rowLabel() { return this.#rowLabel_accessor_storage; }
    set rowLabel(value) { this.#rowLabel_accessor_storage = value; }
    #policyName_accessor_storage = '';
    get policyName() { return this.#policyName_accessor_storage; }
    set policyName(value) { this.#policyName_accessor_storage = value; }
    connectedCallback() {
        super.connectedCallback();
        this.setAttribute('role', 'rowgroup');
    }
    getRowLabelText() {
        return this.rowLabel ? loadTimeData.getString(this.rowLabel) : '';
    }
    getScopeText() {
        if (!this.conflict) {
            return '';
        }
        return loadTimeData.getString(this.conflict.scope === 'user' ? 'scopeUser' : 'scopeDevice');
    }
    getLevelText() {
        if (!this.conflict) {
            return '';
        }
        return loadTimeData.getString(this.conflict.level === 'recommended' ? 'levelRecommended' :
            'levelMandatory');
    }
    getSourceText() {
        if (!this.conflict) {
            return '';
        }
        return loadTimeData.getString(this.conflict.source);
    }
    getFormattedValue() {
        if (!this.conflict) {
            return '';
        }
        return stringifyPolicyValue(this.conflict.value, /*format=*/ true);
    }
    getCopyLabel() {
        return loadTimeData.getStringF('policyCopyValue', this.policyName);
    }
    // Copies the policy's conflicting/superseded value to the clipboard.
    onCopyClick(e) {
        const target = e.currentTarget;
        // Walk up the DOM to find the parent .row and then get the .value element.
        const row = target.closest('.row') || target.closest('.entry');
        const valueDisplay = row?.querySelector('.value');
        if (valueDisplay) {
            copyValue(valueDisplay);
        }
    }
}
customElements.define(PolicyConflictElement.is, PolicyConflictElement);
