var __decorate = (this && this.__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;
};
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { LogUtility } from '../../utils/log';
/**
 * 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
 */
export 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('@heartlandone/vega-sandbox-pr-2953-fcd936815eb61383ed5456af7e94d65ac271334f-telemetry-runtime-metrics/index').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);
