// Copyright 2025 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 './composebox_tab_favicon.js';
import { I18nMixinLit } from '//resources/cr_elements/i18n_mixin_lit.js';
import { assert } from '//resources/js/assert.js';
import { loadTimeData } from '//resources/js/load_time_data.js';
import { htmlEscape } from '//resources/js/util.js';
import { CrLitElement } from '//resources/lit/v3_0/lit.rollup.js';
import { recordContextAdditionMethod, TabUploadOrigin } from './common.js';
import { getCss } from './recent_tab_chip.css.js';
import { getHtml } from './recent_tab_chip.html.js';
const RecentTabChipBase = I18nMixinLit(CrLitElement);
export class RecentTabChipElement extends RecentTabChipBase {
    static get is() {
        return 'composebox-recent-tab-chip';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            // =========================================================================
            // Public properties
            // =========================================================================
            recentTab: { type: Object },
        };
    }
    #recentTab_accessor_storage = undefined;
    get recentTab() { return this.#recentTab_accessor_storage; }
    set recentTab(value) { this.#recentTab_accessor_storage = value; }
    delayTabUploads_ = loadTimeData.getBoolean('addTabUploadDelayOnRecentTabChipClick');
    composeboxSource_ = loadTimeData.getString('composeboxSource');
    getRecentTabChipTitle_() {
        if (!this.recentTab) {
            return '';
        }
        const url = new URL(this.recentTab.url);
        const domain = url.hostname.replace(/^www\./, '');
        // Escape the title and domain as they are passed as arguments to i18n(),
        // which uses `parseHtmlSubset` to sanitize the localized string. If the
        // title contains restricted HTML tags (like <style>), parseHtmlSubset will
        // throw an error.
        return `${htmlEscape(this.recentTab.title)} - ${htmlEscape(domain)}`;
    }
    onRecentTabButtonClick_(e) {
        e.stopPropagation();
        assert(this.recentTab);
        chrome.histograms.recordUserAction(`ContextualSearch.RecentTabChipClick.${this.composeboxSource_}`);
        this.fire('add-tab-context', {
            id: this.recentTab.tabId,
            title: this.recentTab.title,
            url: this.recentTab.url,
            delayUpload: this.delayTabUploads_,
            origin: TabUploadOrigin.RECENT_TAB_CHIP,
        });
        recordContextAdditionMethod(3 /* ComposeboxContextAddedMethod.RECENT_TAB_CHIP */, this.composeboxSource_);
    }
}
customElements.define(RecentTabChipElement.is, RecentTabChipElement);
