/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { __decorate } from "tslib";
import { attachShadowTemplate, coerceBoolean, CustomElement, ensureChild, FoundationProperty, isDefined } from '@tylertech/forge-core';
import { tylIconArrowRight } from '@tylertech/tyler-icons/standard';
import { CircularProgressComponent } from '../circular-progress';
import { IconRegistry } from '../icon';
import { LinearProgressComponent } from '../linear-progress';
import { ListComponent } from '../list';
import { ListDropdownAware } from '../list-dropdown/list-dropdown-aware';
import { PopupComponent } from '../popup';
import { MenuAdapter } from './menu-adapter';
import { MENU_CONSTANTS } from './menu-constants';
import { MenuFoundation } from './menu-foundation';
const template = '<template><slot></slot></template>';
const styles = ':host{display:inline-block}:host([hidden]){display:none}';
/**
 * The web component class behind the `<forge-menu>` custom element.
 *
 * @tag forge-menu
 */
let MenuComponent = class MenuComponent extends ListDropdownAware {
    constructor() {
        super();
        IconRegistry.define(tylIconArrowRight);
        this._foundation = new MenuFoundation(new MenuAdapter(this));
        attachShadowTemplate(this, template, styles);
    }
    static get observedAttributes() {
        return [
            MENU_CONSTANTS.attributes.OPEN,
            MENU_CONSTANTS.attributes.PLACEMENT,
            MENU_CONSTANTS.attributes.SELECTED_INDEX,
            MENU_CONSTANTS.attributes.SELECTED_VALUE,
            MENU_CONSTANTS.attributes.DENSE,
            MENU_CONSTANTS.attributes.ICON_CLASS,
            MENU_CONSTANTS.attributes.PERSIST_SELECTION,
            MENU_CONSTANTS.attributes.MODE,
            MENU_CONSTANTS.attributes.SYNC_POPUP_WIDTH,
            MENU_CONSTANTS.attributes.POPUP_CLASSES,
            MENU_CONSTANTS.attributes.OPTION_LIMIT,
            MENU_CONSTANTS.attributes.OBSERVE_SCROLL,
            MENU_CONSTANTS.attributes.OBSERVE_SCROLL_THRESHOLD
        ];
    }
    connectedCallback() {
        if (this.querySelector(MENU_CONSTANTS.selectors.TOGGLE)) {
            this._foundation.initialize();
        }
        else {
            ensureChild(this, MENU_CONSTANTS.selectors.TOGGLE).then(() => this._foundation.initialize());
        }
    }
    attributeChangedCallback(name, oldValue, newValue) {
        super.attributeChangedCallback(name, oldValue, newValue);
        switch (name) {
            case MENU_CONSTANTS.attributes.OPEN:
                this._foundation.open = isDefined(newValue);
                break;
            case MENU_CONSTANTS.attributes.PLACEMENT:
                this._foundation.placement = newValue;
                break;
            case MENU_CONSTANTS.attributes.SELECTED_INDEX:
                this._foundation.selectedIndex = Number(newValue);
                break;
            case MENU_CONSTANTS.attributes.SELECTED_VALUE:
                this._foundation.selectedValue = newValue;
                break;
            case MENU_CONSTANTS.attributes.DENSE:
                this._foundation.dense = coerceBoolean(newValue);
                break;
            case MENU_CONSTANTS.attributes.ICON_CLASS:
                this._foundation.iconClass = newValue;
                break;
            case MENU_CONSTANTS.attributes.PERSIST_SELECTION:
                this.persistSelection = coerceBoolean(newValue);
                break;
            case MENU_CONSTANTS.attributes.MODE:
                this.mode = newValue;
                break;
        }
    }
    disconnectedCallback() {
        this._foundation.disconnect();
    }
    /** Force propagates the key event from another element this component. */
    propagateKeyEvent(evt) {
        this._foundation.onKeydown(evt);
    }
    activateFirstOption() {
        this._foundation.activateFirstOption();
    }
};
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "open", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "options", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "selectedIndex", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "selectedValue", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "placement", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "fallbackPlacements", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "dense", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "iconClass", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "persistSelection", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "mode", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "popupOffset", void 0);
__decorate([
    FoundationProperty()
], MenuComponent.prototype, "optionBuilder", void 0);
__decorate([
    FoundationProperty({ set: false })
], MenuComponent.prototype, "popupElement", void 0);
MenuComponent = __decorate([
    CustomElement({
        name: MENU_CONSTANTS.elementName,
        dependencies: [
            PopupComponent,
            ListComponent,
            CircularProgressComponent,
            LinearProgressComponent
        ]
    })
], MenuComponent);
export { MenuComponent };
