// #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 { DomServiceLocator } from '../../../../Dom/Dom';
import { RendererServiceLocator } from '../../../../Dom/Renderer';
import { Emit } from '../../../../Events/Decorators/EmitDecorator';
import { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { EID } from '../../../../Utilities/EId';
import { Slottable } from '../../../Behaviors/Slottable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { portalElementTemplate } from './PortalElementTemplate';
import { PortalProjectionElement } from './PortalProjectionElement';
/**
 * Portal - A mechanism to render content outside its parent component's DOM hierarchy.
 *
 * @element mosaik-portal
 * @fires portalAttached - Fired when the portal is attached to the DOM.
 * @fires portalDetached - Fired when the portal is detached from the DOM.
 *
 * @public
 */
let PortalElement = class PortalElement extends Slottable(CustomElement) {
    // #region Fields
    _attached;
    _detached;
    _projection;
    _destination;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._projection = null;
        this._destination = document.body;
        this._attached = new HTMLElementEventEmitter(this, 'portalAttached');
        this._detached = new HTMLElementEventEmitter(this, 'portalDetached');
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     *
     * @public
     * @static
     * @readonly
    */
    static get is() {
        return 'mosaik-portal';
    }
    /**
     * Gets or sets the `destination` property.
     *
     * @public
     * @attr
     */
    get destination() {
        return this._destination;
    }
    set destination(value) {
        if (this._destination !== value) {
            this._destination = value;
            this.requestUpdate('destination');
        }
    }
    /**
     * Called when a projection is attached to the DOM.
     * Provides reference to `IPortalAttachedEventDetail` as event argument.
     *
     * @public
     * @readonly
     * @eventProperty
     * @type IEventEmitter<IPortalAttachedEventDetail>
     */
    get attached() {
        return this._attached;
    }
    /**
     * Called when a projection is detached from the DOM.
     * Provides reference to `IPortalDetachedEventDetail` as event argument.
     *
     * @public
     * @readonly
     * @eventProperty
     * @type IEventEmitter<IPortalDetachedEventDetail>
     */
    get detached() {
        return this._detached;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @overload
     */
    onSlotChanges(slotName) {
        // this is the slot to react on child menu changes
        if (slotName === '') {
            this.attach();
        }
    }
    /**
     * @public
     * @override
     */
    disconnectedCallback() {
        this.detach();
        super.disconnectedCallback();
    }
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        this.attach();
    }
    /**
     * @protected
     */
    attach() {
        const elements = this.getSlotAssignments('');
        const projection = this.getProjection();
        const destination = this.getDestination();
        // map portal slot elements to projection children.
        // afterward, append projection to destination.
        for (const element of elements) {
            projection.appendChild(element);
            destination.appendChild(projection);
        }
        this._attached.emit({
            destination: destination,
            projection: projection,
            elements: elements
        });
    }
    /**
     * @protected
     */
    detach() {
        const destination = this.getDestination();
        if (this._projection) {
            if (this._projection.isConnected) {
                destination.removeChild(this._projection);
            }
            this._detached.emit({
                destination: destination,
                projection: this._projection
            });
        }
    }
    /**
     * @private
     */
    getDestination() {
        if (typeof this._destination === 'string') {
            return DomServiceLocator.current.findDescendant(document, this._destination, { strict: true });
        }
        return this._destination;
    }
    /**
     * @private
     */
    getProjection() {
        if (!this._projection) {
            this._projection = RendererServiceLocator.current.createElement(PortalProjectionElement.is);
            RendererServiceLocator.current.setAttribute(this._projection, 'id', EID.next(PortalProjectionElement.is));
        }
        return this._projection;
    }
};
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], PortalElement.prototype, "destination", null);
__decorate([
    Emit({ eventName: 'portalAttached' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], PortalElement.prototype, "attached", null);
__decorate([
    Emit({ eventName: 'portalDetached' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], PortalElement.prototype, "detached", null);
PortalElement = __decorate([
    Component({
        selector: 'mosaik-portal',
        template: portalElementTemplate
    }),
    __metadata("design:paramtypes", [])
], PortalElement);
export { PortalElement };
//# sourceMappingURL=PortalElement.js.map