// Copyright 2024 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 { CrLitElement } from '//resources/lit/v3_0/lit.rollup.js';
import { getCss } from './model_status.css.js';
import { getHtml } from './model_status.html.js';
import { browserProxyFactory } from './on_device_internals_page.mojom-webui.js';
import { PerformanceClass } from './on_device_model.mojom-webui.js';
export class OnDeviceInternalsModelStatusElement extends CrLitElement {
    static get is() {
        return 'on-device-internals-model-status';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            pageData_: { type: Object },
            mayRestartBrowser_: { type: Boolean },
            loadProgress: { type: Number },
            loadMax: { type: Number },
            readableLoadProgress: { type: String },
            readableLoadMax: { type: String },
        };
    }
    #pageData__accessor_storage = {
        baseModel: {
            state: 'NO STATE',
            registrationCriteria: {},
            info: null,
        },
        suppModels: [],
        modelCrashCount: 0,
        maxModelCrashCount: 0,
        featureAdaptations: [],
        performanceInfo: {
            performanceClass: PerformanceClass.kError,
            vramMb: 0n,
        },
        minVramMb: 0n,
    };
    get pageData_() { return this.#pageData__accessor_storage; }
    set pageData_(value) { this.#pageData__accessor_storage = value; }
    #mayRestartBrowser__accessor_storage = false;
    get mayRestartBrowser_() { return this.#mayRestartBrowser__accessor_storage; }
    set mayRestartBrowser_(value) { this.#mayRestartBrowser__accessor_storage = value; }
    proxy_ = browserProxyFactory.getInstance();
    #loadProgress_accessor_storage = 0;
    get loadProgress() { return this.#loadProgress_accessor_storage; }
    set loadProgress(value) { this.#loadProgress_accessor_storage = value; }
    #loadMax_accessor_storage = 100;
    get loadMax() { return this.#loadMax_accessor_storage; }
    set loadMax(value) { this.#loadMax_accessor_storage = value; }
    #readableLoadProgress_accessor_storage = '_';
    get readableLoadProgress() { return this.#readableLoadProgress_accessor_storage; }
    set readableLoadProgress(value) { this.#readableLoadProgress_accessor_storage = value; }
    #readableLoadMax_accessor_storage = '_';
    get readableLoadMax() { return this.#readableLoadMax_accessor_storage; }
    set readableLoadMax(value) { this.#readableLoadMax_accessor_storage = value; }
    constructor() {
        super();
        this.getPageData_();
    }
    formatBytes(bytes) {
        if (bytes === 0) {
            return '0 Bytes';
        }
        const k = 1024;
        const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
        const i = Math.min(Math.round(Math.log(bytes) / Math.log(k)) - 1, sizes.length);
        return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`;
    }
    connectedCallback() {
        super.connectedCallback();
        browserProxyFactory.getInstance()
            .callbackRouter.onDownloadProgressUpdate.addListener(this.logProgress_.bind(this));
    }
    logProgress_(downloadedBytes, totalBytes) {
        this.loadProgress = Number(downloadedBytes);
        this.loadMax = Number(totalBytes);
        this.readableLoadProgress = this.formatBytes(this.loadProgress);
        this.readableLoadMax = this.formatBytes(this.loadMax);
    }
    async onResetModelCrashCountClick_() {
        await this.proxy_.handler.resetModelCrashCount();
        await this.getPageData_();
        this.mayRestartBrowser_ = true;
    }
    onSetFeatureUsageTrueClick_(e) {
        const feature = Number(e.currentTarget.dataset['feature']);
        this.setFeatureUsage_(feature, true);
    }
    onSetFeatureUsageFalseClick_(e) {
        const feature = Number(e.currentTarget.dataset['feature']);
        this.setFeatureUsage_(feature, false);
    }
    async setFeatureUsage_(feature, isRecentlyUsed) {
        await this.proxy_.handler.setFeatureRecentlyUsedState(feature, isRecentlyUsed);
        this.getPageData_();
    }
    async getPageData_() {
        this.pageData_ = (await this.proxy_.handler.getPageData()).pageData;
    }
    onUninstallDefaultModelClick_() {
        this.proxy_.handler.uninstallDefaultModel();
    }
}
customElements.define(OnDeviceInternalsModelStatusElement.is, OnDeviceInternalsModelStatusElement);
