// 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 '//resources/cr_components/composebox/composebox_dropdown.js';
import '//resources/cr_components/composebox/composebox_file_inputs.js';
import '//resources/cr_components/composebox/composebox_input.js';
import '//resources/cr_components/composebox/composebox_tool_chip.js';
import '//resources/cr_components/composebox/contextual_entrypoint_button.js';
import '//resources/cr_components/composebox/contextual_entrypoint_and_menu.js';
import '//resources/cr_components/composebox/composebox_submit.js';
import '//resources/cr_components/search/animated_glow.js';
import { getLoadTimeBoolean } from '//resources/cr_components/composebox/common.js';
import { ComposeboxEmbedderMixin } from '//resources/cr_components/composebox/composebox_mixin.js';
import { ComposeboxProxyImpl } from '//resources/cr_components/composebox/composebox_proxy.js';
import { GlowAnimationState } from '//resources/cr_components/search/constants.js';
import { CrLitElement } from '//resources/lit/v3_0/lit.rollup.js';
import { ToolMode } from '//resources/mojo/components/omnibox/composebox/composebox_query.mojom-webui.js';
import { getCss } from './composebox.css.js';
import { getHtml } from './composebox.html.js';
export class OmniboxEverywhereComposeboxElement extends ComposeboxEmbedderMixin(CrLitElement) {
    static get is() {
        return 'omnibox-everywhere-composebox';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            applyContextButtonBackground: {
                reflect: true,
                type: Boolean,
            },
            entrypointName: { type: String, reflect: true },
            disableComposeboxAnimation: { type: Boolean },
        };
    }
    #entrypointName_accessor_storage = 'Omnibox';
    get entrypointName() { return this.#entrypointName_accessor_storage; }
    set entrypointName(value) { this.#entrypointName_accessor_storage = value; }
    #disableComposeboxAnimation_accessor_storage = false;
    get disableComposeboxAnimation() { return this.#disableComposeboxAnimation_accessor_storage; }
    set disableComposeboxAnimation(value) { this.#disableComposeboxAnimation_accessor_storage = value; }
    #applyContextButtonBackground_accessor_storage = false;
    get applyContextButtonBackground() { return this.#applyContextButtonBackground_accessor_storage; }
    set applyContextButtonBackground(value) { this.#applyContextButtonBackground_accessor_storage = value; }
    webuiOmniboxSimplificationEnabled_ = getLoadTimeBoolean('webuiOmniboxSimplificationEnabled', false);
    pageHandler_;
    searchboxCallbackRouter_;
    searchboxHandler_;
    constructor() {
        super();
        this.pageHandler_ = ComposeboxProxyImpl.getInstance().handler;
        this.searchboxCallbackRouter_ =
            ComposeboxProxyImpl.getInstance().searchboxCallbackRouter;
        this.searchboxHandler_ = ComposeboxProxyImpl.getInstance().searchboxHandler;
    }
    willUpdate(changedProperties) {
        super.willUpdate(changedProperties);
        if (changedProperties.has('inputState')) {
            const inToolMode = this.inputState?.activeTool !== ToolMode.kUnspecified;
            this.applyContextButtonBackground =
                this.webuiOmniboxSimplificationEnabled_ && !inToolMode;
        }
    }
    firstUpdated(changedProperties) {
        super.firstUpdated(changedProperties);
        this.focusInput();
    }
    async addTabContextHandleCallback(_tabUpload, _replaceAutoActiveTabToken = false, _onBeforeUpdateFiles) {
        // Note: Copied from omnibox_composebox.ts. May need to be implemented
        // fully if adding file carousel.
        // For now, satisfy contract to avoid assertNotReached crashes on state
        // updates.
        return Promise.resolve(null);
    }
    getActiveElement() {
        return this.shadowRoot?.activeElement || null;
    }
    getDropdownElement() {
        return this.$.matches;
    }
    getInputElement() {
        return this.$.composeboxInput;
    }
    getPageHandler() {
        return this.pageHandler_;
    }
    getSearchboxCallbackRouter() {
        return this.searchboxCallbackRouter_;
    }
    getSearchboxHandler() {
        return this.searchboxHandler_;
    }
    getContextEntrypointElement() {
        return this.shadowRoot?.querySelector('#contextEntrypoint') ||
            null;
    }
    addSearchContext(context) {
        if (context) {
            if (context.input.length > 0) {
                this.input = context.input;
            }
            for (const attachment of context.attachments) {
                if (attachment.fileAttachment) {
                    this.addFileFromAttachment_(attachment.fileAttachment);
                }
                else if (attachment.tabAttachment) {
                    this.addTabFromAttachment_(attachment.tabAttachment);
                }
            }
        }
        // Query for ZPS even if there's no context.
        if (this.showZps) {
            this.queryAutocomplete(/* clearMatches= */ false);
        }
    }
    // Note: Copied from omnibox_composebox.ts. May need implementation when
    // carousel is added.
    addFileFromAttachment_(fileAttachment) {
        return fileAttachment;
    }
    // Note: Copied from omnibox_composebox.ts. May need implementation when
    // carousel is added.
    addTabFromAttachment_(tabAttachment) {
        return tabAttachment;
    }
    shouldShowDivider() {
        if (this.searchboxLayoutMode === 'TallBottomContext' &&
            !this.showFileCarousel) {
            return false;
        }
        return super.shouldShowDivider();
    }
    selectFirstMatch() {
        if (this.result?.matches && this.result.matches.length > 0 &&
            this.result.matches[0]?.allowedToBeDefaultMatch &&
            this.selectedMatchIndex !== -1) {
            this.getDropdownElement().selectFirst();
        }
    }
    submitQuery() {
        super.submitQuery();
    }
    hasValidQuery() {
        // If there is at least one file that supports unimodal search, query is
        // valid.
        if (this.files.size > 0 &&
            Array.from(this.files.values()).some(file => file.supportsUnimodal)) {
            return true;
        }
        // If an autocomplete match is selected, it's a valid query.
        if (this.selectedMatchIndex >= 0 && !!this.result) {
            return true;
        }
        if (this.input.trim().length > 0) {
            return true;
        }
        return false;
    }
    playGlowAnimation() {
        this.animationState = GlowAnimationState.NONE;
        requestAnimationFrame(() => {
            this.animationState = GlowAnimationState.EXPANDING;
        });
    }
}
customElements.define(OmniboxEverywhereComposeboxElement.is, OmniboxEverywhereComposeboxElement);
