import { jsx, jsxs } from "react/jsx-runtime";
import { cn } from "../utils/cn.js";
import { defaultChartMargin, getDefaultChartColor } from "./chart-utils.js";
import { g as get, R as ResponsiveContainer } from "../../ResponsiveContainer-by1DMaUt.js";
import * as React from "react";
import { PureComponent } from "react";
import { m as maxBy } from "../../maxBy-C6u9wqc1.js";
import { g as getDefaultExportFromCjs } from "../../_commonjsHelpers-DWwsNxpa.js";
import { aP as requireIteratee, i as isPositiveNumber, k as svgPropertiesNoEvents, R as RechartsStoreProvider, S as SetTooltipEntrySettings, N as ReportChartSize, O as ReportChartMargin, P as TooltipPortalContext, Q as RechartsWrapper, U as Surface, f as filterProps, L as Layer, E as getValueByDataKey, d as useAppDispatch, X as setActiveClickItemIndex, aQ as mouseLeaveItem, Y as setActiveMouseOverItemIndex, V as Rectangle, T as Tooltip } from "../../RechartsWrapper-B6DckFGn.js";
import { s as shallowEqual } from "../../ShallowEqual-BDmmbJf1.js";
import { S as SetComputedData } from "../../chartDataContext-FdKqakhw.js";
var sumBy$2 = {};
var hasRequiredSumBy$1;
function requireSumBy$1() {
  if (hasRequiredSumBy$1) return sumBy$2;
  hasRequiredSumBy$1 = 1;
  (function(exports) {
    Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
    const iteratee = /* @__PURE__ */ requireIteratee();
    function sumBy2(array, iteratee$1) {
      if (!array || !array.length) {
        return 0;
      }
      if (iteratee$1 != null) {
        iteratee$1 = iteratee.iteratee(iteratee$1);
      }
      let result = void 0;
      for (let i = 0; i < array.length; i++) {
        const current = iteratee$1 ? iteratee$1(array[i]) : array[i];
        if (current !== void 0) {
          if (result === void 0) {
            result = current;
          } else {
            result += current;
          }
        }
      }
      return result;
    }
    exports.sumBy = sumBy2;
  })(sumBy$2);
  return sumBy$2;
}
var sumBy$1;
var hasRequiredSumBy;
function requireSumBy() {
  if (hasRequiredSumBy) return sumBy$1;
  hasRequiredSumBy = 1;
  sumBy$1 = requireSumBy$1().sumBy;
  return sumBy$1;
}
var sumByExports = /* @__PURE__ */ requireSumBy();
const sumBy = /* @__PURE__ */ getDefaultExportFromCjs(sumByExports);
var _excluded = ["sourceX", "sourceY", "sourceControlX", "targetX", "targetY", "targetControlX", "linkWidth"], _excluded2 = ["width", "height", "className", "style", "children"];
function _extends() {
  return _extends = Object.assign ? Object.assign.bind() : function(n) {
    for (var e = 1; e < arguments.length; e++) {
      var t = arguments[e];
      for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
    }
    return n;
  }, _extends.apply(null, arguments);
}
function _objectWithoutProperties(e, t) {
  if (null == e) return {};
  var o, r, i = _objectWithoutPropertiesLoose(e, t);
  if (Object.getOwnPropertySymbols) {
    var n = Object.getOwnPropertySymbols(e);
    for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
  }
  return i;
}
function _objectWithoutPropertiesLoose(r, e) {
  if (null == r) return {};
  var t = {};
  for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
    if (-1 !== e.indexOf(n)) continue;
    t[n] = r[n];
  }
  return t;
}
function ownKeys(e, r) {
  var t = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var o = Object.getOwnPropertySymbols(e);
    r && (o = o.filter(function(r2) {
      return Object.getOwnPropertyDescriptor(e, r2).enumerable;
    })), t.push.apply(t, o);
  }
  return t;
}
function _objectSpread(e) {
  for (var r = 1; r < arguments.length; r++) {
    var t = null != arguments[r] ? arguments[r] : {};
    r % 2 ? ownKeys(Object(t), true).forEach(function(r2) {
      _defineProperty(e, r2, t[r2]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r2) {
      Object.defineProperty(e, r2, Object.getOwnPropertyDescriptor(t, r2));
    });
  }
  return e;
}
function _defineProperty(e, r, t) {
  return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
}
function _toPropertyKey(t) {
  var i = _toPrimitive(t, "string");
  return "symbol" == typeof i ? i : i + "";
}
function _toPrimitive(t, r) {
  if ("object" != typeof t || !t) return t;
  var e = t[Symbol.toPrimitive];
  if (void 0 !== e) {
    var i = e.call(t, r);
    if ("object" != typeof i) return i;
    throw new TypeError("@@toPrimitive must return a primitive value.");
  }
  return ("string" === r ? String : Number)(t);
}
var interpolationGenerator = (a, b) => {
  var ka = +a;
  var kb = b - ka;
  return (t) => ka + kb * t;
};
var centerY = (node) => node.y + node.dy / 2;
var getValue = (entry) => entry && entry.value || 0;
var getSumOfIds = (links, ids) => ids.reduce((result, id) => result + getValue(links[id]), 0);
var getSumWithWeightedSource = (tree, links, ids) => ids.reduce((result, id) => {
  var link = links[id];
  var sourceNode = tree[link.source];
  return result + centerY(sourceNode) * getValue(links[id]);
}, 0);
var getSumWithWeightedTarget = (tree, links, ids) => ids.reduce((result, id) => {
  var link = links[id];
  var targetNode = tree[link.target];
  return result + centerY(targetNode) * getValue(links[id]);
}, 0);
var ascendingY = (a, b) => a.y - b.y;
var searchTargetsAndSources = (links, id) => {
  var sourceNodes = [];
  var sourceLinks = [];
  var targetNodes = [];
  var targetLinks = [];
  for (var i = 0, len = links.length; i < len; i++) {
    var link = links[i];
    if (link.source === id) {
      targetNodes.push(link.target);
      targetLinks.push(i);
    }
    if (link.target === id) {
      sourceNodes.push(link.source);
      sourceLinks.push(i);
    }
  }
  return {
    sourceNodes,
    sourceLinks,
    targetLinks,
    targetNodes
  };
};
var updateDepthOfTargets = (tree, curNode) => {
  var {
    targetNodes
  } = curNode;
  for (var i = 0, len = targetNodes.length; i < len; i++) {
    var target = tree[targetNodes[i]];
    if (target) {
      target.depth = Math.max(curNode.depth + 1, target.depth);
      updateDepthOfTargets(tree, target);
    }
  }
};
var getNodesTree = (_ref, width, nodeWidth) => {
  var {
    nodes,
    links
  } = _ref;
  var tree = nodes.map((entry, index) => {
    var result = searchTargetsAndSources(links, index);
    return _objectSpread(_objectSpread(_objectSpread({}, entry), result), {}, {
      value: Math.max(getSumOfIds(links, result.sourceLinks), getSumOfIds(links, result.targetLinks)),
      depth: 0
    });
  });
  for (var i = 0, len = tree.length; i < len; i++) {
    var node = tree[i];
    if (!node.sourceNodes.length) {
      updateDepthOfTargets(tree, node);
    }
  }
  var maxDepth = maxBy(tree, (entry) => entry.depth).depth;
  if (maxDepth >= 1) {
    var childWidth = (width - nodeWidth) / maxDepth;
    for (var _i = 0, _len = tree.length; _i < _len; _i++) {
      var _node = tree[_i];
      if (!_node.targetNodes.length) {
        _node.depth = maxDepth;
      }
      _node.x = _node.depth * childWidth;
      _node.dx = nodeWidth;
    }
  }
  return {
    tree,
    maxDepth
  };
};
var getDepthTree = (tree) => {
  var result = [];
  for (var i = 0, len = tree.length; i < len; i++) {
    var node = tree[i];
    if (!result[node.depth]) {
      result[node.depth] = [];
    }
    result[node.depth].push(node);
  }
  return result;
};
var updateYOfTree = (depthTree, height, nodePadding, links) => {
  var yRatio = Math.min(...depthTree.map((nodes) => (height - (nodes.length - 1) * nodePadding) / sumBy(nodes, getValue)));
  for (var d = 0, maxDepth = depthTree.length; d < maxDepth; d++) {
    for (var i = 0, len = depthTree[d].length; i < len; i++) {
      var node = depthTree[d][i];
      node.y = i;
      node.dy = node.value * yRatio;
    }
  }
  return links.map((link) => _objectSpread(_objectSpread({}, link), {}, {
    dy: getValue(link) * yRatio
  }));
};
var resolveCollisions = function resolveCollisions2(depthTree, height, nodePadding) {
  var sort = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : true;
  for (var i = 0, len = depthTree.length; i < len; i++) {
    var nodes = depthTree[i];
    var n = nodes.length;
    if (sort) {
      nodes.sort(ascendingY);
    }
    var y0 = 0;
    for (var j = 0; j < n; j++) {
      var node = nodes[j];
      var dy = y0 - node.y;
      if (dy > 0) {
        node.y += dy;
      }
      y0 = node.y + node.dy + nodePadding;
    }
    y0 = height + nodePadding;
    for (var _j = n - 1; _j >= 0; _j--) {
      var _node2 = nodes[_j];
      var _dy = _node2.y + _node2.dy + nodePadding - y0;
      if (_dy > 0) {
        _node2.y -= _dy;
        y0 = _node2.y;
      } else {
        break;
      }
    }
  }
};
var relaxLeftToRight = (tree, depthTree, links, alpha) => {
  for (var i = 0, maxDepth = depthTree.length; i < maxDepth; i++) {
    var nodes = depthTree[i];
    for (var j = 0, len = nodes.length; j < len; j++) {
      var node = nodes[j];
      if (node.sourceLinks.length) {
        var sourceSum = getSumOfIds(links, node.sourceLinks);
        var weightedSum = getSumWithWeightedSource(tree, links, node.sourceLinks);
        var y = weightedSum / sourceSum;
        node.y += (y - centerY(node)) * alpha;
      }
    }
  }
};
var relaxRightToLeft = (tree, depthTree, links, alpha) => {
  for (var i = depthTree.length - 1; i >= 0; i--) {
    var nodes = depthTree[i];
    for (var j = 0, len = nodes.length; j < len; j++) {
      var node = nodes[j];
      if (node.targetLinks.length) {
        var targetSum = getSumOfIds(links, node.targetLinks);
        var weightedSum = getSumWithWeightedTarget(tree, links, node.targetLinks);
        var y = weightedSum / targetSum;
        node.y += (y - centerY(node)) * alpha;
      }
    }
  }
};
var updateYOfLinks = (tree, links) => {
  for (var i = 0, len = tree.length; i < len; i++) {
    var node = tree[i];
    var sy = 0;
    var ty = 0;
    node.targetLinks.sort((a, b) => tree[links[a].target].y - tree[links[b].target].y);
    node.sourceLinks.sort((a, b) => tree[links[a].source].y - tree[links[b].source].y);
    for (var j = 0, tLen = node.targetLinks.length; j < tLen; j++) {
      var link = links[node.targetLinks[j]];
      if (link) {
        link.sy = sy;
        sy += link.dy;
      }
    }
    for (var _j2 = 0, sLen = node.sourceLinks.length; _j2 < sLen; _j2++) {
      var _link = links[node.sourceLinks[_j2]];
      if (_link) {
        _link.ty = ty;
        ty += _link.dy;
      }
    }
  }
};
var computeData = (_ref2) => {
  var {
    data,
    width,
    height,
    iterations,
    nodeWidth,
    nodePadding,
    sort
  } = _ref2;
  var {
    links
  } = data;
  var {
    tree
  } = getNodesTree(data, width, nodeWidth);
  var depthTree = getDepthTree(tree);
  var newLinks = updateYOfTree(depthTree, height, nodePadding, links);
  resolveCollisions(depthTree, height, nodePadding, sort);
  var alpha = 1;
  for (var i = 1; i <= iterations; i++) {
    relaxRightToLeft(tree, depthTree, newLinks, alpha *= 0.99);
    resolveCollisions(depthTree, height, nodePadding, sort);
    relaxLeftToRight(tree, depthTree, newLinks, alpha);
    resolveCollisions(depthTree, height, nodePadding, sort);
  }
  updateYOfLinks(tree, newLinks);
  return {
    nodes: tree,
    links: newLinks
  };
};
var getCoordinateOfTooltip = (item, type) => {
  if (type === "node") {
    return {
      x: +item.x + +item.width / 2,
      y: +item.y + +item.height / 2
    };
  }
  return "sourceX" in item && {
    x: (item.sourceX + item.targetX) / 2,
    y: (item.sourceY + item.targetY) / 2
  };
};
var getPayloadOfTooltip = (item, type, nameKey) => {
  var {
    payload
  } = item;
  if (type === "node") {
    return {
      payload,
      name: getValueByDataKey(payload, nameKey, ""),
      value: getValueByDataKey(payload, "value")
    };
  }
  if ("source" in payload && payload.source && payload.target) {
    var sourceName = getValueByDataKey(payload.source, nameKey, "");
    var targetName = getValueByDataKey(payload.target, nameKey, "");
    return {
      payload,
      name: "".concat(sourceName, " - ").concat(targetName),
      value: getValueByDataKey(payload, "value")
    };
  }
  return null;
};
var sankeyPayloadSearcher = (_, activeIndex, computedData, nameKey) => {
  if (activeIndex == null || typeof activeIndex !== "string") {
    return void 0;
  }
  var splitIndex = activeIndex.split("-");
  var [targetType, index] = splitIndex;
  var item = get(computedData, "".concat(targetType, "s[").concat(index, "]"));
  if (item) {
    var payload = getPayloadOfTooltip(item, targetType, nameKey);
    return payload;
  }
  return void 0;
};
var options = {
  chartName: "Sankey",
  defaultTooltipEventType: "item",
  validateTooltipEventTypes: ["item"],
  tooltipPayloadSearcher: sankeyPayloadSearcher,
  eventEmitter: void 0
};
function getTooltipEntrySettings(props) {
  var {
    dataKey,
    nameKey,
    stroke,
    strokeWidth,
    fill,
    name,
    data
  } = props;
  return {
    dataDefinedOnItem: data,
    positions: void 0,
    settings: {
      stroke,
      strokeWidth,
      fill,
      dataKey,
      name,
      nameKey,
      color: fill,
      unit: ""
      // Sankey does not have unit, why?
    }
  };
}
var defaultSankeyMargin = {
  top: 0,
  right: 0,
  bottom: 0,
  left: 0
};
function renderLinkItem(option, props) {
  if (/* @__PURE__ */ React.isValidElement(option)) {
    return /* @__PURE__ */ React.cloneElement(option, props);
  }
  if (typeof option === "function") {
    return option(props);
  }
  var {
    sourceX,
    sourceY,
    sourceControlX,
    targetX,
    targetY,
    targetControlX,
    linkWidth
  } = props, others = _objectWithoutProperties(props, _excluded);
  return /* @__PURE__ */ React.createElement("path", _extends({
    className: "recharts-sankey-link",
    d: "\n          M".concat(sourceX, ",").concat(sourceY, "\n          C").concat(sourceControlX, ",").concat(sourceY, " ").concat(targetControlX, ",").concat(targetY, " ").concat(targetX, ",").concat(targetY, "\n        "),
    fill: "none",
    stroke: "#333",
    strokeWidth: linkWidth,
    strokeOpacity: "0.2"
  }, svgPropertiesNoEvents(others)));
}
var buildLinkProps = (_ref3) => {
  var {
    link,
    nodes,
    left,
    top,
    i,
    linkContent,
    linkCurvature
  } = _ref3;
  var {
    sy: sourceRelativeY,
    ty: targetRelativeY,
    dy: linkWidth
  } = link;
  var sourceNode = nodes[link.source];
  var targetNode = nodes[link.target];
  var sourceX = sourceNode.x + sourceNode.dx + left;
  var targetX = targetNode.x + left;
  var interpolationFunc = interpolationGenerator(sourceX, targetX);
  var sourceControlX = interpolationFunc(linkCurvature);
  var targetControlX = interpolationFunc(1 - linkCurvature);
  var sourceY = sourceNode.y + sourceRelativeY + linkWidth / 2 + top;
  var targetY = targetNode.y + targetRelativeY + linkWidth / 2 + top;
  var linkProps = _objectSpread({
    sourceX,
    targetX,
    sourceY,
    targetY,
    sourceControlX,
    targetControlX,
    sourceRelativeY,
    targetRelativeY,
    linkWidth,
    index: i,
    payload: _objectSpread(_objectSpread({}, link), {}, {
      source: sourceNode,
      target: targetNode
    })
  }, filterProps(linkContent, false));
  return linkProps;
};
function SankeyLinkElement(_ref4) {
  var {
    props,
    i,
    linkContent,
    onMouseEnter: _onMouseEnter,
    onMouseLeave: _onMouseLeave,
    onClick: _onClick,
    dataKey
  } = _ref4;
  var activeCoordinate = getCoordinateOfTooltip(props, "link");
  var activeIndex = "link-".concat(i);
  var dispatch = useAppDispatch();
  var events = {
    onMouseEnter: (e) => {
      dispatch(setActiveMouseOverItemIndex({
        activeIndex,
        activeDataKey: dataKey,
        activeCoordinate
      }));
      _onMouseEnter(props, e);
    },
    onMouseLeave: (e) => {
      dispatch(mouseLeaveItem());
      _onMouseLeave(props, e);
    },
    onClick: (e) => {
      dispatch(setActiveClickItemIndex({
        activeIndex,
        activeDataKey: dataKey,
        activeCoordinate
      }));
      _onClick(props, e);
    }
  };
  return /* @__PURE__ */ React.createElement(Layer, events, renderLinkItem(linkContent, props));
}
function AllSankeyLinkElements(_ref5) {
  var {
    modifiedLinks,
    links,
    linkContent,
    onMouseEnter,
    onMouseLeave,
    onClick,
    dataKey
  } = _ref5;
  return /* @__PURE__ */ React.createElement(Layer, {
    className: "recharts-sankey-links",
    key: "recharts-sankey-links"
  }, links.map((link, i) => {
    var linkProps = modifiedLinks[i];
    return /* @__PURE__ */ React.createElement(SankeyLinkElement, {
      key: "link-".concat(link.source, "-").concat(link.target, "-").concat(link.value),
      props: linkProps,
      linkContent,
      i,
      onMouseEnter,
      onMouseLeave,
      onClick,
      dataKey
    });
  }));
}
function renderNodeItem(option, props) {
  if (/* @__PURE__ */ React.isValidElement(option)) {
    return /* @__PURE__ */ React.cloneElement(option, props);
  }
  if (typeof option === "function") {
    return option(props);
  }
  return (
    // @ts-expect-error recharts radius is not compatible with SVG radius
    /* @__PURE__ */ React.createElement(Rectangle, _extends({
      className: "recharts-sankey-node",
      fill: "#0088fe",
      fillOpacity: "0.8"
    }, svgPropertiesNoEvents(props)))
  );
}
var buildNodeProps = (_ref6) => {
  var {
    node,
    nodeContent,
    top,
    left,
    i
  } = _ref6;
  var {
    x,
    y,
    dx,
    dy
  } = node;
  var nodeProps = _objectSpread(_objectSpread({}, filterProps(nodeContent, false)), {}, {
    x: x + left,
    y: y + top,
    width: dx,
    height: dy,
    index: i,
    payload: node
  });
  return nodeProps;
};
function NodeElement(_ref7) {
  var {
    props,
    nodeContent,
    i,
    onMouseEnter: _onMouseEnter2,
    onMouseLeave: _onMouseLeave2,
    onClick: _onClick2,
    dataKey
  } = _ref7;
  var dispatch = useAppDispatch();
  var activeCoordinate = getCoordinateOfTooltip(props, "node");
  var activeIndex = "node-".concat(i);
  var events = {
    onMouseEnter: (e) => {
      dispatch(setActiveMouseOverItemIndex({
        activeIndex,
        activeDataKey: dataKey,
        activeCoordinate
      }));
      _onMouseEnter2(props, e);
    },
    onMouseLeave: (e) => {
      dispatch(mouseLeaveItem());
      _onMouseLeave2(props, e);
    },
    onClick: (e) => {
      dispatch(setActiveClickItemIndex({
        activeIndex,
        activeDataKey: dataKey,
        activeCoordinate
      }));
      _onClick2(props, e);
    }
  };
  return /* @__PURE__ */ React.createElement(Layer, events, renderNodeItem(nodeContent, props));
}
function AllNodeElements(_ref8) {
  var {
    modifiedNodes,
    nodeContent,
    onMouseEnter,
    onMouseLeave,
    onClick,
    dataKey
  } = _ref8;
  return /* @__PURE__ */ React.createElement(Layer, {
    className: "recharts-sankey-nodes",
    key: "recharts-sankey-nodes"
  }, modifiedNodes.map((modifiedNode, i) => {
    return /* @__PURE__ */ React.createElement(NodeElement, {
      props: modifiedNode,
      nodeContent,
      i,
      onMouseEnter,
      onMouseLeave,
      onClick,
      dataKey
    });
  }));
}
class Sankey extends PureComponent {
  constructor() {
    super(...arguments);
    _defineProperty(this, "state", {
      nodes: [],
      links: [],
      modifiedLinks: [],
      modifiedNodes: []
    });
  }
  static getDerivedStateFromProps(nextProps, prevState) {
    var {
      data,
      width,
      height,
      margin,
      iterations,
      nodeWidth,
      nodePadding,
      sort,
      linkCurvature
    } = nextProps;
    if (data !== prevState.prevData || width !== prevState.prevWidth || height !== prevState.prevHeight || !shallowEqual(margin, prevState.prevMargin) || iterations !== prevState.prevIterations || nodeWidth !== prevState.prevNodeWidth || nodePadding !== prevState.prevNodePadding || sort !== prevState.sort) {
      var contentWidth = width - (margin && margin.left || 0) - (margin && margin.right || 0);
      var contentHeight = height - (margin && margin.top || 0) - (margin && margin.bottom || 0);
      var {
        links,
        nodes
      } = computeData({
        data,
        width: contentWidth,
        height: contentHeight,
        iterations,
        nodeWidth,
        nodePadding,
        sort
      });
      var top = get(margin, "top") || 0;
      var left = get(margin, "left") || 0;
      var modifiedLinks = links.map((link, i) => {
        return buildLinkProps({
          link,
          nodes,
          i,
          top,
          left,
          linkContent: nextProps.link,
          linkCurvature
        });
      });
      var modifiedNodes = nodes.map((node, i) => {
        return buildNodeProps({
          node,
          nodeContent: nextProps.node,
          i,
          top,
          left
        });
      });
      return _objectSpread(_objectSpread({}, prevState), {}, {
        nodes,
        links,
        modifiedLinks,
        modifiedNodes,
        prevData: data,
        prevWidth: iterations,
        prevHeight: height,
        prevMargin: margin,
        prevNodePadding: nodePadding,
        prevNodeWidth: nodeWidth,
        prevIterations: iterations,
        prevSort: sort
      });
    }
    return null;
  }
  handleMouseEnter(item, type, e) {
    var {
      onMouseEnter
    } = this.props;
    if (onMouseEnter) {
      onMouseEnter(item, type, e);
    }
  }
  handleMouseLeave(item, type, e) {
    var {
      onMouseLeave
    } = this.props;
    if (onMouseLeave) {
      onMouseLeave(item, type, e);
    }
  }
  handleClick(item, type, e) {
    var {
      onClick
    } = this.props;
    if (onClick) onClick(item, type, e);
  }
  render() {
    var _this$props = this.props, {
      width,
      height,
      className,
      style,
      children
    } = _this$props, others = _objectWithoutProperties(_this$props, _excluded2);
    if (!isPositiveNumber(width) || !isPositiveNumber(height)) {
      return null;
    }
    var {
      links,
      modifiedNodes,
      modifiedLinks
    } = this.state;
    var attrs = svgPropertiesNoEvents(others);
    return /* @__PURE__ */ React.createElement(RechartsStoreProvider, {
      preloadedState: {
        options
      },
      reduxStoreName: className !== null && className !== void 0 ? className : "Sankey"
    }, /* @__PURE__ */ React.createElement(SetTooltipEntrySettings, {
      fn: getTooltipEntrySettings,
      args: this.props
    }), /* @__PURE__ */ React.createElement(SetComputedData, {
      computedData: {
        links: modifiedLinks,
        nodes: modifiedNodes
      }
    }), /* @__PURE__ */ React.createElement(ReportChartSize, {
      width,
      height
    }), /* @__PURE__ */ React.createElement(ReportChartMargin, {
      margin: defaultSankeyMargin
    }), /* @__PURE__ */ React.createElement(TooltipPortalContext.Provider, {
      value: this.state.tooltipPortal
    }, /* @__PURE__ */ React.createElement(RechartsWrapper, {
      className,
      style,
      width,
      height,
      ref: (node) => {
        if (this.state.tooltipPortal == null) {
          this.setState({
            tooltipPortal: node
          });
        }
      },
      onMouseEnter: void 0,
      onMouseLeave: void 0,
      onClick: void 0,
      onMouseMove: void 0,
      onMouseDown: void 0,
      onMouseUp: void 0,
      onContextMenu: void 0,
      onDoubleClick: void 0,
      onTouchStart: void 0,
      onTouchMove: void 0,
      onTouchEnd: void 0
    }, /* @__PURE__ */ React.createElement(Surface, _extends({}, attrs, {
      width,
      height
    }), children, /* @__PURE__ */ React.createElement(AllSankeyLinkElements, {
      links,
      modifiedLinks,
      linkContent: this.props.link,
      dataKey: this.props.dataKey,
      onMouseEnter: (linkProps, e) => this.handleMouseEnter(linkProps, "link", e),
      onMouseLeave: (linkProps, e) => this.handleMouseLeave(linkProps, "link", e),
      onClick: (linkProps, e) => this.handleClick(linkProps, "link", e)
    }), /* @__PURE__ */ React.createElement(AllNodeElements, {
      modifiedNodes,
      nodeContent: this.props.node,
      dataKey: this.props.dataKey,
      onMouseEnter: (nodeProps, e) => this.handleMouseEnter(nodeProps, "node", e),
      onMouseLeave: (nodeProps, e) => this.handleMouseLeave(nodeProps, "node", e),
      onClick: (nodeProps, e) => this.handleClick(nodeProps, "node", e)
    })))));
  }
}
_defineProperty(Sankey, "displayName", "Sankey");
_defineProperty(Sankey, "defaultProps", {
  nameKey: "name",
  dataKey: "value",
  nodePadding: 10,
  nodeWidth: 10,
  linkCurvature: 0.5,
  iterations: 32,
  margin: {
    top: 5,
    right: 5,
    bottom: 5,
    left: 5
  },
  sort: true
});
const CustomTooltip = ({ active, payload }) => {
  if (active && payload && payload.length && payload[0]) {
    const data = payload[0].payload;
    if (!data || typeof data !== "object") {
      return null;
    }
    if ("source" in data && "target" in data) {
      const linkData = data;
      if (linkData.source && typeof linkData.source === "object" && "name" in linkData.source && linkData.target && typeof linkData.target === "object" && "name" in linkData.target) {
        return /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-border bg-popover p-2 shadow-lg", children: [
          /* @__PURE__ */ jsxs("p", { className: "text-sm font-medium text-primary", children: [
            String(linkData.source.name),
            " → ",
            String(linkData.target.name)
          ] }),
          /* @__PURE__ */ jsxs("p", { className: "text-sm text-secondary", children: [
            "Value: ",
            "value" in linkData ? linkData.value : 0
          ] })
        ] });
      }
    }
    if ("name" in data) {
      return /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-border bg-popover p-2 shadow-lg", children: [
        /* @__PURE__ */ jsx("p", { className: "text-sm font-medium text-primary", children: String(data.name) }),
        /* @__PURE__ */ jsxs("p", { className: "text-sm text-secondary", children: [
          "Total: ",
          "value" in data ? data.value : 0
        ] })
      ] });
    }
    return null;
  }
  return null;
};
const CustomNode = ({ x, y, width, height, payload }) => {
  const fillColor = payload.color || getDefaultChartColor(payload.index || 0);
  return /* @__PURE__ */ jsxs("g", { children: [
    /* @__PURE__ */ jsx(
      "rect",
      {
        x,
        y,
        width,
        height,
        fill: fillColor,
        fillOpacity: 0.9
      }
    ),
    width > 50 && /* @__PURE__ */ jsx(
      "text",
      {
        x: x + width / 2,
        y: y + height / 2,
        textAnchor: "middle",
        dominantBaseline: "middle",
        fill: "white",
        className: "text-xs font-medium",
        children: payload.name
      }
    )
  ] });
};
const CustomLink = ({
  sourceX,
  sourceY,
  sourceControlX,
  targetX,
  targetY,
  targetControlX,
  linkWidth,
  payload
}) => {
  const linkColor = payload.source.color || getDefaultChartColor(payload.source.index || 0);
  return /* @__PURE__ */ jsx(
    "path",
    {
      d: `
        M${sourceX},${sourceY}
        C${sourceControlX},${sourceY} ${targetControlX},${targetY} ${targetX},${targetY}
      `,
      stroke: linkColor,
      strokeWidth: linkWidth,
      strokeOpacity: 0.3,
      fill: "none"
    }
  );
};
function SankeyChart({
  data,
  className,
  showTooltip = true,
  nodeWidth = 10,
  nodePadding = 10,
  tooltipProps,
  sankeyProps,
  height = 400,
  width = "100%",
  margin = defaultChartMargin
}) {
  const processedData = {
    ...data,
    nodes: data.nodes.map((node, index) => ({
      ...node,
      index
    }))
  };
  return /* @__PURE__ */ jsx("div", { className: cn("w-full", className), children: /* @__PURE__ */ jsx(ResponsiveContainer, { width, height, children: /* @__PURE__ */ jsx(
    Sankey,
    {
      data: processedData,
      nodeWidth,
      nodePadding,
      margin,
      link: (props) => /* @__PURE__ */ jsx(CustomLink, { ...props }),
      node: (props) => /* @__PURE__ */ jsx(CustomNode, { ...props }),
      ...sankeyProps,
      children: showTooltip && /* @__PURE__ */ jsx(
        Tooltip,
        {
          content: /* @__PURE__ */ jsx(CustomTooltip, {}),
          cursor: { fill: "hsl(var(--muted))", opacity: 0.5 },
          ...tooltipProps
        }
      )
    }
  ) }) });
}
SankeyChart.displayName = "SankeyChart";
export {
  SankeyChart
};
