/**
 * @license
 * Copyright (c) 2019 - 2026 Vaadin Ltd.
 * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
 */
import { Button } from '@vaadin/button/src/vaadin-button.js';
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
import { menuBarButtonStyles } from './styles/vaadin-menu-bar-button-base-styles.js';

/**
 * An element used internally by `<vaadin-menu-bar>`. Not intended to be used separately.
 *
 * @customElement vaadin-menu-bar-button
 * @extends Button
 * @private
 */
class MenuBarButton extends Button {
  static get is() {
    return 'vaadin-menu-bar-button';
  }

  static get styles() {
    return [super.styles, menuBarButtonStyles];
  }

  /**
   * Override method inherited from `ButtonMixin` to set a flag based on whether the key
   * is Enter and Space. Unlike mouse click, these keys should also focus the first item.
   * This flag is used in menu bar to identify the action that triggered the click.
   *
   * @param {KeyboardEvent} event
   * @protected
   * @override
   */
  _onKeyDown(event) {
    this.__triggeredWithActiveKeys = this._activeKeys.includes(event.key);
    super._onKeyDown(event);
    this.__triggeredWithActiveKeys = null;
  }

  /**
   * Override to preserve the `active` attribute while the button's
   * sub-menu is expanded, so that the pressed visual state remains.
   *
   * @param {boolean} active
   * @protected
   * @override
   */
  _setActive(active) {
    if (!active && this.hasAttribute('expanded')) {
      return;
    }
    super._setActive(active);
  }

  /**
   * Override method inherited from `ButtonMixin` to allow keyboard navigation with
   * arrow keys in the menu bar when the button is focusable in the disabled state.
   *
   * @override
   */
  __shouldSuppressInteractionEvent(event) {
    if (event.type === 'keydown' && ['ArrowLeft', 'ArrowRight'].includes(event.key)) {
      return false;
    }

    return super.__shouldSuppressInteractionEvent(event);
  }
}

defineCustomElement(MenuBarButton);

export { MenuBarButton };
