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 { MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { FeatureFlag } from '../../feature-control/feature-flag-controller';
/**
 * The `VegaWatchMethodCancelSlimmer` class is a TypeScript class that removes
 * the body of watch methods when a component is not fully instantiated and
 * applies the original method body when the component is fully instantiated.
 */
export class VegaWatchMethodCancelSlimmer extends VegaSlimmer {
  /**
   * The constructor function takes a component object as a parameter and assigns it to the "component"
   * property of the class.
   *
   * @param {GlobalSlimmerConfig} option - The `option` parameter is of type `GlobalSlimmerConfig`.
   * It is an object that represents a component in a software system.
   */
  constructor(option) {
    super();
    this.originalWatchers = new Map();
    /**
     * while component reattached, the component will call `connectedCallback` but not call `componentDidLoad`,
     * so we add this flag to skip empty the watch function in this case.
     */
    this.hasDisconnected = false;
    this.isEnabled = FeatureFlag.isEnabled('VEGA_REACT.PREVENT_CALL_WATCH_METHOD_BEFORE_COMPONENT_LOAD') &&
      FeatureFlag.isEnabled('VEGA_FRAMEWORK.PREVENT_CALL_WATCH_METHOD_BEFORE_COMPONENT_LOAD');
    this.component = option.component;
  }
  /**
   * The connectedCallback function removes the body of watch methods when the component is not fully
   * instantiated.
   */
  connectedCallback() {
    if (!this.isEnabled)
      return;
    if (this.hasDisconnected)
      return;
    const watchers = this.component.constructor['watchers'];
    for (const propName in watchers) {
      const watchMethodNames = watchers[propName];
      watchMethodNames.forEach((methodName) => {
        // while watch method listen by multiple property, we should skip make originalMethod as empty.
        if (!this.originalWatchers.has(methodName)) {
          const originalMethod = this.component[methodName];
          this.originalWatchers.set(methodName, originalMethod);
          /**
           * Remove the watch method body when the component not fully instantiated.
           */
          // eslint-disable-next-line @typescript-eslint/no-empty-function
          this.component[methodName] = function () { };
        }
      });
    }
  }
  /**
   * The `@MapToComponentMethod('disconnectedCallback')` decorator is used to map the `disconnectedCallback` method
   * to the corresponding method in the Stencil component.
   */
  disconnectedCallback() {
    if (!this.isEnabled)
      return;
    this.hasDisconnected = true;
  }
  /**
   * The `componentDidLoad` function applies the original method body to the component's watchers when
   * the component is fully instantiated.
   */
  componentDidLoad() {
    if (!this.isEnabled)
      return;
    this.originalWatchers.forEach((method, methodName) => {
      const originalMethodBody = method;
      /**
       * Apply the watch original method body when the component fully instantiated.
       *
       * @param {...any} args - watch method args
       * @returns {unknown} method results
       */
      this.component[methodName] = function (...args) {
        return originalMethodBody.apply(this, args);
      };
    });
  }
}
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaWatchMethodCancelSlimmer.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaWatchMethodCancelSlimmer.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaWatchMethodCancelSlimmer.prototype, "componentDidLoad", null);
