// 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 '/strings.m.js';
import './installed_app_list_item.js';
import { loadTimeData } from '//resources/js/load_time_data.js';
import { CrLitElement } from '//resources/lit/v3_0/lit.rollup.js';
import { getCss } from './app.css.js';
import { getHtml } from './app.html.js';
import { browserProxyFactory } from './iwa_dev.mojom-webui.js';
export class IwaDevAppElement extends CrLitElement {
    static get is() {
        return 'iwa-dev-app';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            devModeEnabled_: { type: Boolean },
            installedApps_: { type: Array },
            hasFetchedApps_: { type: Boolean, state: true },
        };
    }
    #devModeEnabled__accessor_storage = loadTimeData.getBoolean('isIwaDevModeEnabled');
    get devModeEnabled_() { return this.#devModeEnabled__accessor_storage; }
    set devModeEnabled_(value) { this.#devModeEnabled__accessor_storage = value; }
    #installedApps__accessor_storage = [];
    get installedApps_() { return this.#installedApps__accessor_storage; }
    set installedApps_(value) { this.#installedApps__accessor_storage = value; }
    #hasFetchedApps__accessor_storage = false;
    get hasFetchedApps_() { return this.#hasFetchedApps__accessor_storage; }
    set hasFetchedApps_(value) { this.#hasFetchedApps__accessor_storage = value; }
    browserProxy_ = browserProxyFactory.getInstance();
    listenerIds_ = [];
    async onRequestUninstall(e) {
        await this.browserProxy_.handler.uninstallApp(e.detail.app.appId);
    }
    async connectedCallback() {
        super.connectedCallback();
        this.listenerIds_.push(this.browserProxy_.callbackRouter.onAppInstalled.addListener((appInfo) => this.onAppInstalled_(appInfo)));
        this.listenerIds_.push(this.browserProxy_.callbackRouter.onAppUpdated.addListener((appInfo) => this.onAppUpdated_(appInfo)));
        this.listenerIds_.push(this.browserProxy_.callbackRouter.onAppUninstalled.addListener((appId) => this.onAppUninstalled_(appId)));
        if (this.devModeEnabled_) {
            const { apps } = await this.browserProxy_.handler.getInstalledAppsInfo();
            this.installedApps_ = this.sortApps_(apps);
            this.hasFetchedApps_ = true;
        }
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.listenerIds_.forEach(id => this.browserProxy_.callbackRouter.removeListener(id));
        this.listenerIds_ = [];
    }
    sortApps_(apps) {
        return apps.sort((a, b) => a.appId.localeCompare(b.appId));
    }
    onAppInstalled_(appInfo) {
        this.installedApps_ = this.sortApps_([...this.installedApps_, appInfo]);
    }
    onAppUpdated_(appInfo) {
        this.installedApps_ = this.installedApps_.map(app => app.appId === appInfo.appId ? appInfo : app);
    }
    onAppUninstalled_(appId) {
        this.installedApps_ =
            this.installedApps_.filter(app => app.appId !== appId);
    }
}
customElements.define(IwaDevAppElement.is, IwaDevAppElement);
