// #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;
    _ignored;
    _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 ?? false;
        this._ignored = config.ignored ?? (() => []);
        this._clickSubscription = null;
        this._keydownSubscription = null;
        this._resizeSubscription = null;
    }
    // #endregion
    // #region Lifecycle
    hostConnected() {
        if (this._skipInitial) {
            return;
        }
        this.observe();
    }
    hostDisconnected() {
        this.unobserve();
    }
    // #endregion
    // #region Public API
    observe() {
        this._clickSubscription ??= on(document.body, 'click', (e) => this.onClick(e), { passive: true });
        this._resizeSubscription ??= on(document.body, 'resize', (e) => this.onResize(e), { passive: true });
        this._keydownSubscription ??= on(document.body, 'keydown', (e) => this.onKeyDown(e), { passive: true });
    }
    unobserve() {
        this._clickSubscription?.dispose();
        this._clickSubscription = null;
        this._keydownSubscription?.dispose();
        this._keydownSubscription = null;
        this._resizeSubscription?.dispose();
        this._resizeSubscription = null;
    }
    // #endregion
    // #region Handlers
    onClick(event) {
        const path = event.composedPath();
        const isInsideHost = path.includes(this.host);
        if (isInsideHost) {
            return;
        }
        const isInsideIgnored = [...this._ignored()].some((el) => path.includes(el));
        if (isInsideIgnored) {
            return;
        }
        this._callback?.(event);
    }
    onKeyDown(event) {
        if (event.key === 'Escape') {
            this._callback?.(event);
        }
    }
    onResize(event) {
        this._callback?.(event);
    }
}
//# sourceMappingURL=ClickOutsideController.js.map