// 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_link_row/cr_link_row.js';
import '../settings_page/settings_section.js';
// 
import { PrefsMixin } from '/shared/settings/prefs/prefs_mixin.js';
import { assert, assertNotReached } from 'chrome://resources/js/assert.js';
import { OpenWindowProxyImpl } from 'chrome://resources/js/open_window_proxy.js';
import { PolymerElement } from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import { loadTimeData } from '../i18n_setup.js';
import { AiPageInteractions, MetricsBrowserProxyImpl } from '../metrics_browser_proxy.js';
import { routes } from '../route.js';
import { Router } from '../router.js';
import { SettingsViewMixin } from '../settings_page/settings_view_mixin.js';
import { getTemplate } from './ai_page.html.js';
import { FeatureOptInState, SettingsAiPageFeaturePrefName } from './constants.js';
const SettingsAiPageElementBase = SettingsViewMixin(PrefsMixin(PolymerElement));
export class SettingsAiPageElement extends SettingsAiPageElementBase {
    static get is() {
        return 'settings-ai-page';
    }
    static get template() {
        return getTemplate();
    }
    static get properties() {
        return {
            showComposeControl_: {
                type: Boolean,
                value: () => loadTimeData.getBoolean('showComposeControl'),
            },
            showHistorySearchControl_: {
                type: Boolean,
                value: () => loadTimeData.getBoolean('showHistorySearchControl'),
            },
            showPasswordChangeControl_: {
                type: Boolean,
                value: () => loadTimeData.getBoolean('showPasswordChangeControl'),
            },
            showAiSuggestionsControl_: {
                type: Boolean,
                value: () => loadTimeData.getBoolean('showAiSuggestionsControl'),
            },
            showSkillsSettingPage_: {
                type: Boolean,
                value: () => loadTimeData.getBoolean('showSkillsSettingPage'),
            },
            showIndigoControl_: {
                type: Boolean,
                value: () => loadTimeData.getBoolean('showIndigoControl'),
            },
            showGoogleSearchAiModeWorkspaceControl_: {
                type: Boolean,
                value: () => loadTimeData.getBoolean('showGoogleSearchAiModeWorkspaceControl'),
            },
        };
    }
    shouldRecordMetrics_ = true;
    metricsBrowserProxy_ = MetricsBrowserProxyImpl.getInstance();
    connectedCallback() {
        super.connectedCallback();
        this.maybeLogVisibilityMetrics_();
    }
    maybeLogVisibilityMetrics_() {
        // Only record metrics when the user first navigates to the main AI page.
        if (!this.shouldRecordMetrics_ ||
            Router.getInstance().getCurrentRoute() !== routes.AI) {
            return;
        }
        this.shouldRecordMetrics_ = false;
        this.metricsBrowserProxy_.recordBooleanHistogram('Settings.AiPage.ElementVisibility.HistorySearch', this.showHistorySearchControl_);
        this.metricsBrowserProxy_.recordBooleanHistogram('Settings.AiPage.ElementVisibility.Compose', this.showComposeControl_);
        this.metricsBrowserProxy_.recordBooleanHistogram('Settings.AiPage.ElementVisibility.PasswordChange', this.showPasswordChangeControl_);
        this.metricsBrowserProxy_.recordBooleanHistogram('Settings.AiPage.ElementVisibility.AiSuggestions', this.showAiSuggestionsControl_);
        this.metricsBrowserProxy_.recordBooleanHistogram('Settings.AiPage.ElementVisibility.Indigo', this.showIndigoControl_);
        this.metricsBrowserProxy_.recordBooleanHistogram('Settings.AiPage.ElementVisibility.GoogleSearchAiModeWorkspace', this.showGoogleSearchAiModeWorkspaceControl_);
    }
    onHistorySearchRowClick_() {
        this.recordInteractionMetrics_(AiPageInteractions.HISTORY_SEARCH_CLICK, 'Settings.AiPage.HistorySearchEntryPointClick');
        const router = Router.getInstance();
        router.navigateTo(router.getRoutes().HISTORY_SEARCH);
    }
    onComposeRowClick_() {
        this.recordInteractionMetrics_(AiPageInteractions.COMPOSE_CLICK, 'Settings.AiPage.ComposeEntryPointClick');
        const router = Router.getInstance();
        router.navigateTo(router.getRoutes().OFFER_WRITING_HELP);
    }
    onPasswordChangeRowClick_() {
        this.recordInteractionMetrics_(AiPageInteractions.PASSWORD_CHANGE_CLICK, 'Settings.AiPage.PasswordChangeEntryPointClick');
        OpenWindowProxyImpl.getInstance().openUrl(loadTimeData.getString('passwordChangeSettingsUrl'));
    }
    onAiSuggestionsRowClick_() {
        this.recordInteractionMetrics_(AiPageInteractions.AI_SUGGESTIONS_CLICK, 'Settings.AiPage.AiSuggestionsEntryPointClick');
        const router = Router.getInstance();
        router.navigateTo(router.getRoutes().AI_SUGGESTIONS);
    }
    onSkillsRowClick_() {
        this.recordInteractionMetrics_(AiPageInteractions.SKILLS_CLICK, 'Settings.AiPage.SkillsEntryPointClick');
        const router = Router.getInstance();
        router.navigateTo(router.getRoutes().SKILLS);
    }
    onIndigoRowClick_() {
        this.recordInteractionMetrics_(AiPageInteractions.INDIGO_CLICK, 'Settings.AiPage.IndigoEntryPointClick');
        OpenWindowProxyImpl.getInstance().openUrl(loadTimeData.getString('indigoSavedUrl'));
    }
    onGoogleSearchAiModeWorkspaceRowClick_() {
        this.recordInteractionMetrics_(AiPageInteractions.GOOGLE_SEARCH_AI_MODE_WORKSPACE_CLICK, 'Settings.AiPage.GoogleSearchAiModeWorkspaceEntryPointClick');
        let isRestricted = false;
        try {
            const consentState = this.getPref('contextual_search.drive_consent_state').value;
            isRestricted = consentState === 1; // DriveConsentState::kRestricted
        }
        catch (e) {
            console.error('Failed to read contextual_search.drive_consent_state pref:', e);
        }
        let url;
        try {
            url = loadTimeData.getString(isRestricted ? 'googleSearchAiModeRestrictedUrl' :
                'googleSearchAiModeWorkspaceUrl');
        }
        catch (e) {
            console.error('Failed to read URL from loadTimeData:', e);
            return;
        }
        OpenWindowProxyImpl.getInstance().openUrl(url);
    }
    recordInteractionMetrics_(interaction, action) {
        this.metricsBrowserProxy_.recordAiPageInteractions(interaction);
        this.metricsBrowserProxy_.recordAction(action);
    }
    getHistorySearchSublabel_() {
        const isAnswersEnabled = loadTimeData.getBoolean('historyEmbeddingsAnswersFeatureEnabled');
        if (this.getPref(SettingsAiPageFeaturePrefName.HISTORY_SEARCH).value ===
            FeatureOptInState.ENABLED) {
            return isAnswersEnabled ?
                loadTimeData.getString('historySearchWithAnswersSublabelOn') :
                loadTimeData.getString('historySearchSublabelOn');
        }
        return isAnswersEnabled ?
            loadTimeData.getString('historySearchWithAnswersSublabelOff') :
            loadTimeData.getString('historySearchSublabelOff');
    }
    // SettingsViewMixin implementation.
    getFocusConfig() {
        const map = new Map();
        if (routes.HISTORY_SEARCH) {
            map.set(routes.HISTORY_SEARCH.path, '#historySearchRowV2');
        }
        if (routes.OFFER_WRITING_HELP) {
            map.set(routes.OFFER_WRITING_HELP.path, '#composeRowV2');
        }
        if (routes.AI_SUGGESTIONS) {
            map.set(routes.AI_SUGGESTIONS.path, '#aiSuggestionsRow');
        }
        if (routes.SKILLS) {
            map.set(routes.SKILLS.path, '#skillsRow');
        }
        return map;
    }
    // SettingsViewMixin implementation.
    getAssociatedControlFor(childViewId) {
        const ids = [
            'compose',
            'historySearch',
            'aiSuggestions',
            'skills',
        ];
        assert(ids.includes(childViewId));
        let triggerId = null;
        switch (childViewId) {
            case 'compose':
                assert(this.showComposeControl_);
                triggerId = 'composeRowV2';
                break;
            case 'historySearch':
                assert(this.showHistorySearchControl_);
                triggerId = 'historySearchRowV2';
                break;
            case 'aiSuggestions':
                assert(this.showAiSuggestionsControl_);
                triggerId = 'aiSuggestionsRow';
                break;
            case 'skills':
                assert(this.showSkillsSettingPage_);
                triggerId = 'skillsRow';
                break;
            default:
                assertNotReached();
        }
        assert(triggerId);
        const control = this.shadowRoot.querySelector(`#${triggerId}`);
        assert(control, `Failed to find associated control for child '${childViewId}'`);
        return control;
    }
}
customElements.define(SettingsAiPageElement.is, SettingsAiPageElement);
