// #region Imports
import { BREAKPOINTS } from './Breakpoints';
import { on } from '@breadstone/mosaik-elements';
/**
 * The `BreakpointController` class is a reactive controller that provides a set of methods to work with breakpoints.
 *
 * @public
 */
export class BreakpointController {
    // #region Fields
    _host;
    _callback;
    _query;
    _match;
    _documentResizeSubscription;
    _windowResizeSubscription;
    // #endregion
    // #region Ctor
    constructor(host, config) {
        this._host = host;
        this._query = config.query;
        this._callback = config.callback;
        this._match = false;
        this._documentResizeSubscription = null;
        this._windowResizeSubscription = null;
        this._host.addController(this);
    }
    // #endregion
    // #region Properties
    /**
     * Gets the query property.
     *
     * @public
     * @readonly
     */
    get query() {
        return this._query;
    }
    set query(value) {
        if (this._query !== value) {
            this._query = value;
        }
    }
    // #endregion
    // #region Methods
    /**
     * @public
     */
    hostConnected() {
        this._documentResizeSubscription = on(window.document, 'resize', () => this.onResize());
        this._windowResizeSubscription = on(window, 'resize', () => this.onResize());
    }
    /**
     * @public
     */
    hostDisconnected() {
        this._documentResizeSubscription?.dispose();
        this._windowResizeSubscription?.dispose();
    }
    /**
     * @public
     */
    hostUpdated() {
        // Check media query once before any 'resize' event
        this.onResize();
    }
    /**
     * @private
     */
    onResize() {
        if (window.matchMedia(this.getQuery()).matches) {
            // From no match to match
            if (!this._match) {
                this._callback?.(true, this._query);
                this._match = true;
            }
        }
        else {
            // From match to no match
            if (this._match) {
                this._callback?.(false, this._query);
                this._match = false;
            }
        }
    }
    /**
     * @private
     */
    getQuery() {
        let firstQuery = '';
        const queries = this.splitQueries(Array.isArray(this._query) ? this._query : [this._query]);
        if (queries.length > 1) {
            throw new Error('Multiple queries are not allowed.');
        }
        firstQuery = queries[0];
        const bp = BREAKPOINTS.find((x) => x.alias === firstQuery || x.mediaQuery === firstQuery);
        if (!bp) {
            throw new Error(`The alias is ${firstQuery} not defined. Custom queries are not supported.`);
        }
        return bp.mediaQuery;
    }
    /**
     * Split each query string into separate query strings if two queries are provided as comma separated.
     *
     * @private
     */
    splitQueries(queries) {
        return queries
            .map((query) => query.split(','))
            .reduce((a1, a2) => a1.concat(a2))
            .map((query) => query.trim());
    }
}
//# sourceMappingURL=BreakpointController.js.map