"use client";
import { findEntryInArray as T } from "../../../util/DataUtils.js";
import { getTooltipEntry as m, getValueByDataKey as p } from "../../../util/ChartUtils.js";
import { getSliced as _ } from "../../../util/getSliced.js";
function O(r, e) {
  var t = Object.keys(r);
  if (Object.getOwnPropertySymbols) {
    var n = Object.getOwnPropertySymbols(r);
    e && (n = n.filter(function(l) {
      return Object.getOwnPropertyDescriptor(r, l).enumerable;
    })), t.push.apply(t, n);
  }
  return t;
}
function g(r) {
  for (var e = 1; e < arguments.length; e++) {
    var t = arguments[e] != null ? arguments[e] : {};
    e % 2 ? O(Object(t), !0).forEach(function(n) {
      I(r, n, t[n]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(r, Object.getOwnPropertyDescriptors(t)) : O(Object(t)).forEach(function(n) {
      Object.defineProperty(r, n, Object.getOwnPropertyDescriptor(t, n));
    });
  }
  return r;
}
function I(r, e, t) {
  return (e = N(e)) in r ? Object.defineProperty(r, e, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : r[e] = t, r;
}
function N(r) {
  var e = k(r, "string");
  return typeof e == "symbol" ? e : e + "";
}
function k(r, e) {
  if (typeof r != "object" || !r) return r;
  var t = r[Symbol.toPrimitive];
  if (t !== void 0) {
    var n = t.call(r, e);
    if (typeof n != "object") return n;
    throw new TypeError("@@toPrimitive must return a primitive value.");
  }
  return (e === "string" ? String : Number)(r);
}
function x(r, e) {
  return r ?? e;
}
var z = (r, e, t, n, l, s, j) => {
  if (!(e == null || s == null)) {
    var {
      chartData: P,
      computedData: w,
      dataStartIndex: E,
      dataEndIndex: S
    } = t, h = [];
    return r.reduce((y, K) => {
      var f, {
        dataDefinedOnItem: A,
        settings: a
      } = K, c = x(A, P), u = Array.isArray(c) ? _(c, E, S) : c, v = (f = a?.dataKey) !== null && f !== void 0 ? f : n, b = a?.nameKey, i;
      if (n && Array.isArray(u) && /*
       * findEntryInArray won't work for Scatter because Scatter provides an array of arrays
       * as tooltip payloads and findEntryInArray is not prepared to handle that.
       * Sad but also ScatterChart only allows 'item' tooltipEventType
       * and also this is only a problem if there are multiple Scatters and each has its own data array
       * so let's fix that some other time.
       */
      !Array.isArray(u[0]) && /*
       * If the tooltipEventType is 'axis', we should search for the dataKey in the sliced data
       * because thanks to allowDuplicatedCategory=false, the order of elements in the array
       * no longer matches the order of elements in the original data
       * and so we need to search by the active dataKey + label rather than by index.
       *
       * The same happens if multiple graphical items are present in the chart
       * and each of them has its own data array. Those arrays get concatenated
       * and again the tooltip index no longer matches the original data.
       *
       * On the other hand the tooltipEventType 'item' should always search by index
       * because we get the index from interacting over the individual elements
       * which is always accurate, irrespective of the allowDuplicatedCategory setting.
       */
      j === "axis" ? i = T(u, n, l) : i = s(u, e, w, b), Array.isArray(i))
        i.forEach((o) => {
          var D = g(g({}, a), {}, {
            name: o.name,
            unit: o.unit,
            // color and fill are erased to keep 100% the identical behaviour to recharts 2.x - but there's nothing stopping us from returning them here. It's technically a breaking change.
            color: void 0,
            // color and fill are erased to keep 100% the identical behaviour to recharts 2.x - but there's nothing stopping us from returning them here. It's technically a breaking change.
            fill: void 0
          });
          y.push(m({
            tooltipEntrySettings: D,
            dataKey: o.dataKey,
            payload: o.payload,
            // @ts-expect-error getValueByDataKey does not validate the output type
            value: p(o.payload, o.dataKey),
            name: o.name
          }));
        });
      else {
        var d;
        y.push(m({
          tooltipEntrySettings: a,
          dataKey: v,
          payload: i,
          // @ts-expect-error getValueByDataKey does not validate the output type
          value: p(i, v),
          // @ts-expect-error getValueByDataKey does not validate the output type
          name: (d = p(i, b)) !== null && d !== void 0 ? d : a?.name
        }));
      }
      return y;
    }, h);
  }
};
export {
  z as combineTooltipPayload
};
