import { InteractingEntryGenerator } from './interacting-entry-generator';
import { select as d3Select } from '../../../polyfill/d3';
/** The PieLegendHoverGenerator class generates an InteractingEntry object that handles mouseover events on a legend of a D3 pie chart and triggers a mouseover event on the corresponding path element. */
export class PieLegendHoverGenerator extends InteractingEntryGenerator {
  constructor() {
    super(...arguments);
    this.eventType = 'mouseover';
  }
  /**
   * The function "createShouldHandle" returns a function that takes an event as a parameter and returns a boolean value.
   *
   * @returns {InteractingEntry['shouldHandle']} A function that takes an event as an argument and returns a boolean value.
   */
  createShouldHandle() {
    return this.shouldHandle;
  }
  /**
   * The function "createHandle" returns the handle property of an InteractingEntry object.
   *
   * @param {LegendInputType} options the legend render options
   * @returns {InteractingEntry['handle']} The `handle` property of the `InteractingEntry` object is being returned.
   */
  createHandle(options) {
    // eslint-disable-next-line jsdoc/require-jsdoc
    return (event) => {
      this.handle(event, options);
    };
  }
  /**
   * This function checks if a mouse event should be handled based on whether the target or its parent element has a specific class.
   *
   * @param {MouseEvent} event - MouseEvent is a type of event that is triggered by a mouse action, such
   * as clicking, hovering, or scrolling. It contains information about the event, such as the target
   * element and the position of the mouse.
   * @returns {boolean} A boolean value is being returned. The method `shouldHandle` checks if the target element
   * or its parent element has a class of `vega-chart-legend-item` and returns `true` if it does, and
   * `false` otherwise.
   */
  shouldHandle(event) {
    var _a;
    const target = event.target;
    const currentTarget = event.currentTarget;
    const container = currentTarget.parentElement && currentTarget.parentElement.nodeType !== 11
      ? currentTarget.parentElement
      : currentTarget.parentNode['host'];
    return (container.tagName === 'VEGA-PIE-CHART' &&
      (target.classList.contains('vega-chart-legend-item') ||
        !!((_a = target.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('vega-chart-legend-item'))));
  }
  /**
   * The function handles a mouse event on a legend and triggers a mouseover event on the corresponding path element in a D3 chart.
   *
   * @param {MouseEvent} event - The event parameter is a MouseEvent, which represents a user action
   * (such as a click or hover) on a specific element in the DOM.
   * @param {LegendInputType} options - LegendInputType is a custom type that likely contains
   * information about the data being displayed in the chart, such as the names of the categories or
   * series being plotted. It may also include information about the chart's appearance or behavior,
   * such as the colors or labels used in the legend. Without more information about
   */
  handle(event, options) {
    var _a;
    const svgSelection = d3Select(event.currentTarget);
    const target = event.target;
    const targetTextNode = target.tagName === 'text'
      ? target
      : target.tagName === 'circle'
        ? target.nextElementSibling
        : target.querySelector('text');
    const name = targetTextNode ? targetTextNode.textContent : '';
    const index = name ? options.data.indexOf(name) : 0;
    const paths = svgSelection
      .selectAll(`path.vega-chart-arc-path`)
      .nodes();
    (_a = paths[index]) === null || _a === void 0 ? void 0 : _a.dispatchEvent(new MouseEvent('mouseover', { bubbles: true }));
  }
}
