// #region Imports
import { on } from '@breadstone/mosaik-elements';
import { ControllerBase } from './Abstracts/ControllerBase';
/**
 * The Keyboard Shortcut Observer observe keyboard events on the document, if the event.key combination matches the pattern provided invoke the provided handler.
 *
 * @public
 */
export class KeyboardShortcutController extends ControllerBase {
    // #region Fields
    _callback;
    _keyDownSubscription = null;
    _keyUpSubscription = null;
    _directShortcut; // the value as it is originally set (not as regexp)
    _shortcutRegExp;
    _keysDown;
    _allowPropagation;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `KeyboardShortcutController` class.
     *
     * @public
     * @param host - The host element of the controller.
     * @param config - The config options for the controller.
     */
    constructor(host, config) {
        super(host);
        this._directShortcut = config.pattern;
        this._shortcutRegExp = /(?:)/;
        this._keysDown = [];
        this._keyDownSubscription = null;
        this._keyUpSubscription = null;
        this.pattern = config.pattern;
        this._callback = config.callback;
        this._allowPropagation = false;
    }
    // #endregion
    // #region Properties
    /**
     * Gets or sets the `pattern` property.
     *
     * @public
     */
    get pattern() {
        return this._directShortcut;
    }
    set pattern(s) {
        this._directShortcut = s;
        if (typeof s === 'string') {
            // format
            const short = s
                .replace('.', '\\.')
                .replace('(', '\\(')
                .replace(')', '\\)')
                .replace('[', '\\[')
                .replace(']', '\\]')
                .replace(/(Space|Spacebar)/i, ' ')
                .replace(/(Ctr|Ctrl)/i, 'Control');
            this._shortcutRegExp = new RegExp(short, 'gi');
        }
        else {
            this._shortcutRegExp = s;
        }
    }
    /**
     * Gets or sets the `allowPropagation` property.
     *
     * @public
     */
    get allowPropagation() {
        return this._allowPropagation;
    }
    set allowPropagation(value) {
        this._allowPropagation = value;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    hostConnected() {
        if (this.isConnected) {
            return;
        }
        this._keyDownSubscription = on(document, 'keydown', (event) => this.onKeyDown(event), true);
        this._keyUpSubscription = on(document, 'keyup', (event) => this.onKeyUp(event), true);
        super.hostConnected();
    }
    /**
     * @public
     * @override
     */
    hostDisconnected() {
        if (!this.isConnected) {
            return;
        }
        this._keyDownSubscription?.dispose();
        this._keyUpSubscription?.dispose();
        super.hostDisconnected();
    }
    /**
     * @private
     */
    matches() {
        const keys = this._keysDown.join(' ');
        if (this._shortcutRegExp.test(keys)) {
            return true;
        }
        return false;
    }
    /**
     * @private
     */
    onKeyDown(event) {
        if (!this.pattern) {
            return;
        }
        this._keysDown.push(event.key);
    }
    /**
     * @private
     */
    onKeyUp(event) {
        // console.log(this.name, this.pattern, ' == ', this.__keysDown);
        if (!this.pattern) {
            return;
        }
        if (this.matches()) {
            this._callback?.(event);
            if (this.allowPropagation) {
                event.stopPropagation();
            }
        }
        this._keysDown = [];
    }
}
//# sourceMappingURL=KeyboardShortcutController.js.map