// Copyright 2026 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '//resources/cr_elements/cr_button/cr_button.js';
import { assertNotReached } from '//resources/js/assert.js';
import { CrLitElement } from '//resources/lit/v3_0/lit.rollup.js';
import { getCss } from './installed_app_list_item.css.js';
import { getHtml } from './installed_app_list_item.html.js';
/**
 * Converts a mojo origin into a user-readable string, omitting default ports.
 * @param origin Origin to convert.
 */
function originToText(origin) {
    if (origin.host.length === 0) {
        return 'null';
    }
    const url = new URL(`${origin.scheme}://${origin.host}`);
    if (origin.port !== 0) {
        url.port = origin.port.toString();
    }
    return url.origin;
}
/**
 * Converts a mojo representation of `base::FilePath` into a user-readable
 * string.
 * @param filePath File path to convert
 */
function filePathToText(filePath) {
    if (typeof filePath.path === 'string') {
        return filePath.path;
    }
    // On Windows, base::FilePath is represented as a UTF-16 code unit array
    // rather than a string.
    const decoder = new TextDecoder('utf-16');
    return decoder.decode(new Uint16Array(filePath.path));
}
export class InstalledAppListItemElement extends CrLitElement {
    static get is() {
        return 'installed-app-list-item';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            app: { type: Object },
            sourceMetadata: { type: Object },
        };
    }
    #app_accessor_storage = {
        appId: '',
        webBundleId: '',
        name: '',
        source: {
            proxyOrigin: {
                scheme: '',
                host: '',
                port: 0,
                nonceIfOpaque: null,
            },
        },
        installedVersion: '',
    };
    get app() { return this.#app_accessor_storage; }
    set app(value) { this.#app_accessor_storage = value; }
    #sourceMetadata_accessor_storage = {
        label: '',
        description: '',
    };
    get sourceMetadata() { return this.#sourceMetadata_accessor_storage; }
    set sourceMetadata(value) { this.#sourceMetadata_accessor_storage = value; }
    onUninstallClick() {
        this.fire('request-uninstall', { app: this.app });
    }
    willUpdate(changedProperties) {
        super.willUpdate(changedProperties);
        if (changedProperties.has('app')) {
            this.sourceMetadata = this.computeSourceMetadata();
        }
    }
    /**
     * Returns the metadata to be displayed for the app's installation source.
     */
    computeSourceMetadata() {
        if (this.app.source.proxyOrigin) {
            return {
                label: 'Dev mode proxy',
                description: originToText(this.app.source.proxyOrigin),
            };
        }
        if (this.app.source.bundlePath) {
            return {
                label: 'Local Bundle',
                description: filePathToText(this.app.source.bundlePath),
            };
        }
        if (this.app.source.updateInfo) {
            const info = this.app.source.updateInfo;
            return {
                label: 'Update manifest',
                description: `${info.updateManifestUrl} (channel: ${info.updateChannel})`,
            };
        }
        assertNotReached();
    }
}
customElements.define(InstalledAppListItemElement.is, InstalledAppListItemElement);
