'use strict';

const keyboardManager = require('./keyboard-manager-5ed6c472.js');

/**
 * It takes a function that returns an array of keyboard event listeners, and returns a decorator
 * function that takes a component class and a property name, and registers the listeners returned by
 * the function with the keyboard manager when the component is loaded, and remove registers them when the
 * component is disconnected
 *
 * @param {(instance: ComponentInterface) => KeyboardEventListener[]} listenersRetriever - (instance: ComponentInterface) => KeyboardEventListener[]
 * @returns {(target: ComponentInterface, key: string) => void} A decorator function that takes a component class and a property name.
 */
function InjectKeyboardManager(listenersRetriever) {
  /* A decorator function that takes a component class and a property name. */
  return (componentClass) => {
    const { connectedCallback, componentDidLoad, disconnectedCallback } = componentClass;
    /* eslint-disable jsdoc/require-jsdoc */
    componentClass.connectedCallback = function () {
      const { keyboardEventListeners = [] } = this;
      keyboardManager.KeyboardManager.register(keyboardEventListeners);
      return connectedCallback === null || connectedCallback === void 0 ? void 0 : connectedCallback.call(this);
    };
    componentClass.componentDidLoad = function () {
      this.keyboardEventListeners = listenersRetriever(this);
      keyboardManager.KeyboardManager.register(this.keyboardEventListeners);
      return componentDidLoad === null || componentDidLoad === void 0 ? void 0 : componentDidLoad.call(this);
    };
    componentClass.disconnectedCallback = function () {
      const { keyboardEventListeners = [] } = this;
      keyboardManager.KeyboardManager.deregister(keyboardEventListeners);
      return disconnectedCallback === null || disconnectedCallback === void 0 ? void 0 : disconnectedCallback.call(this);
    };
  };
}

exports.InjectKeyboardManager = InjectKeyboardManager;
