"use client";
import * as o from "react";
import { PureComponent as x, useEffect as O } from "react";
import { createPortal as L } from "react-dom";
import { useLegendPortal as D } from "../context/legendPortalContext.js";
import { DefaultLegendContent as W } from "./DefaultLegendContent.js";
import { isNumber as _ } from "../util/DataUtils.js";
import { getUniqPayload as B } from "../util/payload/getUniqPayload.js";
import { useLegendPayload as C } from "../context/legendPayloadContext.js";
import { useElementOffset as H } from "../util/useElementOffset.js";
import { useMargin as z, useChartWidth as A, useChartHeight as F } from "../context/chartLayoutContext.js";
import { setLegendSettings as N, setLegendSize as y } from "../state/legendSlice.js";
import { useAppDispatch as w } from "../state/hooks.js";
var q = ["contextPayload"];
function s() {
  return s = Object.assign ? Object.assign.bind() : function(t) {
    for (var e = 1; e < arguments.length; e++) {
      var r = arguments[e];
      for (var n in r) ({}).hasOwnProperty.call(r, n) && (t[n] = r[n]);
    }
    return t;
  }, s.apply(null, arguments);
}
function P(t, e) {
  var r = Object.keys(t);
  if (Object.getOwnPropertySymbols) {
    var n = Object.getOwnPropertySymbols(t);
    e && (n = n.filter(function(i) {
      return Object.getOwnPropertyDescriptor(t, i).enumerable;
    })), r.push.apply(r, n);
  }
  return r;
}
function d(t) {
  for (var e = 1; e < arguments.length; e++) {
    var r = arguments[e] != null ? arguments[e] : {};
    e % 2 ? P(Object(r), !0).forEach(function(n) {
      v(t, n, r[n]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(t, Object.getOwnPropertyDescriptors(r)) : P(Object(r)).forEach(function(n) {
      Object.defineProperty(t, n, Object.getOwnPropertyDescriptor(r, n));
    });
  }
  return t;
}
function v(t, e, r) {
  return (e = U(e)) in t ? Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }) : t[e] = r, t;
}
function U(t) {
  var e = K(t, "string");
  return typeof e == "symbol" ? e : e + "";
}
function K(t, e) {
  if (typeof t != "object" || !t) return t;
  var r = t[Symbol.toPrimitive];
  if (r !== void 0) {
    var n = r.call(t, e);
    if (typeof n != "object") return n;
    throw new TypeError("@@toPrimitive must return a primitive value.");
  }
  return (e === "string" ? String : Number)(t);
}
function k(t, e) {
  if (t == null) return {};
  var r, n, i = I(t, e);
  if (Object.getOwnPropertySymbols) {
    var a = Object.getOwnPropertySymbols(t);
    for (n = 0; n < a.length; n++) r = a[n], e.indexOf(r) === -1 && {}.propertyIsEnumerable.call(t, r) && (i[r] = t[r]);
  }
  return i;
}
function I(t, e) {
  if (t == null) return {};
  var r = {};
  for (var n in t) if ({}.hasOwnProperty.call(t, n)) {
    if (e.indexOf(n) !== -1) continue;
    r[n] = t[n];
  }
  return r;
}
function M(t) {
  return t.value;
}
function R(t) {
  var {
    contextPayload: e
  } = t, r = k(t, q), n = B(e, t.payloadUniqBy, M), i = d(d({}, r), {}, {
    payload: n
  });
  return /* @__PURE__ */ o.isValidElement(t.content) ? /* @__PURE__ */ o.cloneElement(t.content, i) : typeof t.content == "function" ? /* @__PURE__ */ o.createElement(t.content, i) : /* @__PURE__ */ o.createElement(W, i);
}
function T(t, e, r, n, i, a) {
  var {
    layout: h,
    align: u,
    verticalAlign: f
  } = e, m, l;
  return (!t || (t.left === void 0 || t.left === null) && (t.right === void 0 || t.right === null)) && (u === "center" && h === "vertical" ? m = {
    left: ((n || 0) - a.width) / 2
  } : m = u === "right" ? {
    right: r && r.right || 0
  } : {
    left: r && r.left || 0
  }), (!t || (t.top === void 0 || t.top === null) && (t.bottom === void 0 || t.bottom === null)) && (f === "middle" ? l = {
    top: ((i || 0) - a.height) / 2
  } : l = f === "bottom" ? {
    bottom: r && r.bottom || 0
  } : {
    top: r && r.top || 0
  }), d(d({}, m), l);
}
function V(t) {
  var e = w();
  return O(() => {
    e(N(t));
  }, [e, t]), null;
}
function G(t) {
  var e = w();
  return O(() => (e(y(t)), () => {
    e(y({
      width: 0,
      height: 0
    }));
  }), [e, t]), null;
}
function J(t) {
  var e = C(), r = D(), n = z(), {
    width: i,
    height: a,
    wrapperStyle: h,
    portal: u
  } = t, [f, m] = H([e]), l = A(), g = F();
  if (l == null || g == null)
    return null;
  var j = l - (n.left || 0) - (n.right || 0), c = p.getWidthOrHeight(t.layout, a, i, j), S = u ? h : d(d({
    position: "absolute",
    width: c?.width || i || "auto",
    height: c?.height || a || "auto"
  }, T(h, t, n, l, g, f)), h), b = u ?? r;
  if (b == null)
    return null;
  var E = /* @__PURE__ */ o.createElement("div", {
    className: "recharts-legend-wrapper",
    style: S,
    ref: m
  }, /* @__PURE__ */ o.createElement(V, {
    layout: t.layout,
    align: t.align,
    verticalAlign: t.verticalAlign,
    itemSorter: t.itemSorter
  }), /* @__PURE__ */ o.createElement(G, {
    width: f.width,
    height: f.height
  }), /* @__PURE__ */ o.createElement(R, s({}, t, c, {
    margin: n,
    chartWidth: l,
    chartHeight: g,
    contextPayload: e
  })));
  return /* @__PURE__ */ L(E, b);
}
class p extends x {
  static getWidthOrHeight(e, r, n, i) {
    return e === "vertical" && _(r) ? {
      height: r
    } : e === "horizontal" ? {
      width: n || i
    } : null;
  }
  render() {
    return /* @__PURE__ */ o.createElement(J, this.props);
  }
}
v(p, "displayName", "Legend");
v(p, "defaultProps", {
  align: "center",
  iconSize: 14,
  itemSorter: "value",
  layout: "horizontal",
  verticalAlign: "bottom"
});
export {
  p as Legend
};
