// 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 '/icons.html.js';
import 'chrome://resources/cr_elements/cr_button/cr_button.js';
import 'chrome://resources/cr_elements/cr_checkbox/cr_checkbox.js';
import './managed_user_profile_notice_disclosure_refresh.js';
import './managed_user_profile_notice_value_prop.js';
import './managed_user_profile_notice_state.js';
import './managed_user_profile_notice_data_handling.js';
import { I18nMixinLit } from 'chrome://resources/cr_elements/i18n_mixin_lit.js';
import { WebUiListenerMixinLit } from 'chrome://resources/cr_elements/web_ui_listener_mixin_lit.js';
import { assertNotReached, assertNotReachedCase } from 'chrome://resources/js/assert.js';
import { loadTimeData } from 'chrome://resources/js/load_time_data.js';
import { CrLitElement } from 'chrome://resources/lit/v3_0/lit.rollup.js';
import { getCss } from './managed_user_profile_notice_app_refresh.css.js';
import { getHtml } from './managed_user_profile_notice_app_refresh.html.js';
import { AppMode, BrowsingDataHandling, ManagedUserProfileNoticeBrowserProxyImpl, ScreenType, State } from './managed_user_profile_notice_browser_proxy.js';
const ManagedUserProfileNoticeAppRefreshElementBase = WebUiListenerMixinLit(I18nMixinLit(CrLitElement));
export class ManagedUserProfileNoticeAppRefreshElement extends ManagedUserProfileNoticeAppRefreshElementBase {
    static get is() {
        return 'managed-user-profile-notice-app-refresh';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            profileInfo_: { type: Object },
            /**
             * Whether this page is being shown as a dialog.
             *
             * Reflected as an attribute to allow configuring variables and styles at
             * the element host level.
             */
            isModalDialog_: {
                type: Boolean,
                reflect: true,
            },
            proceedLabel_: { type: String },
            errorTitle_: { type: String },
            errorSubtitle_: { type: String },
            disableProceedButton_: { type: Boolean },
            currentState_: { type: Number },
            processingSubtitle_: { type: String },
            selectedDataHandling_: { type: String },
            usePrimaryAndTonalButtons_: { type: Boolean },
            appMode_: {
                type: String,
                reflect: true,
                attribute: 'app-mode',
            },
            revampEnabled_: {
                type: Boolean,
                reflect: true,
                attribute: 'revamp-enabled',
            },
        };
    }
    #profileInfo__accessor_storage = {
        accountName: '',
        continueAs: '',
        email: '',
        pictureUrl: '',
        showEnterpriseBadge: false,
        title: '',
        subtitle: '',
        proceedLabel: '',
        checkLinkDataCheckboxByDefault: false,
    };
    get profileInfo_() { return this.#profileInfo__accessor_storage; }
    set profileInfo_(value) { this.#profileInfo__accessor_storage = value; }
    #isModalDialog__accessor_storage = loadTimeData.getBoolean('isModalDialog');
    get isModalDialog_() { return this.#isModalDialog__accessor_storage; }
    set isModalDialog_(value) { this.#isModalDialog__accessor_storage = value; }
    #proceedLabel__accessor_storage = '';
    get proceedLabel_() { return this.#proceedLabel__accessor_storage; }
    set proceedLabel_(value) { this.#proceedLabel__accessor_storage = value; }
    #errorTitle__accessor_storage = '';
    get errorTitle_() { return this.#errorTitle__accessor_storage; }
    set errorTitle_(value) { this.#errorTitle__accessor_storage = value; }
    #errorSubtitle__accessor_storage = '';
    get errorSubtitle_() { return this.#errorSubtitle__accessor_storage; }
    set errorSubtitle_(value) { this.#errorSubtitle__accessor_storage = value; }
    #disableProceedButton__accessor_storage = false;
    get disableProceedButton_() { return this.#disableProceedButton__accessor_storage; }
    set disableProceedButton_(value) { this.#disableProceedButton__accessor_storage = value; }
    #currentState__accessor_storage = State.DISCLOSURE;
    get currentState_() { return this.#currentState__accessor_storage; }
    set currentState_(value) { this.#currentState__accessor_storage = value; }
    #appMode__accessor_storage = this.getAppModeFromScreenType(loadTimeData.getInteger('screenType'));
    get appMode_() { return this.#appMode__accessor_storage; }
    set appMode_(value) { this.#appMode__accessor_storage = value; }
    #revampEnabled__accessor_storage = loadTimeData.getBoolean('isFirstRunDesktopRevampEnabled');
    get revampEnabled_() { return this.#revampEnabled__accessor_storage; }
    set revampEnabled_(value) { this.#revampEnabled__accessor_storage = value; }
    #processingSubtitle__accessor_storage = loadTimeData.getString('processingSubtitle');
    get processingSubtitle_() { return this.#processingSubtitle__accessor_storage; }
    set processingSubtitle_(value) { this.#processingSubtitle__accessor_storage = value; }
    #selectedDataHandling__accessor_storage;
    get selectedDataHandling_() { return this.#selectedDataHandling__accessor_storage; }
    set selectedDataHandling_(value) { this.#selectedDataHandling__accessor_storage = value; }
    #usePrimaryAndTonalButtons__accessor_storage = loadTimeData.getBoolean('usePrimaryAndTonalButtonsForPromos');
    get usePrimaryAndTonalButtons_() { return this.#usePrimaryAndTonalButtons__accessor_storage; }
    set usePrimaryAndTonalButtons_(value) { this.#usePrimaryAndTonalButtons__accessor_storage = value; }
    managedUserProfileNoticeBrowserProxy_ = ManagedUserProfileNoticeBrowserProxyImpl.getInstance();
    connectedCallback() {
        super.connectedCallback();
        this.addWebUiListener('on-state-changed', (state) => this.updateCurrentState_(state));
        this.addWebUiListener('on-state-changed-to-error', (errorTitle, errorSubTitle) => {
            this.updateErrorStrings_(errorTitle, errorSubTitle);
            this.updateCurrentState_(State.ERROR);
        });
        this.addWebUiListener('on-profile-info-changed', (info) => this.updateProfileInfo_(info));
        this.addWebUiListener('on-long-processing', () => this.updateProcessingText_());
        this.managedUserProfileNoticeBrowserProxy_.initialized().then(info => {
            this.updateProfileInfo_(info);
            this.updateCurrentState_(loadTimeData.getInteger('initialState'));
            // Prefer using |document.body.offsetHeight| instead of
            // |document.body.scrollHeight| as it returns the correct height of the
            // page even when the page zoom in Chrome is different than 100%.
            this.managedUserProfileNoticeBrowserProxy_.initializedWithSize(document.body.offsetHeight);
        });
    }
    willUpdate(changedProperties) {
        super.willUpdate(changedProperties);
        const changedPrivateProperties = changedProperties;
        if (changedPrivateProperties.has('currentState_')) {
            this.disableProceedButton_ = false;
        }
        if (changedPrivateProperties.has('profileInfo_')) {
            this.selectedDataHandling_ =
                this.profileInfo_.checkLinkDataCheckboxByDefault ?
                    BrowsingDataHandling.MERGE :
                    BrowsingDataHandling.SEPARATE;
        }
        if (changedPrivateProperties.has('currentState_') ||
            changedPrivateProperties.has('profileInfo_')) {
            this.proceedLabel_ = this.computeProceedLabel_();
        }
    }
    /** Called when the proceed button is clicked. */
    onProceedButtonClick_() {
        this.disableProceedButton_ = true;
        const linkData = this.selectedDataHandling_ === BrowsingDataHandling.MERGE;
        this.managedUserProfileNoticeBrowserProxy_.proceed(
        /*currentState=*/ this.currentState_, linkData);
    }
    /** Called when the cancel button is clicked. */
    onCancelButtonClick_() {
        if (this.allowValuePropStateBackFromDisclosure_()) {
            this.updateCurrentState_(State.VALUE_PROPOSITION);
            return;
        }
        this.managedUserProfileNoticeBrowserProxy_.cancel();
    }
    allowValuePropStateBackFromDisclosure_() {
        return this.currentState_ === State.DISCLOSURE &&
            loadTimeData.getInteger('initialState') !== State.DISCLOSURE;
    }
    updateProfileInfo_(info) {
        this.profileInfo_ = info;
    }
    updateCurrentState_(state) {
        this.currentState_ = state;
    }
    getAppModeFromScreenType(screenType) {
        switch (screenType) {
            case ScreenType.PROFILE_PICKER:
            case ScreenType.ENTERPRISE_ACCOUNT_SYNC_ENABLED:
            case ScreenType.ENTERPRISE_ACCOUNT_SYNC_DISABLED:
            case ScreenType.CONSUMER_ACCOUNT_SYNC_DISABLED:
                return AppMode.PROFILE_PICKER;
            case ScreenType.FIRST_RUN:
                return AppMode.FIRST_RUN;
            case ScreenType.ENTERPRISE_ACCOUNT_CREATION:
            case ScreenType.ENTERPRISE_OIDC:
            case ScreenType.DEVICE_SIGNALS_DISCLAIMER:
                assertNotReached();
            default:
                assertNotReachedCase(screenType);
        }
    }
    updateErrorStrings_(errorTitle, errorSubTitle) {
        this.errorTitle_ = errorTitle;
        this.errorSubtitle_ = errorSubTitle;
    }
    allowCancel_() {
        return this.isState_(State.DISCLOSURE) ||
            this.isState_(State.VALUE_PROPOSITION) ||
            this.isState_(State.USER_DATA_HANDLING) ||
            this.isState_(State.TIMEOUT) || this.isState_(State.PROCESSING);
    }
    getCancelLabel_() {
        if (this.isState_(State.VALUE_PROPOSITION) &&
            !loadTimeData.getBoolean('enforcedByPolicy')) {
            return this.i18n('cancelValueProp');
        }
        return this.i18n(this.allowValuePropStateBackFromDisclosure_() ? 'backLabel' :
            'cancelLabel');
    }
    shouldDisableProceedButton_() {
        return this.disableProceedButton_ ||
            (this.isState_(State.USER_DATA_HANDLING) &&
                !this.selectedDataHandling_);
    }
    computeProceedLabel_() {
        switch (this.currentState_) {
            case State.VALUE_PROPOSITION:
                return this.profileInfo_.continueAs;
            case State.DISCLOSURE:
            case State.PROCESSING:
            case State.SUCCESS:
                return this.profileInfo_.proceedLabel || this.i18n('continueLabel');
            case State.USER_DATA_HANDLING:
                return this.i18n('confirmLabel');
            case State.ERROR:
                return this.i18n('closeLabel');
            case State.TIMEOUT:
                return this.i18n('retryLabel');
            default:
                assertNotReachedCase(this.currentState_);
        }
    }
    updateProcessingText_() {
        this.processingSubtitle_ = this.i18n('longProcessingSubtitle');
    }
    onSelectedDataHandlingChanged_(e) {
        this.selectedDataHandling_ = e.detail.value;
    }
    getCancelButtonClass_() {
        return this.usePrimaryAndTonalButtons_ ? 'tonal-button' : '';
    }
    isState_(state) {
        return this.currentState_ === state;
    }
}
customElements.define(ManagedUserProfileNoticeAppRefreshElement.is, ManagedUserProfileNoticeAppRefreshElement);
