// #region Imports
import { on } from '@breadstone/mosaik-elements';
import { ControllerBase } from './Abstracts/ControllerBase';
/**
 * ClickOutsideController - This controller is used to detect clicks outside of the host element.
 *
 * @public
 */
export class ClickOutsideController extends ControllerBase {
    // #region Fields
    _callback;
    _skipInitial;
    _clickSubscription;
    _keydownSubscription;
    _resizeSubscription;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `ClickOutsideController` class.
     *
     * @public
     */
    constructor(host, config) {
        super(host);
        this._callback = config.callback;
        this._skipInitial = config.skipInitial;
        this._clickSubscription = null;
        this._keydownSubscription = null;
        this._resizeSubscription = null;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    hostConnected() {
        if (this._skipInitial) {
            return;
        }
        this.observe();
    }
    /**
     * @public
     * @override
     */
    hostDisconnected() {
        this.unobserve();
    }
    /**
     * @public
     */
    observe() {
        if (!this._clickSubscription) {
            this._clickSubscription = on(document.body, 'click', (e) => {
                this.onClick(e);
            }, { passive: true });
        }
        if (!this._resizeSubscription) {
            this._resizeSubscription = on(document.body, 'resize', (e) => {
                this.onResize(e);
            }, { passive: true });
        }
        if (!this._keydownSubscription) {
            this._keydownSubscription = on(document.body, 'keydown', (e) => {
                this.onKeyDown(e);
            }, { passive: true });
        }
    }
    /**
     * @public
     */
    unobserve() {
        this._clickSubscription?.dispose();
        this._clickSubscription = null;
        this._keydownSubscription?.dispose();
        this._keydownSubscription = null;
        this._resizeSubscription?.dispose();
        this._resizeSubscription = null;
    }
    /**
     * @private
     */
    onClick(event) {
        const isTarget = event.composedPath().includes(this.host);
        if (!isTarget) {
            this._callback?.(event);
        }
    }
    /**
     * @private
     */
    onKeyDown(event) {
        if (event.key === 'Escape') {
            this._callback?.(event);
        }
    }
    /**
     * @private
     */
    onResize(event) {
        this._callback?.(event);
    }
}
//# sourceMappingURL=ClickOutsideController.js.map