// 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 'chrome://resources/cr_elements/cr_button/cr_button.js';
import 'chrome://resources/cr_elements/cr_dialog/cr_dialog.js';
import 'chrome://resources/cr_elements/cr_link_row/cr_link_row.js';
import 'chrome://resources/cr_elements/cr_shared_style.css.js';
import 'chrome://resources/cr_elements/cr_spinner_style.css.js';
import 'chrome://resources/cr_elements/cr_toast/cr_toast.js';
import './shared_style.css.js';
import { I18nMixin } from 'chrome://resources/cr_elements/i18n_mixin.js';
import { assert } from 'chrome://resources/js/assert.js';
import { loadTimeData } from 'chrome://resources/js/load_time_data.js';
import { PolymerElement } from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import { ExportPasswordsResult, ExportProgressStatus, PasswordManagerImpl, toMojoExportProgressStatus } from './password_manager_proxy.js';
import { getTemplate } from './passwords_exporter.html.js';
const PasswordsExporterElementBase = I18nMixin(PolymerElement);
export class PasswordsExporterElement extends PasswordsExporterElementBase {
    static get is() {
        return 'passwords-exporter';
    }
    static get template() {
        return getTemplate();
    }
    static get properties() {
        return {
            /** Whether password export progress spinner is shown. */
            showExportInProgress_: {
                type: Boolean,
                value: false,
            },
            /** Whether password export error dialog is shown. */
            showPasswordsExportErrorDialog_: {
                type: Boolean,
                value: false,
            },
            /** The error that occurred while exporting. */
            exportErrorMessage_: {
                type: String,
                value: null,
            },
        };
    }
    exportListenerId_ = null;
    onPasswordsFileExportProgressListener_ = null;
    connectedCallback() {
        super.connectedCallback();
        const proxy = PasswordManagerImpl.getInstance();
        // If export started on a different tab and is still in progress, display a
        // busy UI.
        proxy.requestExportProgressStatus().then(status => {
            if (status === ExportProgressStatus.kInProgress) {
                this.showExportInProgress_ = true;
            }
        });
        if (loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            this.exportListenerId_ =
                proxy.callbackRouter.onPasswordsExportProgress.addListener(this.onPasswordsFileExportProgress_.bind(this));
        }
        else {
            this.onPasswordsFileExportProgressListener_ =
                (progress) => {
                    this.onPasswordsFileExportProgress_(toMojoExportProgressStatus(progress.status), progress.folderName || null);
                };
            proxy.addPasswordsFileExportProgressListener(this.onPasswordsFileExportProgressListener_);
        }
    }
    disconnectedCallback() {
        const proxy = PasswordManagerImpl.getInstance();
        if (loadTimeData.getBoolean('enablePasswordManagerMojoApi')) {
            assert(this.exportListenerId_ !== null);
            proxy.callbackRouter.removeListener(this.exportListenerId_);
            this.exportListenerId_ = null;
        }
        else {
            assert(this.onPasswordsFileExportProgressListener_);
            proxy.removePasswordsFileExportProgressListener(this.onPasswordsFileExportProgressListener_);
            this.onPasswordsFileExportProgressListener_ = null;
        }
        super.disconnectedCallback();
    }
    /**
     * Tells the PasswordsPrivate API to export saved passwords in a .csv file.
     */
    onExportClick_() {
        PasswordManagerImpl.getInstance().exportPasswords().then((result) => {
            if (result === ExportPasswordsResult.kInProgress) {
                // Exporting was started by a different call to exportPasswords()
                // and is still in progress. This UI needs to be updated to the
                // current status.
                this.showExportInProgress_ = true;
            }
        });
    }
    /**
     * Closes the export error dialog.
     */
    closePasswordsExportErrorDialog_() {
        this.showPasswordsExportErrorDialog_ = false;
    }
    /**
     * Retries export from the error dialog.
     */
    onTryAgainClick_() {
        this.closePasswordsExportErrorDialog_();
        this.onExportClick_();
    }
    /**
     * Handles an export progress event by showing the progress spinner or caching
     * the event for later consumption.
     */
    onPasswordsFileExportProgress_(status, folderName) {
        if (status === ExportProgressStatus.kInProgress) {
            this.showExportInProgress_ = true;
            return;
        }
        this.showExportInProgress_ = false;
        switch (status) {
            case ExportProgressStatus.kSucceeded:
                this.$.exportSuccessToast.show();
                break;
            case ExportProgressStatus.kFailedWrite:
                assert(folderName);
                this.exportErrorMessage_ =
                    this.i18n('exportPasswordsFailTitle', folderName);
                this.showPasswordsExportErrorDialog_ = true;
                break;
            default:
                break;
        }
    }
    onOpenInShellButtonClick_() {
        PasswordManagerImpl.getInstance().showLastExportedFileInShell();
        this.$.exportSuccessToast.hide();
    }
    getAriaDescription_() {
        return [
            this.i18n('exportPasswords'),
            this.i18n('exportPasswordsDescription'),
        ].join('. ');
    }
}
customElements.define(PasswordsExporterElement.is, PasswordsExporterElement);
