// #region Imports
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var DropDownButtonElement_1;
import { Busyable } from '../../../Behaviors/Busyable';
import { ContentAlignable } from '../../../Behaviors/ContentAlignable';
import { DropDownable } from '../../../Behaviors/DropDownable';
import { Fitable } from '../../../Behaviors/Fitable';
import { Labelable } from '../../../Behaviors/Labelable';
import { Orientable } from '../../../Behaviors/Orientable';
import { Reversible } from '../../../Behaviors/Reversible';
import { ClickOutsideController } from '../../../Controllers/ClickOutsideController';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { RippleElement } from '../../Effects/Ripple/RippleElement';
import { StackElement } from '../../Layouts/Stack/StackElement';
import { IconElement } from '../../Media/Icon/IconElement';
import { FloatingElement } from '../../Primitives/Floating/FloatingElement';
import { FocusRingElement } from '../../Primitives/FocusRing/FocusRingElement';
import { TextElement } from '../../Primitives/Text/TextElement';
import { ProgressRingElement } from '../../Ranges/ProgressRing/ProgressRingElement';
import { MenuElement } from '../../Selectors/Menu/MenuElement';
import { ButtonBaseElement } from '../Abstracts/ButtonBaseElement';
import { dropDownButtonElementMemphisStyle } from './Themes/DropDownButtonElement.Memphis';
import { dropDownButtonElementTemplate } from './DropDownButtonElementTemplate';
import { dropDownButtonElementJoyStyle } from './Themes/DropDownButtonElement.Joy';
import { Themeable } from '../../../../Controls/Behaviors/Themeable';
import { dropDownButtonElementCosmopolitanStyle } from './Themes/DropDownButtonElement.Cosmopolitan';
import { Iconable } from '../../../Behaviors/Iconable';
import { PortalElement } from '../../Primitives/Portal/PortalElement';
import { ProjectionController } from '../../Primitives/Portal/ProjectionController';
// #endregion
/**
 * Drop Down Button - A button that opens a menu when clicked, allowing users to select from a list of options.
 *
 * @description
 * The Drop Down Button is a versatile user interface component that triggers a dropdown menu containing additional
 * options or actions. When clicked, the button reveals a menu with a list of choices, providing users with multiple
 * actions or selections in a compact and organized manner. Drop Down Buttons are commonly used in navigation bars,
 * toolbars, and other interactive areas where a set of related options needs to be presented.
 *
 * @name Drop Down Button
 * @element mosaik-dropdown-button
 * @category Buttons
 *
 * @slot icon - The icon slot.
 * @slot label - The label slot.
 * @slot overlay - The overlay slot (useful for badges).
 * @slot - The default dropdown content slot.
 *
 * @csspart button - The button part.
 * @csspart focusRing - The focus ring part.
 * @csspart ripple - The ripple part.
 * @csspart icon - The icon part.
 * @csspart label - The label part.
 * @csspart progressRing - The progress ring part.
 * @csspart popup - The popup part.
 * @csspart menu - The menu part.
 * @csspart iconCaret - The icon caret part.
 *
 * @cssprop {unknown} --drop-down-button-background-color - The background color of the drop down button.
 * @cssprop {unknown} --drop-down-button-border-color - The border color of the drop down button.
 * @cssprop {unknown} --drop-down-button-border-radius - The border radius of the drop down button.
 * @cssprop {unknown} --drop-down-button-border-width - The border width of the drop down button.
 * @cssprop {unknown} --drop-down-button-font-family - The font family of the drop down button.
 * @cssprop {unknown} --drop-down-button-font-letter-spacing - The letter spacing of the drop down button font.
 * @cssprop {unknown} --drop-down-button-font-line-height - The line height of the drop down button font.
 * @cssprop {unknown} --drop-down-button-font-size - The font size of the drop down button.
 * @cssprop {unknown} --drop-down-button-font-text-decoration - The text decoration of the drop down button font.
 * @cssprop {unknown} --drop-down-button-font-text-transform - The text transform of the drop down button font.
 * @cssprop {unknown} --drop-down-button-font-weight - The font weight of the drop down button.
 * @cssprop {unknown} --drop-down-button-foreground-color - The foreground color of the drop down button.
 * @cssprop {unknown} --drop-down-button-gap - The gap between elements in the drop down button.
 * @cssprop {unknown} --drop-down-button-padding-bottom - The bottom padding of the drop down button.
 * @cssprop {unknown} --drop-down-button-padding-left - The left padding of the drop down button.
 * @cssprop {unknown} --drop-down-button-padding-right - The right padding of the drop down button.
 * @cssprop {unknown} --drop-down-button-padding-top - The top padding of the drop down button.
 * @cssprop {unknown} --drop-down-button-shadow - The shadow of the drop down button.
 * @cssprop {unknown} --drop-down-button-transition-duration - The transition duration of the drop down button.
 *
 * @dependency {StackElement} - The Stack element.
 * @dependency {IconElement} - The Icon element.
 * @dependency {RippleElement} - The Ripple element.
 * @dependency {FocusRingElement} - The Focus Ring element.
 * @dependency {TextElement} - The Text element.
 * @dependency {ProgressRingElement} - The Progress Ring element.
 * @dependency {FloatingElement} - The Floating element.
 * @dependency {MenuElement} - The Menu element.
 *
 * @example
 * ```html
 * <mosaik-dropdown-button label="Options" icon="menu">
 *   <mosaik-menu>
 *     <mosaik-menu-item>Option 1</mosaik-menu-item>
 *     <mosaik-menu-item>Option 2</mosaik-menu-item>
 *   </mosaik-menu>
 * </mosaik-dropdown-button>
 * ```
 *
 * @public
 */
