// 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 'chrome://resources/cr_elements/cr_button/cr_button.js';
import 'chrome://resources/cr_elements/cr_icon_button/cr_icon_button.js';
import 'chrome://resources/cr_elements/cr_toast/cr_toast.js';
import 'chrome://resources/cr_elements/cr_shared_style.css.js';
import '../settings_page/settings_subpage.js';
import '../simple_confirmation_dialog.js';
import '../site_favicon.js';
import { assert } from '//resources/js/assert.js';
import { I18nMixin } from 'chrome://resources/cr_elements/i18n_mixin.js';
import { WebUiListenerMixin } from 'chrome://resources/cr_elements/web_ui_listener_mixin.js';
import { PolymerElement } from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import { GlicBrowserProxyImpl } from './glic_browser_proxy.js';
import { getTemplate } from './glic_login_permissions_page.html.js';
const SettingsGlicLoginPermissionsPageElementBase = WebUiListenerMixin(I18nMixin(PolymerElement));
export class SettingsGlicLoginPermissionsPageElement extends SettingsGlicLoginPermissionsPageElementBase {
    static get is() {
        return 'settings-glic-login-permissions-page';
    }
    static get template() {
        return getTemplate();
    }
    static get properties() {
        return {
            actorLoginPermissions_: {
                type: Array,
                value: () => [],
            },
            isOnline_: {
                type: Boolean,
                value: () => navigator.onLine,
            },
            selectedPermissionToRemove_: {
                type: Object,
                value: null,
            },
        };
    }
    browserProxy_ = GlicBrowserProxyImpl.getInstance();
    boundOnOnline_ = () => this.isOnline_ = true;
    boundOnOffline_ = () => this.isOnline_ = false;
    connectedCallback() {
        super.connectedCallback();
        this.addWebUiListener('actor-login-permissions-changed', (permissions) => {
            this.actorLoginPermissions_ = permissions;
        });
        window.addEventListener('online', this.boundOnOnline_);
        window.addEventListener('offline', this.boundOnOffline_);
        this.browserProxy_.getActorLoginPermissions().then(permissions => {
            this.actorLoginPermissions_ = permissions;
        });
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        window.removeEventListener('online', this.boundOnOnline_);
        window.removeEventListener('offline', this.boundOnOffline_);
    }
    onRemoveActorLoginPermissionClick_(e) {
        this.selectedPermissionToRemove_ = e.model.item;
    }
    async onRemoveDialogClose_() {
        const dialog = this.shadowRoot.querySelector('settings-simple-confirmation-dialog');
        assert(dialog);
        assert(this.selectedPermissionToRemove_);
        if (dialog.wasConfirmed()) {
            const success = await this.browserProxy_.revokeActorLoginPermission(this.selectedPermissionToRemove_.signonRealm, this.selectedPermissionToRemove_.username);
            if (!success) {
                const toast = this.shadowRoot.querySelector('#removeErrorToast');
                assert(toast);
                toast.show();
            }
        }
        this.selectedPermissionToRemove_ = null;
    }
    getRemoveDialogDescription_(url) {
        return this.i18n('glicRemoveActorLoginDialogDescription', url);
    }
}
customElements.define(SettingsGlicLoginPermissionsPageElement.is, SettingsGlicLoginPermissionsPageElement);
