// 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 './icons.html.js';
import '//resources/cr_elements/cr_action_menu/cr_action_menu.js';
import '//resources/cr_elements/cr_icon/cr_icon.js';
import '//resources/cr_elements/cr_auto_img/cr_auto_img.js';
import '//resources/cr_elements/cr_url_list_item/cr_url_list_item.js';
import { AnchorAlignment } from '//resources/cr_elements/cr_action_menu/cr_action_menu.js';
import { getFaviconForPageURL } from 'chrome://resources/js/icon.js';
import { CrLitElement } from 'chrome://resources/lit/v3_0/lit.rollup.js';
import { BrowserProxyImpl } from './contextual_tasks_browser_proxy.js';
import { getCss } from './sources_menu.css.js';
import { getHtml } from './sources_menu.html.js';
import { recordAction } from './utils.js';
export class SourcesMenuElement extends CrLitElement {
    static get is() {
        return 'contextual-tasks-sources-menu';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            contextInfos: { type: Array },
        };
    }
    #contextInfos_accessor_storage = [];
    get contextInfos() { return this.#contextInfos_accessor_storage; }
    set contextInfos(value) { this.#contextInfos_accessor_storage = value; }
    browserProxy_ = BrowserProxyImpl.getInstance();
    showAt(target) {
        this.$.menu.showAt(target, {
            noOffset: true,
            anchorAlignmentY: AnchorAlignment.AFTER_END,
        });
    }
    close() {
        this.$.menu.close();
    }
    getContextInfoFromEvent_(e) {
        const index = Number(e.currentTarget.dataset['index']);
        return this.contextInfos[index];
    }
    onTabClick_(e) {
        this.close();
        recordAction('ContextualTasks.WebUI.UserAction.TabFromSourcesMenuClicked');
        const contextInfo = this.getContextInfoFromEvent_(e);
        this.browserProxy_.handler.onTabClickedFromSourcesMenu(contextInfo.tab.tabId, contextInfo.tab.url);
    }
    onFileClick_(e) {
        this.close();
        const contextInfo = this.getContextInfoFromEvent_(e);
        this.browserProxy_.handler.onFileClickedFromSourcesMenu(contextInfo.file.url);
    }
    onImageClick_(e) {
        this.close();
        const contextInfo = this.getContextInfoFromEvent_(e);
        this.browserProxy_.handler.onImageClickedFromSourcesMenu(contextInfo.image.url);
    }
    faviconUrl_(url) {
        return getFaviconForPageURL(url, false);
    }
    getHostname_(url) {
        try {
            return new URL(url).hostname;
        }
        catch (e) {
            return url;
        }
    }
}
customElements.define(SourcesMenuElement.is, SourcesMenuElement);
