// #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 { getSlotElementOfType } from '../../../../Dom/Slot';
import { on } from '@breadstone/mosaik-elements';
import { ClickOutsideController } from '../../../Controllers/ClickOutsideController';
import { Component } from '../../../Decorators/ComponentDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { FloatingElement } from './FloatingElement';
import { floatingTriggerElementTemplate } from './FloatingTriggerElementTemplate';
import { floatingTriggerElementJoyStyle } from './Themes/FloatingTriggerElement.Joy';
import { floatingTriggerElementRetroStyle } from './Themes/FloatingTriggerElement.Retro';
import { floatingTriggerElementCosmopolitanStyle } from './Themes/FloatingTriggerElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
// #endregion
/**
 * The `{@link FloatingTriggerElement}` element.
 *
 * @slot - The slot where the trigger element is placed.
 * @slot floating - The slot where the floating element is placed.
 *
 * @public
 */
let FloatingTriggerElement = class FloatingTriggerElement extends Themeable(CustomElement) {
    // #region Fields
    _clickOutsideController;
    _clickSubscription;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._clickSubscription = null;
        this._clickOutsideController = new ClickOutsideController(this, {
            skipInitial: true,
            callback: () => this.onClickedOutside()
        });
    }
    // #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-floating-trigger';
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    disconnectedCallback() {
        super.disconnectedCallback();
        this._clickSubscription?.dispose();
    }
    /**
     * @internal
     * @templatefunction
     */
    onSlotChange(event) {
        const assignedElements = event.target.assignedElements();
        const firstElement = assignedElements.at(0);
        if (firstElement) {
            this._clickSubscription = on(firstElement, 'click', () => {
                const floatingElement = getSlotElementOfType(this.shadowRoot, FloatingElement, 'floating');
                if (!floatingElement.active) {
                    floatingElement.active = true;
                    this._clickOutsideController.observe();
                }
                else {
                    floatingElement.active = false;
                    this._clickOutsideController.unobserve();
                }
            });
        }
        if (assignedElements.length > 1) {
            throw new Error('Only one element is allowed in the slot.');
        }
    }
    /**
     * @private
     */
    onClickedOutside() {
        this._clickOutsideController.unobserve();
        const floatingElement = getSlotElementOfType(this.shadowRoot, FloatingElement, 'floating');
        floatingElement.active = false;
    }
};
FloatingTriggerElement = __decorate([
    Component({
        selector: 'mosaik-floating-trigger',
        template: floatingTriggerElementTemplate,
        themes: {
            joy: floatingTriggerElementJoyStyle,
            retro: floatingTriggerElementRetroStyle,
            cosmopolitan: floatingTriggerElementCosmopolitanStyle
        },
        imports: []
    }),
    __metadata("design:paramtypes", [])
], FloatingTriggerElement);
export { FloatingTriggerElement };
//# sourceMappingURL=FloatingTriggerElement.js.map