import { LogUtility } from '../../../utils/log';
/** The code is defining an abstract class called `BaseStrategy` that implements the `JQueryDelegatedEventStrategy` interface. */
export class BaseStrategy {
  /**
   * The function "getHostName" returns the VegaComponentName.
   *
   * @returns {VegaComponentName} The method is returning the value of the variable "vegaComponentName" of type VegaComponentName.
   */
  getHostName() {
    return this.vegaComponentName;
  }
  /**
   * The function "getEventName" returns the name of an event as a string.
   *
   * @returns {VegaNativeEventNameType} a string value, which is the eventName.
   */
  getEventName() {
    return this.eventName;
  }
  /**
   * The function handles a click event on a specified element and dispatches a custom event with an optional detail.
   *
   * @param {HTMLElement} vegaHost - The `vegaHost` parameter is an HTMLElement that represents the host element where the event will be dispatched.
   * @param {unknown} [detail] - The `detail` parameter is an optional argument that represents additional information or data that can be passed along with the event. It can be of any type, as it is defined as `unknown`.
   * @returns {Event} a custom event of type `Event`.
   */
  handle(vegaHost, detail) {
    const target = this.createEventTarget(vegaHost);
    target.style.display = 'none';
    vegaHost.appendChild(target);
    const clonedEvent = this.createEvent(this.eventName);
    if (detail) {
      Object.defineProperty(clonedEvent, 'detail', {
        configurable: true,
        value: detail,
      });
    }
    target.dispatchEvent(clonedEvent);
    target.remove();
    return clonedEvent;
  }
  /**
   * The function copies attributes from one HTML element to another, excluding attributes starting with "on", "id".
   *
   * @param {HTMLElement} element - The `element` parameter is an HTMLElement object that represents the
   * source element from which the attributes will be copied.
   * @param {HTMLElement} target - The target parameter is an HTMLElement where the attributes of the
   * element parameter will be copied to.
   */
  copyAttributesToTarget(element, target) {
    if (element && target) {
      const attributes = Array.from(element.attributes).filter((attr) => !attr.name.startsWith('on'));
      attributes.forEach((attr) => {
        try {
          target.setAttribute(attr.name, attr.value);
        }
        catch (_a) {
          LogUtility.error(`Delegated strategy copy ${element.tagName} attribute '${attr.name}' error.`);
        }
      });
    }
  }
  /**
   * The function creates and returns a new event object based on the given event name.
   *
   * @param {VegaNativeEventNameType} eventName - The eventName parameter is the name of the event that
   * you want to create. It can be any valid event name, such as "click", "keydown", "mouseover", etc.
   * @returns {Event} a cloned event object.
   */
  createEvent(eventName) {
    let clonedEvent = new Event(eventName, { bubbles: true, cancelable: true });
    if (eventName === 'click') {
      clonedEvent = new MouseEvent(eventName, { bubbles: true, cancelable: true });
    }
    return clonedEvent;
  }
}
