// #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);
};
import { growHorIn, growVerIn, growVerOut } from '../../../../Animations/Presets/Grow';
import { Emit } from '../../../../Events/Decorators/EmitDecorator';
import { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { Animatable } from '../../../Behaviors/Animatable';
import { Disableable } from '../../../Behaviors/Disableable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { ExpandDirection } from '../../../Types/ExpandDirection';
import { CustomElement } from '../../Abstracts/CustomElement';
import { expandableElementTemplate } from './ExpandableElementTemplate';
import { expandableElementJoyStyle } from './Themes/ExpandableElement.Joy';
import { expandableElementRetroStyle } from './Themes/ExpandableElement.Retro';
import { expandableElementCosmopolitanStyle } from './Themes/ExpandableElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
// #endregion
/**
 * Expandable - A collapsible container that can be expanded or collapsed to reveal or hide its content.
 *
 * @description
 * The Expandable component provides a way to organize and display content that can be toggled to show or hide.
 * It typically consists of a header section that serves as a toggle button and a content section that can be expanded or collapsed.
 * Expandables are commonly used in UIs to manage space and provide a cleaner user experience by allowing users to focus on relevant information.
 *
 * @element mosaik-expandable
 * @category Layout
 *
 * @fires expanded - Called when the element is expanded.
 * @fires collapsed - Called when the element is collapsed.
 * @fires toggled - Called when the element is toggled.
 *
 * @example
 * ```html
 * <mosaik-expandable>
 *   <!-- Content to be revealed or hidden -->
 * </mosaik-expandable>
 * ```
 *
 * @public
 */
let ExpandableElement = class ExpandableElement extends Themeable(Animatable(Disableable(CustomElement))) {
    // #region Fields
    _expanded;
    _collapsed;
    _toggled;
    _direction;
    _isExpanded;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._expanded = new HTMLElementEventEmitter(this, 'expanded');
        this._collapsed = new HTMLElementEventEmitter(this, 'collapsed');
        this._toggled = new HTMLElementEventEmitter(this, 'toggled');
        this._direction = ExpandDirection.Down;
        this._isExpanded = false;
    }
    // #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-expandable';
    }
    /**
     * Gets or sets the `direction` property.
     *
     * @public
     * @attr
     */
    get direction() {
        return this._direction;
    }
    set direction(value) {
        if (this._direction !== value) {
            this._direction = value;
            this.requestUpdate('direction');
        }
    }
    /**
     * Gets or sets the `isExpanded` property.
     *
     * @public
     * @attr
     */
    get isExpanded() {
        return this._isExpanded;
    }
    set isExpanded(value) {
        if (this._isExpanded !== value) {
            this.onIsExpandedChanged(this._isExpanded, value);
        }
    }
    /**
     * Called when the element is expanded.
     * Provides reference to `{@link IExpandExpandedEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get expanded() {
        return this._expanded;
    }
    /**
     * Called when the element is collapsed.
     * Provides reference to `{@link IEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get collapsed() {
        return this._collapsed;
    }
    /**
     * Called when the element is toggled.
     * Provides reference to `{@link IEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get toggled() {
        return this._toggled;
    }
    // #endregion
    // #region Methods
    /**
     * Toggles the element between expanded and collapsed states.
     *
     * @public
     */
    toggle() {
        if (this.disabled) {
            return;
        }
        if (!this.isExpanded) {
            this.expand();
        }
        else {
            this.collapse();
        }
    }
    /**
     * Expands the element.
     *
     * @public
     */
    expand() {
        if (this.disabled) {
            return;
        }
        if (this._isExpanded) {
            return;
        }
        this.isExpanded = true;
    }
    /**
     * Collapses the element.
     *
     * @public
     */
    collapse() {
        if (this.disabled) {
            return;
        }
        if (!this._isExpanded) {
            return;
        }
        this.isExpanded = false;
    }
    /**
     * Emits the {@link expanded} event.
     *
     * @protected
     */
    onExpanded(args) {
        this._expanded.emit(args);
    }
    /**
     * Emits the {@link collapsed} event.
     *
     * @protected
     */
    onCollapsed(args) {
        this._collapsed.emit(args);
    }
    /**
     * Emits the {@link toggled} event.
     *
     * @protected
     */
    onToggled(args) {
        this._toggled.emit(args);
    }
    /**
     * This method is invoked when the `isExpanded` property is changed.
     *
     * @protected
     */
    onIsExpandedChanged(prev, next) {
        if (prev === next) {
            return;
        }
        // we hare not really sure if this is the best way to handle the initial flicker of the animation.
        // but when any update is pending, we should not start the animation
        // keep an eye on this and will see if there are any side effects
        if (this.isUpdatePending) {
            this._isExpanded = next;
            this.requestUpdate('isExpanded');
            return;
        }
        this._isExpanded = next;
        this.requestUpdate('isExpanded');
        const inAnimation = this._direction === ExpandDirection.Down || this._direction === ExpandDirection.Up
            ? growVerIn
            : growHorIn;
        const outAnimation = this._direction === ExpandDirection.Down || this._direction === ExpandDirection.Up
            ? growVerOut
            : growHorIn;
        if (next) {
            void this.play(inAnimation);
            this.onExpanded({});
            this.onToggled({});
        }
        else {
            void this.play(outAnimation);
            this.onCollapsed({});
            this.onToggled({});
        }
    }
};
__decorate([
    Emit({ eventName: 'expanded' }),
    __metadata("design:type", Object)
], ExpandableElement.prototype, "_expanded", void 0);
__decorate([
    Emit({ eventName: 'collapsed' }),
    __metadata("design:type", Object)
], ExpandableElement.prototype, "_collapsed", void 0);
__decorate([
    Emit({ eventName: 'toggled' }),
    __metadata("design:type", Object)
], ExpandableElement.prototype, "_toggled", void 0);
__decorate([
    Attribute({ type: ExpandDirection }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], ExpandableElement.prototype, "direction", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], ExpandableElement.prototype, "isExpanded", null);
ExpandableElement = __decorate([
    Component({
        selector: 'mosaik-expandable',
        template: expandableElementTemplate,
        themes: {
            joy: expandableElementJoyStyle,
            retro: expandableElementRetroStyle,
            cosmopolitan: expandableElementCosmopolitanStyle
        },
        host: {
            exportparts: 'content'
        },
        imports: []
    }),
    __metadata("design:paramtypes", [])
], ExpandableElement);
export { ExpandableElement };
//# sourceMappingURL=ExpandableElement.js.map