import { C as ConsumerSlimmers, S as SlimmerConsumedBy, a as SlimmerMappedMethod } from './global-slimmer-registry-0a7aa308.js';

/**
 * This decorator is used to re-define a field in a vega component
 * which will be used for injecting the VegaSlimmer into the vega component.
 *
 * @returns {Function} The decorator function.
 */
function InjectVegaSlimmer() {
  return function (componentPrototype, propertyKey) {
    Object.defineProperty(componentPrototype, propertyKey, {
      set: createSetterForVegaSlimmerField(propertyKey),
      get: createGetterForVegaSlimmerField(propertyKey),
    });
  };
}
/**
 * Creates a setter for a vega slimmer field which will be used for injecting the VegaSlimmer into the vega component.
 *
 * @param {string} fieldName The name of the field to inject the VegaSlimmer into.
 * @returns {Function} A setter for the vega slimmer field.
 */
function createSetterForVegaSlimmerField(fieldName) {
  return function (slimmer) {
    if (!this[ConsumerSlimmers]) {
      this[ConsumerSlimmers] = new Map();
    }
    this[ConsumerSlimmers].set(fieldName, slimmer);
    slimmer[SlimmerConsumedBy] = this;
    registerSlimMethodsToComponent(slimmer, this);
  };
}
/**
 * Creates a getter for a vega slimmer field which will be used for injecting the VegaSlimmer into the vega component.
 *
 * @param {string} fieldName The name of the field to inject the VegaSlimmer into.
 * @returns {Function} A getter for the vega slimmer field.
 */
function createGetterForVegaSlimmerField(fieldName) {
  return function () {
    return this[ConsumerSlimmers].get(fieldName);
  };
}
/**
 * Given a VegaSlimmer and a component, register the slimmer methods to the component.
 * This is done by appending the slimmer methods to the end of the corresponding component method.
 *
 * @param {VegaSlimmer} slimmer The slimmer to register the slimmer method to.
 * @param {ComponentInterface} component The component to register the slimmer method to.
 * @returns {void}
 */
function registerSlimMethodsToComponent(slimmer, component) {
  const slimmerPrototype = slimmer.constructor.prototype;
  if (slimmerPrototype[SlimmerMappedMethod]) {
    for (const entry of slimmerPrototype[SlimmerMappedMethod]) {
      component[entry.componentMethodName] = createRegisteredSlimMethod(component, slimmer, component[entry.componentMethodName], entry);
    }
  }
}
/**
 * Append the slimmer methods to the end of the corresponding component method.
 * Which means the slimmer method will be called when the original method is invoked in the component.
 *
 * @param {ComponentInterface} component The component to register the slimmer method to.
 * @param {VegaSlimmer} slimmer The slimmer to register the slimmer method to.
 * @param {Function} originalMethod The original method to override.
 * @param {SlimmerMappedMethodEntry} mapEntry The map entry for the slimmer method.
 * @returns {Function} The registered slimmer method.
 */
function createRegisteredSlimMethod(component, slimmer, originalMethod, mapEntry) {
  return function (...args) {
    const result = originalMethod === null || originalMethod === void 0 ? void 0 : originalMethod.apply(component, args);
    if (result && typeof result.then === 'function') {
      return result.then(() => slimmer[mapEntry.slimmerMethodName].call(slimmer, ...args));
    }
    else {
      return slimmer[mapEntry.slimmerMethodName].call(slimmer, ...args);
    }
  };
}

export { InjectVegaSlimmer as I, createGetterForVegaSlimmerField as a, createSetterForVegaSlimmerField as c };
