/**
 * Copyright IBM Corp. 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import "../../globals/settings.js";
import { find } from "../../globals/internal/collection-helpers.js";
import { carbonElement } from "../../globals/decorators/carbon-element.js";
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.126.0/helpers/decorate.js";
import FocusMixin from "../../globals/mixins/focus.js";
import HostListener from "../../globals/decorators/host-listener.js";
import HostListenerMixin from "../../globals/mixins/host-listener.js";
import { OVERFLOW_MENU_SIZE } from "./defs.js";
import icon_button_default from "../icon-button/icon-button.scss.js";
import overflow_menu_default$1 from "./overflow-menu.scss.js";
import icon_button_default$1 from "../icon-button/icon-button.js";
import { adoptStyles, html } from "lit";
import { property } from "lit/decorators.js";
//#region src/components/overflow-menu/overflow-menu.ts
/**
* Copyright IBM Corp. 2019, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Overflow menu.
*
* @element cds-overflow-menu
* @slot icon - The icon for the trigger button.
*/
let CDSOverflowMenu = class CDSOverflowMenu extends HostListenerMixin(FocusMixin(icon_button_default$1)) {
	constructor(..._args) {
		super(..._args);
		this._menuBody = null;
		this._handleClickTrigger = async () => {
			this._handleUserInitiatedToggle();
		};
		this._handleKeydownTrigger = async (event) => {
			if (event.key === " " || event.key === "Enter") {
				this._handleUserInitiatedToggle();
				event.preventDefault();
			}
		};
		this.dataTable = false;
		this.disabled = false;
		this.flipped = false;
		this.open = false;
		this.index = 1;
		this.size = "md";
		this.toolbarAction = false;
		this.breadcrumb = false;
	}
	/**
	* Handles user-initiated toggling of the menu.
	*/
	async _handleUserInitiatedToggle() {
		this.open = !this.open;
		const { index, open, updateComplete } = this;
		if (open) {
			await updateComplete;
			const { _menuBody: menuBody } = this;
			(menuBody?.querySelector(`cds-overflow-menu-item:nth-of-type(${index})`))?.focus();
		}
	}
	/**
	* @returns The position of the trigger button in the viewport.
	*/
	get triggerPosition() {
		return this.getBoundingClientRect();
	}
	connectedCallback() {
		if (!this.hasAttribute("aria-haspopup")) this.setAttribute("aria-haspopup", "true");
		if (!this.shadowRoot) this.attachShadow({ mode: "open" });
		super.connectedCallback();
		adoptStyles(this.renderRoot, [icon_button_default, overflow_menu_default$1]);
	}
	updated(changedProperties) {
		const button = this.shadowRoot?.querySelector(`cds-tooltip`)?.querySelector("button");
		button?.classList.add(`cds--btn--icon-only`, `cds--overflow-menu`);
		if (changedProperties.has("open")) {
			const { open } = this;
			if (open && !this._menuBody) this._menuBody = find(this.childNodes, (elem) => elem.constructor.FLOATING_MENU);
			const { _menuBody: menuBody, size } = this;
			if (menuBody) {
				menuBody.setAttribute("breadcrumb", String(this.breadcrumb));
				menuBody.open = open;
				menuBody.size = size;
				const tooltipContent = this.querySelector("[slot=tooltip-content]")?.textContent;
				button?.setAttribute("aria-expanded", String(open));
				button?.setAttribute("aria-label", String(tooltipContent));
			}
		}
		if (changedProperties.has("dataTable")) (this.shadowRoot?.querySelector(`cds-tooltip`))?.setAttribute("data-table", "");
		if (changedProperties.has("size")) {
			button?.classList.forEach((item) => {
				if (item.startsWith(`cds--overflow-menu--`)) button?.classList.remove(item);
			});
			button?.classList.add(`cds--overflow-menu--${this.size}`);
			(this.shadowRoot?.querySelector(`cds-tooltip`))?.setAttribute("size", this.size);
		}
		if (changedProperties.has("toolbarAction") && this.toolbarAction) this.shadowRoot?.querySelector(`cds-tooltip`)?.setAttribute("toolbar-action", "");
		super.updated(changedProperties);
	}
	render() {
		return html`${super.render()} `;
	}
};
__decorate([HostListener("click")], CDSOverflowMenu.prototype, "_handleClickTrigger", void 0);
__decorate([HostListener("keydown")], CDSOverflowMenu.prototype, "_handleKeydownTrigger", void 0);
__decorate([property({
	type: Boolean,
	reflect: true,
	attribute: "data-table"
})], CDSOverflowMenu.prototype, "dataTable", void 0);
__decorate([property({
	type: Boolean,
	reflect: true
})], CDSOverflowMenu.prototype, "disabled", void 0);
__decorate([property({
	type: Boolean,
	reflect: true
})], CDSOverflowMenu.prototype, "flipped", void 0);
__decorate([property({
	type: Boolean,
	reflect: true
})], CDSOverflowMenu.prototype, "open", void 0);
__decorate([property()], CDSOverflowMenu.prototype, "index", void 0);
__decorate([property({ reflect: true })], CDSOverflowMenu.prototype, "size", void 0);
__decorate([property({
	type: Boolean,
	attribute: "toolbar-action",
	reflect: true
})], CDSOverflowMenu.prototype, "toolbarAction", void 0);
__decorate([property({
	type: Boolean,
	reflect: true
})], CDSOverflowMenu.prototype, "breadcrumb", void 0);
CDSOverflowMenu = __decorate([carbonElement(`cds-overflow-menu`)], CDSOverflowMenu);
var overflow_menu_default = CDSOverflowMenu;
//#endregion
export { OVERFLOW_MENU_SIZE, overflow_menu_default as default };

//# sourceMappingURL=overflow-menu.js.map