/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { C as CSS_UTILITY, c as customElement } from "../../chunks/runtime.js";
import { css, html } from "lit";
import { keyed } from "lit/directives/keyed.js";
import { createRef, ref } from "lit/directives/ref.js";
import { LitElement, createEvent, safeClassMap, nothing } from "@arcgis/lumina";
import { useDirection } from "@arcgis/lumina/controllers";
import { a as slotChangeGetAssignedElements } from "../../chunks/dom.js";
import { u as useT9n } from "../../chunks/useT9n.js";
import { u as useSetFocus } from "../../chunks/useSetFocus.js";
const CSS = {
  container: "container",
  content: "content",
  dropdownVertical: "dropdown--vertical",
  dropdownMenuItems: "dropdown-menu-items",
  dropdownAction: "dropdown-action",
  layoutVertical: "layout--vertical",
  hoverHrefIcon: "hover-href-icon",
  icon: "icon",
  iconBreadcrumb: "icon--breadcrumb",
  iconDropdown: "icon--dropdown",
  iconEnd: "icon--end",
  iconStart: "icon--start",
  isParentVertical: "parent--vertical",
  itemContent: "item-content",
  open: "open",
  nested: "nested",
  textContainer: "text-container"
};
const SLOTS = {
  submenuItem: "submenu-item"
};
const ICONS = {
  arrowLeft: "arrow-left",
  arrowRight: "arrow-right",
  chevronLeft: "chevron-left",
  chevronRight: "chevron-right",
  chevronUp: "chevron-up",
  chevronDown: "chevron-down"
};
const styles = css`:host{--calcite-internal-menu-item-border: 1px solid var(--calcite-menu-item-sub-menu-border-color, var(--calcite-color-border-3));position:relative;box-sizing:border-box;display:flex;align-items:center;flex-shrink:0}:host .container,:host .item-content,:host .content{min-block-size:3rem}:host([layout=vertical]){inline-size:100%}:host(:not([layout=vertical])){block-size:100%}.container,.item-content{display:flex;block-size:100%;inline-size:100%;flex-direction:row;align-items:stretch}.item-content{background-color:var(--calcite-menu-background-color, var(--calcite-internal-menu-background-color, var(--calcite-color-foreground-1)))}.content{position:relative;box-sizing:border-box;display:flex;block-size:100%;inline-size:100%;cursor:pointer;align-items:center;justify-content:center;padding-inline:1rem;font-size:var(--calcite-font-size-relative-md);outline:2px solid transparent;outline-offset:2px;text-decoration:none;padding-block-start:.125rem;border-block-end:.125rem solid var(--calcite-color-transparent);background-color:var(--calcite-menu-background-color, var(--calcite-internal-menu-background-color, var(--calcite-color-foreground-1)));color:var(--calcite-menu-text-color, var(--calcite-internal-menu-text-color, var(--calcite-color-text-2)))}.content:hover{--calcite-internal-menu-background-color: var(--calcite-color-foreground-2)}.content:focus{border-block-end-width:4px;outline:var(--calcite-border-width-md) solid var(--calcite-color-focus, var(--calcite-ui-focus-color, var(--calcite-color-brand)));outline-offset:calc(calc(-1 * var(--calcite-spacing-base)) * calc(1 - (2*clamp(0,var(--calcite-offset-invert-focus),1))));padding-block-start:.25rem;border-block-end-width:.25rem}.content:active{--calcite-internal-menu-background-color: var(--calcite-color-foreground-3);--calcite-internal-menu-text-color: var(--calcite-color-text-1)}.content span{display:inline-flex}.content.layout--vertical{display:flex;inline-size:100%;justify-content:flex-start;padding-block:var(--calcite-space-md);border-block-end:0;border-inline-end:.25rem solid var(--calcite-color-transparent)}:host([layout=vertical]) .content{padding-inline:.75rem}:host([active]) .content{--calcite-internal-menu-text-color: var(--calcite-color-text-1);border-color:var(--calcite-menu-item-accent-color, var(--calcite-color-brand))}:host([active]) .icon{--calcite-internal-menu-item-icon-color: var( --calcite-icon-color, var(--calcite-ui-icon-color, var(--calcite-color-brand)) )}.icon{color:var(--calcite-menu-text-color, var(--calcite-icon-color, var(--calcite-internal-menu-item-icon-color, var(--calcite-ui-icon-color, var(--calcite-color-text-3)))))}.icon--start{margin-inline-end:.75rem}.icon--end{margin-inline-start:.75rem}:host([layout=vertical]) .icon--end{margin-inline-start:auto;padding-inline-start:.75rem}.icon--dropdown{position:relative;margin-inline-start:auto;margin-inline-end:0px;padding-inline-start:.5rem}:host([layout=vertical]) .icon--end~.icon--dropdown{margin-inline-start:.75rem}:host([layout=vertical]) .hover-href-icon{padding-inline-start:.5rem}:host([layout=vertical]) .hover-href-icon~.icon--end{margin-inline-start:.5rem}:host([layout=vertical]) .hover-href-icon~.icon--breadcrumb{margin-inline-start:.75rem}.icon--breadcrumb{margin-inline-end:0px;padding-inline-start:.5rem}:host([layout=vertical]) .icon--breadcrumb{margin-inline-start:auto}:host([layout=vertical]) .icon--breadcrumb~.icon--dropdown{margin-inline-start:.5rem}:host([layout=vertical]) .icon--end~.icon--breadcrumb{margin-inline-start:.5rem}:host([breadcrumb]) .content{padding-inline-end:.75rem}.dropdown-action{position:relative;align-self:center;--calcite-action-background-color: var(--calcite-menu-background-color);--calcite-action-text-color: var(--calcite-menu-text-color);margin-inline-start:var(--calcite-spacing-xxs)}.dropdown-action:after{position:absolute;display:block;inline-size:1px;content:"";inset-inline-start:calc(-1 * var(--calcite-spacing-xxs));inset-block:var(--calcite-spacing-xxs);background-color:var(--calcite-color-border-3)}.dropdown-action:active{--calcite-action-background-color-press: var(--calcite-menu-background-color)}:host([layout=vertical]) .dropdown-action{margin-inline-end:var(--calcite-spacing-xxs)}.dropdown-menu-items{position:absolute;display:none;block-size:auto;min-inline-size:100%;flex-direction:column;overflow:visible;box-shadow:var(--calcite-shadow-md);border:var(--calcite-internal-menu-item-border);border-radius:var(--calcite-menu-item-sub-menu-corner-radius, var(--calcite-corner-radius));inset-block-start:100%;z-index:var(--calcite-z-index-dropdown)}.dropdown-menu-items.open{display:block}.dropdown-menu-items.nested{position:absolute;inset-block-start:-1px;transform:translate(calc(100% - 2px))}.parent--vertical{flex-direction:column}.dropdown--vertical.dropdown-menu-items{position:relative;box-shadow:none;inset-block-start:0;transform:none}.dropdown--vertical.dropdown-menu-items:last-of-type{border-inline:0}:host([layout=vertical]:last-of-type) .dropdown-menu-items{border-block-end:0}:host([layout=vertical]) .dropdown--vertical.dropdown-menu-items{border:none}:host([layout=vertical]) .dropdown--vertical.dropdown-menu-items:after{content:"";border-block-start:var(--calcite-internal-menu-item-border);position:absolute;inset-block-start:0;inset-inline:var(--calcite-space-md) 0}:host([slot=submenu-item]) .parent--vertical{padding-inline-start:1.5rem}.dropdown-menu-items.nested.calcite--rtl{transform:translate(calc(-100% + 2px))}.dropdown--vertical.dropdown-menu-items.nested.calcite--rtl{transform:none}.hover-href-icon{position:relative;inset-inline-end:.25rem;margin-inline-start:auto;opacity:0;transition:all var(--calcite-internal-animation-timing-medium) ease-in-out}.content:focus .hover-href-icon,.content:hover .hover-href-icon{inset-inline-end:-.25rem;opacity:1}:host([hidden]){display:none}[hidden]{display:none}`;
class MenuItem extends LitElement {
  constructor() {
    super();
    this.anchorRef = createRef();
    this.direction = useDirection();
    this.dropdownActionRef = createRef();
    this.isFocused = false;
    this.messages = useT9n();
    this.focusSetter = useSetFocus()(this);
    this.hasSubmenu = false;
    this.active = false;
    this.breadcrumb = false;
    this.isTopLevelItem = false;
    this.open = false;
    this.calciteInternalMenuItemKeyEvent = createEvent();
    this.calciteMenuItemSelect = createEvent();
    this.listenOn(window, "click", this.handleClickOut);
    this.listen("focusout", this.handleFocusOut);
    this.listen("blur", this.blurHandler);
    this.listen("focus", this.focusHandler);
  }
  static {
    this.properties = { hasSubmenu: [16, {}, { state: true }], submenuItems: [16, {}, { state: true }], active: [7, {}, { reflect: true, type: Boolean }], breadcrumb: [7, {}, { reflect: true, type: Boolean }], href: 1, iconEnd: [3, { type: String }, { reflect: true }], iconFlipRtl: [3, {}, { reflect: true }], iconStart: [3, { type: String }, { reflect: true }], isTopLevelItem: [5, {}, { type: Boolean }], label: 1, layout: [3, {}, { reflect: true }], messageOverrides: [0, {}, { attribute: false }], open: [7, {}, { reflect: true, type: Boolean }], rel: [3, {}, { reflect: true }], target: [3, {}, { reflect: true }], text: 1, topLevelMenuLayout: 1 };
  }
  static {
    this.styles = styles;
  }
  async setFocus(options) {
    return this.focusSetter(() => this.anchorRef.value, options);
  }
  handleClickOut(event) {
    if (this.topLevelMenuLayout !== "vertical" && this.hasSubmenu && this.open && !event.composedPath().includes(this.el)) {
      this.open = false;
    }
  }
  handleFocusOut(event) {
    if (this.topLevelMenuLayout !== "vertical" && !this.el.contains(event.relatedTarget)) {
      this.open = false;
    }
  }
  blurHandler() {
    this.isFocused = false;
  }
  clickHandler(event) {
    if (this.href && event.target === this.dropdownActionRef.value || !this.href && this.hasSubmenu) {
      this.open = !this.open;
    }
    this.selectMenuItem(event);
  }
  focusHandler(event) {
    const target = event.target;
    this.isFocused = true;
    if (target.open && !this.open) {
      target.open = false;
    }
  }
  handleMenuItemSlotChange(event) {
    this.submenuItems = slotChangeGetAssignedElements(event);
    this.submenuItems.forEach((item) => {
      if (!item.topLevelMenuLayout) {
        item.topLevelMenuLayout = this.topLevelMenuLayout;
      }
    });
    this.hasSubmenu = this.submenuItems.length > 0;
  }
  async keyDownHandler(event) {
    const { hasSubmenu, href, layout, open, submenuItems } = this;
    const key = event.key;
    const targetIsDropdown = event.target === this.dropdownActionRef.value;
    if (event.defaultPrevented) {
      return;
    }
    if (key === " " || key === "Enter") {
      if (hasSubmenu && (!href || href && targetIsDropdown)) {
        this.open = !open;
      }
      if (!(href && targetIsDropdown) && key !== "Enter") {
        this.selectMenuItem(event);
      }
      if (key === " " || href && targetIsDropdown) {
        event.preventDefault();
      }
    } else if (key === "Escape") {
      if (open) {
        this.open = false;
        return;
      }
      this.calciteInternalMenuItemKeyEvent.emit({ event });
      event.preventDefault();
    } else if (key === "ArrowDown" || key === "ArrowUp") {
      event.preventDefault();
      if ((targetIsDropdown || !href) && hasSubmenu && !open && layout === "horizontal") {
        this.open = true;
        return;
      }
      this.calciteInternalMenuItemKeyEvent.emit({
        event,
        children: submenuItems,
        isSubmenuOpen: open && hasSubmenu
      });
    } else if (key === "ArrowLeft") {
      event.preventDefault();
      this.calciteInternalMenuItemKeyEvent.emit({
        event,
        children: submenuItems,
        isSubmenuOpen: true
      });
    } else if (key === "ArrowRight") {
      event.preventDefault();
      if ((targetIsDropdown || !href) && hasSubmenu && !open && layout === "vertical") {
        this.open = true;
        return;
      }
      this.calciteInternalMenuItemKeyEvent.emit({
        event,
        children: submenuItems,
        isSubmenuOpen: open && hasSubmenu
      });
    }
  }
  selectMenuItem(event) {
    if (event.target !== this.dropdownActionRef.value) {
      this.calciteMenuItemSelect.emit();
    }
  }
  renderIconStart() {
    return keyed(CSS.iconStart, html`<calcite-icon class=${`${CSS.icon} ${CSS.iconStart}`} .flipRtl=${this.iconFlipRtl === "start" || this.iconFlipRtl === "both"} .icon=${this.iconStart} scale=s></calcite-icon>`);
  }
  renderIconEnd() {
    return keyed(CSS.iconEnd, html`<calcite-icon class=${`${CSS.icon} ${CSS.iconEnd}`} .flipRtl=${this.iconFlipRtl === "end" || this.iconFlipRtl === "both"} .icon=${this.iconEnd} scale=s></calcite-icon>`);
  }
  renderBreadcrumbIcon(dir) {
    return keyed(CSS.iconBreadcrumb, html`<calcite-icon class=${`${CSS.icon} ${CSS.iconBreadcrumb}`} .icon=${dir === "rtl" ? ICONS.chevronLeft : ICONS.chevronRight} scale=s></calcite-icon>`);
  }
  renderDropdownIcon(dir) {
    const dirChevron = dir === "rtl" ? "chevron-left" : "chevron-right";
    return keyed(CSS.iconDropdown, html`<calcite-icon class=${`${CSS.icon} ${CSS.iconDropdown}`} .icon=${this.topLevelMenuLayout === "vertical" || this.isTopLevelItem ? this.open ? ICONS.chevronUp : ICONS.chevronDown : dirChevron} scale=s></calcite-icon>`);
  }
  renderDropdownAction(dir) {
    const dirChevron = dir === "rtl" ? "chevron-left" : "chevron-right";
    return keyed(CSS.dropdownAction, html`<calcite-action .aria=${{ expanded: this.open }} class=${safeClassMap(CSS.dropdownAction)} .icon=${this.topLevelMenuLayout === "vertical" || this.isTopLevelItem ? this.open ? ICONS.chevronUp : ICONS.chevronDown : dirChevron} @click=${this.clickHandler} @keydown=${this.keyDownHandler} .text=${this.messages.open} ${ref(this.dropdownActionRef)}></calcite-action>`);
  }
  renderSubmenuItems(dir) {
    return html`<calcite-menu class=${safeClassMap({
      [CSS.dropdownMenuItems]: true,
      [CSS.open]: this.open,
      [CSS.nested]: !this.isTopLevelItem,
      [CSS_UTILITY.rtl]: dir === "rtl",
      [CSS.dropdownVertical]: this.topLevelMenuLayout === "vertical"
    })} .label=${this.messages.submenu} layout=vertical role=menu><slot name=${SLOTS.submenuItem} @slotchange=${this.handleMenuItemSlotChange}></slot></calcite-menu>`;
  }
  renderHrefIcon(dir) {
    return keyed(CSS.hoverHrefIcon, html`<calcite-icon class=${safeClassMap(CSS.hoverHrefIcon)} .icon=${dir === "rtl" ? ICONS.arrowLeft : ICONS.arrowRight} scale=s></calcite-icon>`);
  }
  renderItemContent(dir) {
    const hasHref = this.href && (this.topLevelMenuLayout === "vertical" || !this.isTopLevelItem);
    return html`${this.iconStart && this.renderIconStart() || ""}<div class=${safeClassMap(CSS.textContainer)}><span>${this.text}</span></div>${hasHref && this.renderHrefIcon(dir) || ""}${this.iconEnd && this.renderIconEnd() || ""}${this.breadcrumb ? this.renderBreadcrumbIcon(dir) : null}${!this.href && this.hasSubmenu ? this.renderDropdownIcon(dir) : null}`;
  }
  render() {
    const dir = this.direction;
    return html`<li class=${safeClassMap({
      [CSS.container]: true,
      [CSS.isParentVertical]: this.topLevelMenuLayout === "vertical"
    })} role=none><div class=${safeClassMap(CSS.itemContent)}><a .ariaCurrent=${this.isFocused ? "page" : false} .ariaExpanded=${this.open} .ariaHasPopup=${this.hasSubmenu} .ariaLabel=${this.label} class=${safeClassMap({ [CSS.layoutVertical]: this.layout === "vertical", [CSS.content]: true })} href=${this.href ?? nothing} @click=${this.clickHandler} @keydown=${this.keyDownHandler} rel=${this.rel ?? nothing} role=menuitem .tabIndex=${this.isTopLevelItem ? 0 : -1} target=${this.target ?? nothing} ${ref(this.anchorRef)}>${this.renderItemContent(dir)}</a>${this.href && this.hasSubmenu ? this.renderDropdownAction(dir) : null}</div>${this.renderSubmenuItems(dir)}</li>`;
  }
}
customElement("calcite-menu-item", MenuItem);
export {
  MenuItem
};
