import { Component, ElementRef, EventEmitter, HostBinding, Input, Output, ViewChild } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { PopupContainerDirective } from '../../shared/directives/popup/popup-container.directive';
import { MenuComponent } from '../menu/menu.component';
import * as i0 from "@angular/core";
import * as i1 from "@angular/common";
import * as i2 from "../button/button.component";
import * as i3 from "../../display/view-overlay/view-overlay.component";
import * as i4 from "../menu/menu.component";
import * as i5 from "../../shared/directives/popup/popup-container.directive";
export class DropdownComponent {
    constructor() {
        /**
         * Automatically focus the dropdown toggle button
         */
        this.autofocus = false;
        /**
         * Controls the display status overlay for the menu
         */
        this.status = { status: 'hasData', message: '' };
        /**
         * Disable the button to prevent the menu from being opened
         */
        this.disabled = false;
        /**
         * The button type, defaults to icon
         */
        this.buttonType = 'icon';
        /**
         * Adds a tooltip to the dropdown toggle button. Should only be used when
         * the toggle buttonType is 'icon'
         */
        this.buttonTitle = '';
        /**
         * Display a caret with the button label or icon
         */
        this.showArrow = true;
        /**
         * Items to display in the menu
         */
        this.items = [];
        /**
         * The menu item template
         */
        this.menuTemplateType = 'label';
        /**
         * Fix position the menu instead of absolute. Useful when the dropdown is in a
         * dialog
         */
        this.popupFixed = false;
        /**
        * Used to set the menu buttons state to pending
        */
        this.pending = false;
        /**
         * Emits an event when a menu item is selected
         */
        this.itemSelected = new EventEmitter();
        /**
         * Emits an event when the popup menu opens
         */
        this.popupOpened = new EventEmitter();
        /**
         * The status of the menu
         */
        this.menuStatus = 'hidden';
        /** Item to highlight when the menu is opened, used when the user
         *  opens the menu via the keyboard
         */
        this.highlightedItem = null;
        /** Used to know if the menu was toggled via the keyboard */
        this.isKeyboardEvent = false;
        /**
         * Unsubscribe any active subscriptions when the component is destroyed
         */
        this.componentDestroyed = new Subject();
    }
    /**
     * Subscribe to changes to the menu status
     */
    ngOnInit() {
        this.popup.popperStatusChange
            .pipe(takeUntil(this.componentDestroyed))
            .subscribe((event) => {
            this.menuStatus = event;
            if (this.menuStatus === 'visible') {
                this.popupOpened.emit();
            }
        });
    }
    /**
     * Set up the popup
     */
    ngAfterViewInit() {
        let popperOptions = {
            modifiers: {
                preventOverflow: {
                    boundariesElement: 'viewport'
                }
            },
            placement: 'bottom-end'
        };
        if (this.menuPosition === 'left') {
            popperOptions.placement = 'bottom-start';
        }
        if (this.popupFixed) {
            popperOptions.onCreate = () => {
                this.popup.fixPosition();
            };
            popperOptions.onUpdate = () => {
                // Hide the popup when the dropdown changes position due to window resize
                // or container scrolling
                this.popup.hide();
            };
        }
        this.popup.popperOptions = popperOptions;
    }
    /**
     * Unsubscribe to any active subscriptions
     */
    ngOnDestroy() {
        this.componentDestroyed.next();
        this.componentDestroyed.unsubscribe();
    }
    /**
     * Open and close the menu
     */
    toggleMenu(event) {
        if (event) {
            event.stopPropagation();
        }
        if (!this.disabled) {
            if (this.menuStatus === 'hidden') {
                if (this.isKeyboardEvent && this.items.length) {
                    // Setup the first item in the list to highlight when opening the
                    // menu via the keyboard, ignoring headings
                    this.highlightedItem = this.items.find(item => item.display !== 'heading') || null;
                }
                this.popup.show();
            }
            else {
                this.popup.hide();
                // Reset the highlighted item and our keyboard flag
                // when we close the popup
                this.highlightedItem = null;
                this.isKeyboardEvent = false;
            }
        }
    }
    /**
     * Emit the selected item when it changes and close the menu
     */
    selectedChanged(item) {
        this.itemSelected.emit(item);
        this.toggleMenu();
    }
    /**
     * Closes the menu if the tab key is pressed
     */
    keyNavigate(event) {
        switch (event.key) {
            case 'Enter':
                this.isKeyboardEvent = true;
                break;
            case 'Escape':
            case 'Tab':
                if (this.menuStatus === 'visible') {
                    this.toggleMenu(event);
                }
                break;
            default:
                return;
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: DropdownComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: DropdownComponent, selector: "ec-dropdown", inputs: { id: "id", autofocus: "autofocus", status: "status", disabled: "disabled", label: "label", icon: "icon", buttonType: "buttonType", buttonAlignment: "buttonAlignment", buttonTitle: "buttonTitle", tabindex: "tabindex", showArrow: "showArrow", items: "items", menuTemplateType: "menuTemplateType", menuTitle: "menuTitle", menuHeight: "menuHeight", menuWidth: "menuWidth", menuMinWidth: "menuMinWidth", menuPosition: "menuPosition", menuFooter: "menuFooter", popupFixed: "popupFixed", buttonCustomTemplate: "buttonCustomTemplate", pending: "pending" }, outputs: { itemSelected: "itemSelected", popupOpened: "popupOpened" }, host: { properties: { "class.is-pending": "this.pending" } }, viewQueries: [{ propertyName: "menuEl", first: true, predicate: MenuComponent, descendants: true, read: ElementRef }, { propertyName: "popup", first: true, predicate: PopupContainerDirective, descendants: true, static: true }, { propertyName: "dropdownToggleButton", first: true, predicate: ["dropdownToggle"], descendants: true }], ngImport: i0, template: "<div class=\"control\">\r\n  <ec-button [disabled]=\"disabled\"\r\n             id=\"{{id}}_toggle\"\r\n             #dropdownToggle\r\n             [ngClass]=\"{'has-arrow':showArrow, 'active': menuStatus === 'visible', 'text-left': buttonAlignment === 'left', 'text-right': buttonAlignment === 'right'}\"\r\n             *ecPopup=\"dropdownMenu\"\r\n             [label]=\"label\"\r\n             [icon]=\"icon\"\r\n             [tabindex]=\"tabindex\"\r\n             [type]=\"buttonType\"\r\n             [customTemplate]=\"buttonCustomTemplate\"\r\n             [pending]=\"pending\"\r\n             title=\"{{buttonTitle}}\"\r\n             (click)=\"toggleMenu($event)\"\r\n             (keydown)=\"keyNavigate($event)\"\r\n             [autofocus]=\"autofocus\">\r\n  </ec-button>\r\n  <span class=\"arrow is-{{buttonType}}\"\r\n        *ngIf=\"showArrow\"\r\n        [class.is-disabled]=\"disabled\">\r\n    <i class=\"ec-icon icon-caret-down\" [class.flip-y]=\"menuStatus === 'visible'\"></i>\r\n  </span>\r\n</div>\r\n\r\n<ng-template #dropdownMenu>\r\n  <div id=\"{{id}}_popup\"\r\n       class=\"popup\"\r\n       ecOverlay\r\n       [status]=\"status?.status\"\r\n       [message]=\"status?.message\"\r\n       [displayAsMask]=\"true\">\r\n    <!-- Dropdown Menu Title -->\r\n    <header id=\"{{id}}_title\"\r\n            class=\"text-heading-2 px-2 my-2\"\r\n            *ngIf=\"menuTitle\">{{menuTitle}}\r\n    </header>\r\n    <!-- Dropdown Menu -->\r\n    <ec-menu id=\"{{id}}_menu\"\r\n             [class.wrap-items]=\"menuWidth\"\r\n             [templateType]=\"menuTemplateType\"\r\n             [style.width]=\"menuWidth ? menuWidth + 'px' : 'auto'\"\r\n             [style.min-width]=\"menuMinWidth ? menuMinWidth + 'px' : 'auto'\"\r\n             [style.height]=\"menuHeight ? menuHeight + 'px' : 'auto'\"\r\n             [items]=\"items\"\r\n             [showNoItems]=\"true\"\r\n             [enableKeyNav]=\"true\"\r\n             [highlightedItem]=\"highlightedItem\"\r\n             [dropdownToggleButton]=\"dropdownToggleButton.buttonElement\"\r\n             (selectedChanged)=\"selectedChanged($event)\">\r\n    </ec-menu>\r\n    <footer *ngIf=\"menuFooter\">{{menuFooter}}</footer>\r\n  </div>\r\n</ng-template>", styles: [":host{display:inline-block;box-sizing:border-box;vertical-align:top}:host(.font-size-inherit) ec-button{font-size:inherit}:host(.font-size-inherit) ec-button ::ng-deep button{font-size:inherit}:host(.fill){display:block;height:100%;width:100%}:host(.fill) .control{display:block;height:100%;width:100%}:host(.fill) .control ec-button{display:block;height:100%;width:100%}:host(.fill) .control ec-button ::ng-deep button{display:block;height:100%;width:100%;display:flex}:host(.fill) ec-menu{min-width:100%}:host(.td-fill){position:absolute;left:0;top:0}:host(.td-fill) ec-button ::ng-deep button{padding-left:.375rem;padding-right:.375rem;font-size:.75rem}:host(.is-action) ec-button,:host(.is-action) .arrow{color:var(--ec-color-interactive)}:host(.is-condensed) ec-button ::ng-deep button{height:1.5rem}:host(.is-condensed) ec-button.has-arrow ::ng-deep button{padding:0 1.125rem 0 .25rem}:host(.is-condensed) ec-button.has-arrow~.arrow{right:.375rem}:host(.d-block) ec-button{display:block}:host(.d-block) ec-button ::ng-deep button{width:100%}:host(.is-pending) .arrow{opacity:0}:host-context(th){color:var(--ec-color-primary-dark)}.control{position:relative}ec-button{display:block}ec-button.has-arrow~.arrow{position:absolute;top:0;right:.5rem;height:100%;display:flex;align-items:center;pointer-events:none;width:.625rem;z-index:1}ec-button.has-arrow~.arrow .ec-icon{color:var(--ec-color-icon);width:auto;transition:color .3s ease}ec-button.has-arrow~.arrow.is-link .ec-icon{color:var(--ec-button-color-text, var(--ec-color-link))}ec-button.has-arrow~.arrow.is-primary .ec-icon{color:var(--ec-button-color-primary, var(--ec-color-primary-light))}ec-button.has-arrow~.arrow.is-common .ec-icon,ec-button.has-arrow~.arrow.is-secondary .ec-icon{color:var(--ec-button-color-secondary, var(--ec-color-primary-dark))}ec-button.has-arrow~.arrow.is-text .ec-icon{color:var(--ec-button-color-text, var(--ec-color-primary-dark))}ec-button.has-arrow~.arrow.is-disabled .ec-icon{opacity:.5}ec-button.has-arrow.active~.arrow.is-primary .ec-icon,ec-button.has-arrow.active~.arrow.is-secondary .ec-icon,ec-button.has-arrow.active~.arrow.is-text .ec-icon,ec-button.has-arrow.active~.arrow.is-link .ec-icon{color:var(--ec-button-color-active, var(--ec-color-primary-dark))}ec-button.has-arrow ::ng-deep button{width:auto;padding-right:1.25rem}ec-button.has-arrow ::ng-deep button:not(.ec-button-custom){padding-left:.5rem}.popup{background-color:var(--ec-background-color);border-radius:var(--ec-border-radius-card);box-shadow:var(--ec-box-shadow-overlay);margin-top:.25rem;overflow:hidden;z-index:var(--ec-z-index-popup);text-align:start}.popup>header{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.popup footer{color:var(--ec-color-secondary-dark);font-size:var(--ec-font-size-label);border-top:var(--ec-border-width) solid var(--ec-border-color);padding:.1875rem .5rem;line-height:1.2}ec-menu ::ng-deep ul{max-height:50vh}ec-menu ::ng-deep li{white-space:nowrap}ec-menu.wrap-items ::ng-deep li{white-space:normal}\n"], dependencies: [{ kind: "directive", type: i1.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i2.ButtonComponent, selector: "ec-button", inputs: ["id", "disabled", "icon", "label", "badge", "tabindex", "type", "pending", "pendingIcon", "customTemplate", "isSubmit", "autofocus"], outputs: ["clicked"] }, { kind: "component", type: i3.ViewOverlayComponent, selector: "[ecOverlay]", inputs: ["status", "message", "action", "noDataTemplate", "displayAsMask", "overlayClassList"] }, { kind: "component", type: i4.MenuComponent, selector: "ec-menu", inputs: ["id", "items", "selected", "parent", "templateType", "customMenuTemplate", "title", "showNoItems", "noDataText", "enableKeyNav", "highlightedItem", "maintainSelectedItem", "truncateItems", "preserveIconSpace", "dropdownToggleButton"], outputs: ["selectedChanged", "menuClosed"] }, { kind: "directive", type: i5.PopupContainerDirective, selector: "[ecPopup]", inputs: ["ecPopup", "options"] }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: DropdownComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ec-dropdown', template: "<div class=\"control\">\r\n  <ec-button [disabled]=\"disabled\"\r\n             id=\"{{id}}_toggle\"\r\n             #dropdownToggle\r\n             [ngClass]=\"{'has-arrow':showArrow, 'active': menuStatus === 'visible', 'text-left': buttonAlignment === 'left', 'text-right': buttonAlignment === 'right'}\"\r\n             *ecPopup=\"dropdownMenu\"\r\n             [label]=\"label\"\r\n             [icon]=\"icon\"\r\n             [tabindex]=\"tabindex\"\r\n             [type]=\"buttonType\"\r\n             [customTemplate]=\"buttonCustomTemplate\"\r\n             [pending]=\"pending\"\r\n             title=\"{{buttonTitle}}\"\r\n             (click)=\"toggleMenu($event)\"\r\n             (keydown)=\"keyNavigate($event)\"\r\n             [autofocus]=\"autofocus\">\r\n  </ec-button>\r\n  <span class=\"arrow is-{{buttonType}}\"\r\n        *ngIf=\"showArrow\"\r\n        [class.is-disabled]=\"disabled\">\r\n    <i class=\"ec-icon icon-caret-down\" [class.flip-y]=\"menuStatus === 'visible'\"></i>\r\n  </span>\r\n</div>\r\n\r\n<ng-template #dropdownMenu>\r\n  <div id=\"{{id}}_popup\"\r\n       class=\"popup\"\r\n       ecOverlay\r\n       [status]=\"status?.status\"\r\n       [message]=\"status?.message\"\r\n       [displayAsMask]=\"true\">\r\n    <!-- Dropdown Menu Title -->\r\n    <header id=\"{{id}}_title\"\r\n            class=\"text-heading-2 px-2 my-2\"\r\n            *ngIf=\"menuTitle\">{{menuTitle}}\r\n    </header>\r\n    <!-- Dropdown Menu -->\r\n    <ec-menu id=\"{{id}}_menu\"\r\n             [class.wrap-items]=\"menuWidth\"\r\n             [templateType]=\"menuTemplateType\"\r\n             [style.width]=\"menuWidth ? menuWidth + 'px' : 'auto'\"\r\n             [style.min-width]=\"menuMinWidth ? menuMinWidth + 'px' : 'auto'\"\r\n             [style.height]=\"menuHeight ? menuHeight + 'px' : 'auto'\"\r\n             [items]=\"items\"\r\n             [showNoItems]=\"true\"\r\n             [enableKeyNav]=\"true\"\r\n             [highlightedItem]=\"highlightedItem\"\r\n             [dropdownToggleButton]=\"dropdownToggleButton.buttonElement\"\r\n             (selectedChanged)=\"selectedChanged($event)\">\r\n    </ec-menu>\r\n    <footer *ngIf=\"menuFooter\">{{menuFooter}}</footer>\r\n  </div>\r\n</ng-template>", styles: [":host{display:inline-block;box-sizing:border-box;vertical-align:top}:host(.font-size-inherit) ec-button{font-size:inherit}:host(.font-size-inherit) ec-button ::ng-deep button{font-size:inherit}:host(.fill){display:block;height:100%;width:100%}:host(.fill) .control{display:block;height:100%;width:100%}:host(.fill) .control ec-button{display:block;height:100%;width:100%}:host(.fill) .control ec-button ::ng-deep button{display:block;height:100%;width:100%;display:flex}:host(.fill) ec-menu{min-width:100%}:host(.td-fill){position:absolute;left:0;top:0}:host(.td-fill) ec-button ::ng-deep button{padding-left:.375rem;padding-right:.375rem;font-size:.75rem}:host(.is-action) ec-button,:host(.is-action) .arrow{color:var(--ec-color-interactive)}:host(.is-condensed) ec-button ::ng-deep button{height:1.5rem}:host(.is-condensed) ec-button.has-arrow ::ng-deep button{padding:0 1.125rem 0 .25rem}:host(.is-condensed) ec-button.has-arrow~.arrow{right:.375rem}:host(.d-block) ec-button{display:block}:host(.d-block) ec-button ::ng-deep button{width:100%}:host(.is-pending) .arrow{opacity:0}:host-context(th){color:var(--ec-color-primary-dark)}.control{position:relative}ec-button{display:block}ec-button.has-arrow~.arrow{position:absolute;top:0;right:.5rem;height:100%;display:flex;align-items:center;pointer-events:none;width:.625rem;z-index:1}ec-button.has-arrow~.arrow .ec-icon{color:var(--ec-color-icon);width:auto;transition:color .3s ease}ec-button.has-arrow~.arrow.is-link .ec-icon{color:var(--ec-button-color-text, var(--ec-color-link))}ec-button.has-arrow~.arrow.is-primary .ec-icon{color:var(--ec-button-color-primary, var(--ec-color-primary-light))}ec-button.has-arrow~.arrow.is-common .ec-icon,ec-button.has-arrow~.arrow.is-secondary .ec-icon{color:var(--ec-button-color-secondary, var(--ec-color-primary-dark))}ec-button.has-arrow~.arrow.is-text .ec-icon{color:var(--ec-button-color-text, var(--ec-color-primary-dark))}ec-button.has-arrow~.arrow.is-disabled .ec-icon{opacity:.5}ec-button.has-arrow.active~.arrow.is-primary .ec-icon,ec-button.has-arrow.active~.arrow.is-secondary .ec-icon,ec-button.has-arrow.active~.arrow.is-text .ec-icon,ec-button.has-arrow.active~.arrow.is-link .ec-icon{color:var(--ec-button-color-active, var(--ec-color-primary-dark))}ec-button.has-arrow ::ng-deep button{width:auto;padding-right:1.25rem}ec-button.has-arrow ::ng-deep button:not(.ec-button-custom){padding-left:.5rem}.popup{background-color:var(--ec-background-color);border-radius:var(--ec-border-radius-card);box-shadow:var(--ec-box-shadow-overlay);margin-top:.25rem;overflow:hidden;z-index:var(--ec-z-index-popup);text-align:start}.popup>header{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.popup footer{color:var(--ec-color-secondary-dark);font-size:var(--ec-font-size-label);border-top:var(--ec-border-width) solid var(--ec-border-color);padding:.1875rem .5rem;line-height:1.2}ec-menu ::ng-deep ul{max-height:50vh}ec-menu ::ng-deep li{white-space:nowrap}ec-menu.wrap-items ::ng-deep li{white-space:normal}\n"] }]
        }], ctorParameters: () => [], propDecorators: { id: [{
                type: Input
            }], autofocus: [{
                type: Input
            }], status: [{
                type: Input
            }], disabled: [{
                type: Input
            }], label: [{
                type: Input
            }], icon: [{
                type: Input
            }], buttonType: [{
                type: Input
            }], buttonAlignment: [{
                type: Input
            }], buttonTitle: [{
                type: Input
            }], tabindex: [{
                type: Input
            }], showArrow: [{
                type: Input
            }], items: [{
                type: Input
            }], menuTemplateType: [{
                type: Input
            }], menuTitle: [{
                type: Input
            }], menuHeight: [{
                type: Input
            }], menuWidth: [{
                type: Input
            }], menuMinWidth: [{
                type: Input
            }], menuPosition: [{
                type: Input
            }], menuFooter: [{
                type: Input
            }], popupFixed: [{
                type: Input
            }], buttonCustomTemplate: [{
                type: Input
            }], pending: [{
                type: HostBinding,
                args: ['class.is-pending']
            }, {
                type: Input
            }], itemSelected: [{
                type: Output
            }], popupOpened: [{
                type: Output
            }], menuEl: [{
                type: ViewChild,
                args: [MenuComponent, { read: ElementRef }]
            }], popup: [{
                type: ViewChild,
                args: [PopupContainerDirective, { static: true }]
            }], dropdownToggleButton: [{
                type: ViewChild,
                args: ['dropdownToggle']
            }] } });
//# sourceMappingURL=data:application/json;base64, 