// 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_elements/cr_icon_button/cr_icon_button.js';
import '//resources/cr_elements/icons.html.js';
import { loadTimeData } from '//resources/js/load_time_data.js';
import { CrLitElement } from '//resources/lit/v3_0/lit.rollup.js';
import { getHtml } from './back_forward_button.html.js';
import { BrowserProxyImpl, ContextMenuType } from './browser_proxy.js';
import { getCss } from './toolbar_button.css.js';
import { getContextMenuPosition, getEventDispositionFlags, HelpBubbleAnchorMixin, PressHandler, roundedIconsEnabled } from './toolbar_button.js';
const BackForwardButtonElementBase = HelpBubbleAnchorMixin(CrLitElement);
export class BackForwardButtonElement extends BackForwardButtonElementBase {
    static get is() {
        return 'back-forward-button';
    }
    static get styles() {
        return getCss();
    }
    render() {
        return getHtml.bind(this)();
    }
    static get properties() {
        return {
            ...super.properties,
            direction: { type: String },
            state: { type: Object },
            leadingMargin: { type: Number },
            touchUi: { type: Boolean },
        };
    }
    #direction_accessor_storage = 'back';
    get direction() { return this.#direction_accessor_storage; }
    set direction(value) { this.#direction_accessor_storage = value; }
    #state_accessor_storage = {
        enabled: false,
        shouldBeShown: true,
        isContextMenuVisible: false,
    };
    get state() { return this.#state_accessor_storage; }
    set state(value) { this.#state_accessor_storage = value; }
    #leadingMargin_accessor_storage = 0;
    get leadingMargin() { return this.#leadingMargin_accessor_storage; }
    set leadingMargin(value) { this.#leadingMargin_accessor_storage = value; }
    #touchUi_accessor_storage = false;
    get touchUi() { return this.#touchUi_accessor_storage; }
    set touchUi(value) { this.#touchUi_accessor_storage = value; }
    browserProxy_ = BrowserProxyImpl.getInstance();
    pressHandler_ = new PressHandler(this.onLongPress_.bind(this), this.onShortPress_.bind(this));
    get contextMenuType_() {
        return this.direction === 'back' ? ContextMenuType.kBack :
            ContextMenuType.kForward;
    }
    onLongPress_(source) {
        this.browserProxy_.toolbarUIHandler.showContextMenu(this.contextMenuType_, getContextMenuPosition(this), source);
    }
    onShortPress_(e) {
        const flags = getEventDispositionFlags(e);
        if (this.direction === 'back') {
            this.browserProxy_.browserControlsHandler.back(flags);
        }
        else {
            this.browserProxy_.browserControlsHandler.forward(flags);
        }
    }
    getAriaLabel_() {
        return this.direction === 'back' ?
            loadTimeData.getString('backButtonAccName') :
            loadTimeData.getString('forwardButtonAccName');
    }
    getTooltip_() {
        return this.adjustTooltipForHelpBubble(this.direction === 'back' ?
            loadTimeData.getString('backButtonTooltip') :
            loadTimeData.getString('forwardButtonTooltip'));
    }
    onPointerenter_() {
        if (this.direction === 'back') {
            this.browserProxy_.browserControlsHandler.backButtonHovered();
        }
    }
    onClick_(e) {
        // Only handle keyboard 'click', which triggers a left-click equivalent.
        // Other events like mouse 'click' are handled in onShortPress_.
        if (e.detail === 0) {
            this.onShortPress_(e);
        }
    }
    getIronIcon_() {
        if (this.direction === 'back') {
            if (roundedIconsEnabled()) {
                return 'webui-toolbar:arrow_back';
            }
            else {
                return this.touchUi ? 'webui-toolbar:back_arrow_touch_old' :
                    'webui-toolbar:back_arrow_chrome_refresh_old';
            }
        }
        else {
            if (roundedIconsEnabled()) {
                return 'webui-toolbar:arrow_forward';
            }
            else {
                return this.touchUi ? 'webui-toolbar:forward_arrow_touch_old' :
                    'webui-toolbar:forward_arrow_chrome_refresh_old';
            }
        }
    }
}
customElements.define(BackForwardButtonElement.is, BackForwardButtonElement);
