/**
 * 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 { carbonElement } from "../../globals/decorators/carbon-element.js";
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.126.0/helpers/decorate.js";
import HostListener from "../../globals/decorators/host-listener.js";
import modal_default from "../modal/modal.js";
import dialog_default$1 from "./dialog.scss.js";
import { html } from "lit";
import { property, query } from "lit/decorators.js";
import { ifDefined } from "lit/directives/if-defined.js";
//#region src/components/dialog/dialog.ts
/**
* 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.
*/
/**
* Dialog.
*
* @see This component is in {@link https://web-components.carbondesignsystem.com/?path=/docs/preview-about-preview--about-preview|preview status}
*
* @element cds-dialog
* @fires cds-dialog-beingclosed
*   The custom event fired before this dialog is being closed upon a user gesture.
*   Cancellation of this event stops the user-initiated action of closing this dialog.
* @fires cds-dialog-closed - The custom event fired after this dialog is closed upon a user gesture.
*/
let CDSDialog = class CDSDialog extends modal_default {
	constructor(..._args) {
		super(..._args);
		this._handleClick = (event) => {
			if (this.open && this.modal && event.composedPath()[0] === this._dialogElement && !this.preventCloseOnClickOutside) this._handleUserInitiatedClose(event.target);
		};
		this.loadingStatus = "inactive";
		this.modal = true;
		this.ariaLabel = null;
		this.ariaLabelledBy = null;
		this.ariaDescribedBy = null;
		this.role = "dialog";
		this.preventCloseOnClickOutside = false;
		this._handleHostKeydown = () => {};
		this._handleKeydown = (event) => {
			const { key, target } = event;
			if (!this.modal || !this.open) return;
			if (key === "Esc" || key === "Escape") {
				event.preventDefault();
				this._handleUserInitiatedClose(target);
			}
		};
	}
	/**
	* Handles `slotchange` event.
	*/
	_handleSlotChange() {
		if (this.querySelector(`cds-dialog-footer`)) this.setAttribute("has-footer", "");
		else this.removeAttribute("has-footer");
		this.requestUpdate();
	}
	/**
	* Gets footer elements.
	*/
	_getFooterElements() {
		return {
			footer: this.querySelector(`cds-dialog-footer`),
			primaryButton: this.querySelector(`cds-dialog-footer-button[kind="primary"]`) || this.querySelector(`cds-dialog-footer-button[kind="danger"]`) || null,
			secondaryButtons: Array.from(this.querySelectorAll(`cds-dialog-footer-button[kind="secondary"]`))
		};
	}
	async updated(changedProperties) {
		if (changedProperties.has("open")) if (this.open) {
			this._launcher = this.ownerDocument.activeElement;
			if (this._dialogElement && !this.ariaLabel && !this.ariaLabelledBy) {
				const title = this.querySelector(`cds-dialog-title`);
				if (title && title.id) this._dialogElement.setAttribute("aria-labelledby", title.id);
			}
			if (this._dialogElement) if (this.modal) this._dialogElement.showModal();
			else this._dialogElement.show();
			const primaryFocusNode = this.querySelector(this.constructor.selectorPrimaryFocus);
			await this.constructor._delay();
			if (primaryFocusNode) primaryFocusNode.focus();
			else {
				const { primaryButton, secondaryButtons } = this._getFooterElements();
				if (primaryButton && primaryButton?.getAttribute("kind") === "danger" && secondaryButtons[0]) secondaryButtons[0].focus();
				else {
					const closeButton = this.querySelector(this.constructor.selectorCloseButton);
					if (closeButton) closeButton.focus();
					else {
						const { first } = this.getFocusable();
						first?.focus();
					}
				}
			}
		} else {
			if (this._dialogElement && this._dialogElement.open) this._dialogElement.close();
			if (this._launcher && typeof this._launcher.focus === "function") {
				this._launcher.focus();
				this._launcher = null;
			}
		}
	}
	render() {
		const { ariaLabel, ariaLabelledBy, ariaDescribedBy, role } = this;
		const title = this.querySelector(`cds-dialog-title`);
		return html`
      <dialog
        part="dialog"
        role=${role}
        aria-label=${ifDefined(ariaLabel || void 0)}
        aria-labelledby=${ifDefined(ariaLabelledBy || (!ariaLabel ? title?.id : void 0))}
        aria-describedby=${ifDefined(ariaDescribedBy || void 0)}>
        <div
          class="${"cds"}--dialog-container"
          @click=${this._handleClickContainer}>
          <slot @slotchange="${this._handleSlotChange}"></slot>
        </div>
      </dialog>
    `;
	}
	/**
	* A selector selecting buttons that should close this dialog.
	*/
	static get selectorCloseButton() {
		return `[data-dialog-close],cds-dialog-close-button`;
	}
	/**
	* A selector selecting the nodes that should be focused when dialog gets open.
	*/
	static get selectorPrimaryFocus() {
		return `[data-dialog-primary-focus]`;
	}
	/**
	* A selector selecting the dialog body component
	*/
	static get selectorModalBody() {
		return `cds-dialog-body`;
	}
	/**
	* The name of the custom event fired before this dialog is being closed upon a user gesture.
	* Cancellation of this event stops the user-initiated action of closing this dialog.
	*/
	static get eventBeforeClose() {
		return `cds-dialog-beingclosed`;
	}
	/**
	* The name of the custom event fired after this dialog is closed upon a user gesture.
	*/
	static get eventClose() {
		return `cds-dialog-closed`;
	}
	static {
		this.styles = dialog_default$1;
	}
};
__decorate([query("dialog")], CDSDialog.prototype, "_dialogElement", void 0);
__decorate([HostListener("click")], CDSDialog.prototype, "_handleClick", void 0);
__decorate([property({ attribute: false })], CDSDialog.prototype, "loadingStatus", void 0);
__decorate([property({
	type: Boolean,
	reflect: true
})], CDSDialog.prototype, "modal", void 0);
__decorate([property({ attribute: "aria-label" })], CDSDialog.prototype, "ariaLabel", void 0);
__decorate([property({ attribute: "aria-labelledby" })], CDSDialog.prototype, "ariaLabelledBy", void 0);
__decorate([property({ attribute: "aria-describedby" })], CDSDialog.prototype, "ariaDescribedBy", void 0);
__decorate([property({ reflect: true })], CDSDialog.prototype, "role", void 0);
__decorate([property({
	type: Boolean,
	attribute: "prevent-close-on-click-outside"
})], CDSDialog.prototype, "preventCloseOnClickOutside", void 0);
__decorate([HostListener("keydown")], CDSDialog.prototype, "_handleHostKeydown", void 0);
__decorate([HostListener("document:keydown")], CDSDialog.prototype, "_handleKeydown", void 0);
CDSDialog = __decorate([carbonElement(`cds-dialog`)], CDSDialog);
var dialog_default = CDSDialog;
//#endregion
export { dialog_default as default };

//# sourceMappingURL=dialog.js.map