let DropDownButtonElement = DropDownButtonElement_1 = class DropDownButtonElement extends Themeable(Reversible(Orientable(ContentAlignable(Fitable(DropDownable(Busyable(Labelable(Iconable(ButtonBaseElement))))))))) {
    // #region Fields
    _clickOutsideController;
    _projectionController;
    _caretVisible;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._caretVisible = true;
        this._clickOutsideController = new ClickOutsideController(this, {
            skipInitial: true,
            callback: () => this.onClickedOutside()
        });
        this._projectionController = new ProjectionController(this, {
            slotName: '',
            targetSelector: '[part="projection"]',
            projecting: (element) => {
                element.setAttribute(`${DropDownButtonElement_1.is}-projection`, '');
            }
        });
        // REMARK: This is a workaround for the issue with the docs generation.
        Object.assign(this, { maxDropDownHeight: 0 });
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `{@link is}` property.
     * The `{@link is}` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-dropdown-button';
    }
    /**
     * Gets or sets the `caretVisible` property.
     *
     * @public
     * @attr
     */
    get caretVisible() {
        return this._caretVisible;
    }
    set caretVisible(value) {
        if (this._caretVisible !== value) {
            this._caretVisible = value;
            this.requestUpdate('caretVisible');
        }
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    connectedCallback() {
        super.connectedCallback();
        this.on('click', (event) => {
            // check if the event.target is the button
            if (event.currentTarget !== this) {
                return;
            }
            this.toggle();
        });
    }
    /**
     * @protected
     */
    onIsDropDownOpenPropertyChanged(_prev, next) {
        // handle interaction outside of popup
        if (next) {
            // if busy, close the popup
            if (this.isBusy) {
                this.close();
                return;
            }
            this._clickOutsideController.observe();
        }
        else {
            this._clickOutsideController.unobserve();
        }
    }
    /**
     * @protected
     */
    onIsBusyPropertyChanged(_prev, next) {
        // if busy, close the popup
        if (next) {
            this.isDropDownOpen = false;
        }
    }
    /**
     * @private
     */
    onClickedOutside() {
        this.close();
    }
    /**
     * @private
     * @template
     */
    onFloatingActiveChanged(active) {
        if (!active) {
            this.close();
        }
    }
};
__decorate([
    Attribute({
        type: Boolean
        // useDefault: true // TODO: dump naming, because the default is actually true, and should be false
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], DropDownButtonElement.prototype, "caretVisible", null);
__decorate([
    Watch('isDropDownOpen', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Boolean, Boolean]),
    __metadata("design:returntype", void 0)
], DropDownButtonElement.prototype, "onIsDropDownOpenPropertyChanged", null);
__decorate([
    Watch('isBusy', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Boolean, String]),
    __metadata("design:returntype", void 0)
], DropDownButtonElement.prototype, "onIsBusyPropertyChanged", null);
DropDownButtonElement = DropDownButtonElement_1 = __decorate([
    Component({
        selector: 'mosaik-dropdown-button',
        template: dropDownButtonElementTemplate,
        themes: {
            joy: dropDownButtonElementJoyStyle,
            memphis: dropDownButtonElementMemphisStyle,
            cosmopolitan: dropDownButtonElementCosmopolitanStyle
        },
        options: {
            mode: 'open',
            delegatesFocus: true
        },
        imports: [
            StackElement,
            IconElement,
            RippleElement,
            FocusRingElement,
            TextElement,
            ProgressRingElement,
            FloatingElement,
            MenuElement,
            PortalElement
        ]
    }),
    __metadata("design:paramtypes", [])
], DropDownButtonElement);
export { DropDownButtonElement };
//# sourceMappingURL=DropDownButtonElement.js.map