import { G as GlobalSlimmersRegistry, d as createSetterForVegaSlimmerField, C as ConsumerSlimmers, e as createGetterForVegaSlimmerField, c as MapToComponentField, M as MapToComponentMethod, V as VegaSlimmer, L as LogUtility } from './global-slimmer-registry-17c4efd4.js';

/**
 * The function `InjectVegaGlobalSlimmer` is a TypeScript decorator that registers a property as a Vega
 * global slimmer.
 *
 * @returns {Function} The function `InjectVegaGlobalSlimmer` is returning a property decorator.
 */
function InjectVegaGlobalSlimmer() {
  return function (componentPrototype, propertyKey) {
    Object.defineProperty(componentPrototype, propertyKey, {
      set: registerVegaGlobalSlimmers(propertyKey),
      get: createGetterForVegaSlimmerField(propertyKey),
    });
  };
}
/**
 * The function `registerVegaGlobalSlimmers` registers Vega global slimmers by creating setters for
 * each slimmer field.
 *
 * @param {string} propertyKey - The `propertyKey` parameter is a string that represents the key of a
 * property in an object.
 * @returns {Function} The function `registerVegaGlobalSlimmers` returns another function.
 */
function registerVegaGlobalSlimmers(propertyKey) {
  return function () {
    const globalSlimmers = {};
    GlobalSlimmersRegistry.globalSlimmers.forEach((vegaSlimmer) => {
      globalSlimmers[vegaSlimmer.name] = new vegaSlimmer({
        component: this,
      });
    });
    const slimmers = Object.values(globalSlimmers);
    slimmers.forEach((vegaSlimmer) => {
      createSetterForVegaSlimmerField(propertyKey).call(this, vegaSlimmer);
    });
    if (slimmers.length > 0) {
      this[ConsumerSlimmers].set(propertyKey, globalSlimmers);
    }
  };
}

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
/**
 * The `VegaComponentUsageRuntimeMetricsSlimmer` class is used for sending usage
 * telemetry data to a telemetry service based on defined usage metrics for a specific component.
 * We did not use global slimmer because each component introduces all the definitions of other components during the did load
 */
class VegaComponentUsageRuntimeMetricsSlimmer extends VegaSlimmer {
  /**
   * The constructor initializes a private readonly property with an optional array of
   * ComponentUsageRuntimeMetricsDefinition.
   *
   * @param {ComponentUsageRuntimeMetricsDefinition[]} [usageDefinitions] - The `usageDefinitions`
   * parameter in the constructor is of type `ComponentUsageRuntimeMetricsDefinition<T>[]`, which is an
   * array of `ComponentUsageRuntimeMetricsDefinition` objects. This parameter is optional as it is
   * marked with a question mark `?`, meaning it can be omitted when creating an instance of
   */
  constructor(usageDefinitions) {
    super();
    this.usageDefinitions = usageDefinitions;
  }
  /**
   * register the observer when the slimmer method called
   */
  sendUsagePayloadToTelemetry() {
    // eslint-disable-next-line spellcheck/spell-checker
    void import('./index-9c9865c0.js').then((
    // eslint-disable-next-line spellcheck/spell-checker
    module) => {
      try {
        module['default']['emit']('componentUsage', this.getComponentUsageMetricsPayload());
      }
      catch (error) {
        LogUtility.log('Component usage payload failed to emit telemetry:', error);
      }
    });
  }
  /**
   * The function `getComponentUsageMetricsPayload` generates a payload containing component usage
   * metrics data.
   *
   * @returns {Record<string, unknown>} The `getComponentUsageMetricsPayload` method returns an object with the following
   * structure:
   * {
   * 	componentName: string, // The tag name of the host component in uppercase
   * 	data: { // An object containing payload data with keys and boolean values
   * 		'COMPONENT_USED': true,
   * }
   */
  getComponentUsageMetricsPayload() {
    const payloadData = {};
    payloadData[VegaComponentUsageRuntimeMetricsSlimmer.baseUsageKey] = true;
    if (this.usageDefinitions) {
      for (const usageDefinition of this.usageDefinitions) {
        const { payloadKey, payloadValue } = usageDefinition;
        payloadData[payloadKey] = payloadValue(this.host);
      }
    }
    return {
      componentName: this.host.tagName.toUpperCase(),
      data: payloadData,
    };
  }
}
VegaComponentUsageRuntimeMetricsSlimmer.baseUsageKey = 'COMPONENT_USED';
__decorate([
  MapToComponentField()
], VegaComponentUsageRuntimeMetricsSlimmer.prototype, "host", void 0);
__decorate([
  MapToComponentMethod('componentDidRender')
], VegaComponentUsageRuntimeMetricsSlimmer.prototype, "sendUsagePayloadToTelemetry", null);

export { InjectVegaGlobalSlimmer as I, VegaComponentUsageRuntimeMetricsSlimmer as V };
