/**
 * Copyright Aquera Inc 2023
 *
 * This source code is licensed under the BSD-3-Clause license found in the
 * LICENSE file in the root directory of this source tree.
 */
import { __decorate } from "tslib";
import { html, } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { styles } from './nile-menu-item.css';
import '../nile-icon';
import { classMap } from 'lit/directives/class-map.js';
import { query } from 'lit/decorators.js';
import { getTextContent } from '../internal/slot';
import { watch } from '../internal/watch';
import NileElement from '../internal/nile-element';
/**
 * Nile icon component.
 *
 * @tag nile-menu-item
 *
 * @summary Menu items provide options for the user to pick from in a menu.
 * @status stable
 * @since 2.0
 *
 * @dependency nile-icon
 *
 * @slot - The menu item's label.
 * @slot prefix - Used to prepend an icon or similar element to the menu item.
 * @slot suffix - Used to append an icon or similar element to the menu item.
 *
 * @csspart base - The component's base wrapper.
 * @csspart checked-icon - The checked icon, which is only visible when the menu item is checked.
 * @csspart prefix - The prefix container.
 * @csspart label - The menu item label.
 * @csspart suffix - The suffix container.
 * @csspart submenu-icon - The submenu icon, visible only when the menu item has a submenu (not yet implemented).
 */
let NileMenuItem = class NileMenuItem extends NileElement {
    constructor() {
        super(...arguments);
        /** The type of menu item to render. To use `checked`, this value must be set to `checkbox`. */
        this.type = 'normal';
        /** Draws the item in a checked state. */
        this.checked = false;
        /** A unique value to store in the menu item. This can be used as a way to identify menu items when selected. */
        this.value = '';
        /** Draws the menu item in a disabled state, preventing selection. */
        this.disabled = false;
        /** Draws the menu item in an active/selected state. */
        this.active = false;
        /** Draws the item in a checked state. */
        this.hasSubMenu = false;
    }
    connectedCallback() {
        super.connectedCallback();
        this.handleHostClick = this.handleHostClick.bind(this);
        this.addEventListener('click', this.handleHostClick);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.removeEventListener('click', this.handleHostClick);
    }
    handleDefaultSlotChange() {
        const textLabel = this.getTextLabel();
        // Ignore the first time the label is set
        if (typeof this.cachedTextLabel === 'undefined') {
            this.cachedTextLabel = textLabel;
            return;
        }
        // When the label changes, emit a slotchange event so parent controls see it
        if (textLabel !== this.cachedTextLabel) {
            this.cachedTextLabel = textLabel;
            this.emit('slotchange');
        }
    }
    handleHostClick(event) {
        // Prevent the click event from being emitted when the button is disabled or loading
        if (this.disabled) {
            event.preventDefault();
            event.stopImmediatePropagation();
        }
    }
    handleCheckedChange() {
        // For proper accessibility, users have to use type="checkbox" to use the checked attribute
        if (this.checked && this.type !== 'checkbox') {
            this.checked = false;
            console.error('The checked attribute can only be used on menu items with type="checkbox"', this);
            return;
        }
        // Only checkbox types can receive the aria-checked attribute
        if (this.type === 'checkbox') {
            this.setAttribute('aria-checked', this.checked ? 'true' : 'false');
        }
        else {
            this.removeAttribute('aria-checked');
        }
    }
    handleDisabledChange() {
        this.setAttribute('aria-disabled', this.disabled ? 'true' : 'false');
    }
    handleTypeChange() {
        if (this.type === 'checkbox') {
            this.setAttribute('role', 'menuitemcheckbox');
            this.setAttribute('aria-checked', this.checked ? 'true' : 'false');
        }
        else {
            this.setAttribute('role', 'menuitem');
            this.removeAttribute('aria-checked');
        }
    }
    /** Returns a text label based on the contents of the menu item's default slot. */
    getTextLabel() {
        return getTextContent(this.defaultSlot);
    }
    render() {
        return html `
      <div
        part="base"
        class=${classMap({
            'menu-item': true,
            'menu-item--checked': this.checked,
            'menu-item--disabled': this.disabled,
            'menu-item--active': this.active,
            'menu-item--has-submenu': this.hasSubMenu,
        })}
      >
        ${this.checked
            ? html `
              <span part="checked-icon" class="menu-item__check">
                <nile-icon
                  name="var(--nile-icon-tick, var(--ng-icon-check))"
                  method="var(--nile-svg-method-fill, var(--ng-svg-method-stroke))"
                  library="system"
                  aria-hidden="true"
                ></nile-icon>
              </span>
            `
            : ``}

        <slot name="prefix" part="prefix" class="menu-item__prefix"></slot>

        <slot
          part="label"
          class="menu-item__label"
          @slotchange=${this.handleDefaultSlotChange}
        ></slot>

        <slot name="suffix" part="suffix" class="menu-item__suffix"></slot>

        ${this.hasSubMenu
            ? html ` <span part="submenu-icon" class="menu-item__chevron">
              <nile-icon
                name="var(--nile-icon-arrow-right, var(--ng-icon-chevron-right))"
                method="var(--nile-svg-method-fill, var(--ng-svg-method-stroke))"
                library="system"
                aria-hidden="true"
              ></nile-icon>
            </span>`
            : ``}
      </div>
    `;
    }
};
NileMenuItem.styles = styles;
__decorate([
    query('slot:not([name])')
], NileMenuItem.prototype, "defaultSlot", void 0);
__decorate([
    query('.menu-item')
], NileMenuItem.prototype, "menuItem", void 0);
__decorate([
    property()
], NileMenuItem.prototype, "type", void 0);
__decorate([
    property({ type: Boolean, reflect: true })
], NileMenuItem.prototype, "checked", void 0);
__decorate([
    property()
], NileMenuItem.prototype, "value", void 0);
__decorate([
    property({ type: Boolean, reflect: true })
], NileMenuItem.prototype, "disabled", void 0);
__decorate([
    property({ type: Boolean, reflect: true })
], NileMenuItem.prototype, "active", void 0);
__decorate([
    property({ type: Boolean, reflect: true })
], NileMenuItem.prototype, "hasSubMenu", void 0);
__decorate([
    watch('checked')
], NileMenuItem.prototype, "handleCheckedChange", null);
__decorate([
    watch('disabled')
], NileMenuItem.prototype, "handleDisabledChange", null);
__decorate([
    watch('type')
], NileMenuItem.prototype, "handleTypeChange", null);
NileMenuItem = __decorate([
    customElement('nile-menu-item')
], NileMenuItem);
export { NileMenuItem };
export default NileMenuItem;
//# sourceMappingURL=nile-menu-item.js.map