import * as React from "react";
import { PureComponent, useEffect, useMemo } from "react";
import { m as maxBy } from "./maxBy-C6u9wqc1.js";
import { g as getDefaultExportFromCjs } from "./_commonjsHelpers-DWwsNxpa.js";
import { m as createSelector, a0 as selectRealScaleType, aV as combineScaleFunction, n as selectChartLayout, aW as selectDuplicateDomain, _ as pickAxisType, aX as combineAxisTicks, ay as selectRadiusAxis, ax as selectAngleAxis, aY as selectRadiusAxisRangeWithReversed, aZ as selectAngleAxisRangeWithReversed, a_ as selectAxisSettings, a$ as combineCategoricalDomain, b0 as combineGraphicalItemTicks, aP as requireIteratee, aR as requireIdentity, d as useAppDispatch, b1 as addRadiusAxis, b2 as removeRadiusAxis, u as useAppSelector, aw as selectPolarViewBox, L as Layer, ar as polarToCartesian, k as svgPropertiesNoEvents, f as filterProps, ai as adaptEventsOfChild, b3 as getTickClassName, b4 as defaultPolarRadiusAxisProps, b5 as addAngleAxis, b6 as removeAngleAxis, h as useIsPanorama, b7 as defaultPolarAngleAxisProps } from "./RechartsWrapper-B6DckFGn.js";
import { c as clsx } from "./clsx-DgYk2OaC.js";
import { c as Label, T as Text } from "./CategoricalChart-B4Tr_94h.js";
import { D as Dot } from "./Dot-DS5wYRFc.js";
import { P as Polygon } from "./Polygon-B5nnF46T.js";
import { b as selectPolarAxisDomainIncludingNiceTicks, c as selectPolarNiceTicks, d as selectPolarAppliedValues } from "./PolarChart-C3HL4nFT.js";
var selectPolarAxis = (state, axisType, axisId) => {
  switch (axisType) {
    case "angleAxis": {
      return selectAngleAxis(state, axisId);
    }
    case "radiusAxis": {
      return selectRadiusAxis(state, axisId);
    }
    default: {
      throw new Error("Unexpected axis type: ".concat(axisType));
    }
  }
};
var selectPolarAxisRangeWithReversed = (state, axisType, axisId) => {
  switch (axisType) {
    case "angleAxis": {
      return selectAngleAxisRangeWithReversed(state, axisId);
    }
    case "radiusAxis": {
      return selectRadiusAxisRangeWithReversed(state, axisId);
    }
    default: {
      throw new Error("Unexpected axis type: ".concat(axisType));
    }
  }
};
var selectPolarAxisScale = createSelector([selectPolarAxis, selectRealScaleType, selectPolarAxisDomainIncludingNiceTicks, selectPolarAxisRangeWithReversed], combineScaleFunction);
var selectPolarCategoricalDomain = createSelector([selectChartLayout, selectPolarAppliedValues, selectAxisSettings, pickAxisType], combineCategoricalDomain);
var selectPolarAxisTicks = createSelector([selectChartLayout, selectPolarAxis, selectRealScaleType, selectPolarAxisScale, selectPolarNiceTicks, selectPolarAxisRangeWithReversed, selectDuplicateDomain, selectPolarCategoricalDomain, pickAxisType], combineAxisTicks);
var selectPolarGraphicalItemAxisTicks = createSelector([selectChartLayout, selectPolarAxis, selectPolarAxisScale, selectPolarAxisRangeWithReversed, selectDuplicateDomain, selectPolarCategoricalDomain, pickAxisType], combineGraphicalItemTicks);
var minBy$3 = {};
var minBy$2 = {};
var hasRequiredMinBy$2;
function requireMinBy$2() {
  if (hasRequiredMinBy$2) return minBy$2;
  hasRequiredMinBy$2 = 1;
  (function(exports) {
    Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
    function minBy2(items, getValue) {
      if (items.length === 0) {
        return void 0;
      }
      let minElement = items[0];
      let min = getValue(minElement);
      for (let i = 1; i < items.length; i++) {
        const element = items[i];
        const value = getValue(element);
        if (value < min) {
          min = value;
          minElement = element;
        }
      }
      return minElement;
    }
    exports.minBy = minBy2;
  })(minBy$2);
  return minBy$2;
}
var hasRequiredMinBy$1;
function requireMinBy$1() {
  if (hasRequiredMinBy$1) return minBy$3;
  hasRequiredMinBy$1 = 1;
  (function(exports) {
    Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
    const minBy$12 = /* @__PURE__ */ requireMinBy$2();
    const identity = /* @__PURE__ */ requireIdentity();
    const iteratee = /* @__PURE__ */ requireIteratee();
    function minBy2(items, iteratee$1) {
      if (items == null) {
        return void 0;
      }
      return minBy$12.minBy(Array.from(items), iteratee.iteratee(iteratee$1 ?? identity.identity));
    }
    exports.minBy = minBy2;
  })(minBy$3);
  return minBy$3;
}
var minBy$1;
var hasRequiredMinBy;
function requireMinBy() {
  if (hasRequiredMinBy) return minBy$1;
  hasRequiredMinBy = 1;
  minBy$1 = requireMinBy$1().minBy;
  return minBy$1;
}
var minByExports = /* @__PURE__ */ requireMinBy();
const minBy = /* @__PURE__ */ getDefaultExportFromCjs(minByExports);
var _excluded$1 = ["cx", "cy", "angle", "axisLine"], _excluded2 = ["angle", "tickFormatter", "stroke", "tick"];
function _extends$1() {
  return _extends$1 = 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$1.apply(null, arguments);
}
function ownKeys$1(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$1(e) {
  for (var r = 1; r < arguments.length; r++) {
    var t = null != arguments[r] ? arguments[r] : {};
    r % 2 ? ownKeys$1(Object(t), true).forEach(function(r2) {
      _defineProperty$1(e, r2, t[r2]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function(r2) {
      Object.defineProperty(e, r2, Object.getOwnPropertyDescriptor(t, r2));
    });
  }
  return e;
}
function _defineProperty$1(e, r, t) {
  return (r = _toPropertyKey$1(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
}
function _toPropertyKey$1(t) {
  var i = _toPrimitive$1(t, "string");
  return "symbol" == typeof i ? i : i + "";
}
function _toPrimitive$1(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);
}
function _objectWithoutProperties$1(e, t) {
  if (null == e) return {};
  var o, r, i = _objectWithoutPropertiesLoose$1(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$1(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;
}
var AXIS_TYPE$1 = "radiusAxis";
function SetRadiusAxisSettings(settings) {
  var dispatch = useAppDispatch();
  useEffect(() => {
    dispatch(addRadiusAxis(settings));
    return () => {
      dispatch(removeRadiusAxis(settings));
    };
  });
  return null;
}
var getTickValueCoord = (_ref, angle, cx, cy) => {
  var {
    coordinate
  } = _ref;
  return polarToCartesian(cx, cy, coordinate, angle);
};
var getTickTextAnchor$1 = (orientation) => {
  var textAnchor;
  switch (orientation) {
    case "left":
      textAnchor = "end";
      break;
    case "right":
      textAnchor = "start";
      break;
    default:
      textAnchor = "middle";
      break;
  }
  return textAnchor;
};
var getViewBox = (angle, cx, cy, ticks) => {
  var maxRadiusTick = maxBy(ticks, (entry) => entry.coordinate || 0);
  var minRadiusTick = minBy(ticks, (entry) => entry.coordinate || 0);
  return {
    cx,
    cy,
    startAngle: angle,
    endAngle: angle,
    innerRadius: minRadiusTick.coordinate || 0,
    outerRadius: maxRadiusTick.coordinate || 0
  };
};
var renderAxisLine = (props, ticks) => {
  var {
    cx,
    cy,
    angle,
    axisLine
  } = props, others = _objectWithoutProperties$1(props, _excluded$1);
  var extent = ticks.reduce((result, entry) => [Math.min(result[0], entry.coordinate), Math.max(result[1], entry.coordinate)], [Infinity, -Infinity]);
  var point0 = polarToCartesian(cx, cy, extent[0], angle);
  var point1 = polarToCartesian(cx, cy, extent[1], angle);
  var axisLineProps = _objectSpread$1(_objectSpread$1(_objectSpread$1({}, svgPropertiesNoEvents(others)), {}, {
    fill: "none"
  }, filterProps(axisLine, false)), {}, {
    x1: point0.x,
    y1: point0.y,
    x2: point1.x,
    y2: point1.y
  });
  return /* @__PURE__ */ React.createElement("line", _extends$1({
    className: "recharts-polar-radius-axis-line"
  }, axisLineProps));
};
var renderTickItem = (option, tickProps, value) => {
  var tickItem;
  if (/* @__PURE__ */ React.isValidElement(option)) {
    tickItem = /* @__PURE__ */ React.cloneElement(option, tickProps);
  } else if (typeof option === "function") {
    tickItem = option(tickProps);
  } else {
    tickItem = /* @__PURE__ */ React.createElement(Text, _extends$1({}, tickProps, {
      className: "recharts-polar-radius-axis-tick-value"
    }), value);
  }
  return tickItem;
};
var renderTicks = (props, ticks) => {
  var {
    angle,
    tickFormatter,
    stroke,
    tick
  } = props, others = _objectWithoutProperties$1(props, _excluded2);
  var textAnchor = getTickTextAnchor$1(props.orientation);
  var axisProps = svgPropertiesNoEvents(others);
  var customTickProps = filterProps(tick, false);
  var items = ticks.map((entry, i) => {
    var coord = getTickValueCoord(entry, props.angle, props.cx, props.cy);
    var tickProps = _objectSpread$1(_objectSpread$1(_objectSpread$1(_objectSpread$1({
      textAnchor,
      transform: "rotate(".concat(90 - angle, ", ").concat(coord.x, ", ").concat(coord.y, ")")
    }, axisProps), {}, {
      stroke: "none",
      fill: stroke
    }, customTickProps), {}, {
      index: i
    }, coord), {}, {
      payload: entry
    });
    return /* @__PURE__ */ React.createElement(Layer, _extends$1({
      className: clsx("recharts-polar-radius-axis-tick", getTickClassName(tick)),
      key: "tick-".concat(entry.coordinate)
    }, adaptEventsOfChild(props, entry, i)), renderTickItem(tick, tickProps, tickFormatter ? tickFormatter(entry.value, i) : entry.value));
  });
  return /* @__PURE__ */ React.createElement(Layer, {
    className: "recharts-polar-radius-axis-ticks"
  }, items);
};
var PolarRadiusAxisWrapper = (defaultsAndInputs) => {
  var {
    radiusAxisId
  } = defaultsAndInputs;
  var viewBox = useAppSelector(selectPolarViewBox);
  var scale = useAppSelector((state) => selectPolarAxisScale(state, "radiusAxis", radiusAxisId));
  var ticks = useAppSelector((state) => selectPolarAxisTicks(state, "radiusAxis", radiusAxisId, false));
  if (viewBox == null || !ticks || !ticks.length) {
    return null;
  }
  var props = _objectSpread$1(_objectSpread$1(_objectSpread$1({}, defaultsAndInputs), {}, {
    scale
  }, viewBox), {}, {
    radius: viewBox.outerRadius
  });
  var {
    tick,
    axisLine
  } = props;
  return /* @__PURE__ */ React.createElement(Layer, {
    className: clsx("recharts-polar-radius-axis", AXIS_TYPE$1, props.className)
  }, axisLine && renderAxisLine(props, ticks), tick && renderTicks(props, ticks), Label.renderCallByParent(props, getViewBox(props.angle, props.cx, props.cy, ticks)));
};
class PolarRadiusAxis extends PureComponent {
  render() {
    return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(SetRadiusAxisSettings, {
      domain: this.props.domain,
      id: this.props.radiusAxisId,
      scale: this.props.scale,
      type: this.props.type,
      dataKey: this.props.dataKey,
      unit: void 0,
      name: this.props.name,
      allowDuplicatedCategory: this.props.allowDuplicatedCategory,
      allowDataOverflow: this.props.allowDataOverflow,
      reversed: this.props.reversed,
      includeHidden: this.props.includeHidden,
      allowDecimals: this.props.allowDecimals,
      tickCount: this.props.tickCount,
      ticks: this.props.ticks,
      tick: this.props.tick
    }), /* @__PURE__ */ React.createElement(PolarRadiusAxisWrapper, this.props));
  }
}
_defineProperty$1(PolarRadiusAxis, "displayName", "PolarRadiusAxis");
_defineProperty$1(PolarRadiusAxis, "axisType", AXIS_TYPE$1);
_defineProperty$1(PolarRadiusAxis, "defaultProps", defaultPolarRadiusAxisProps);
var _excluded = ["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 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);
}
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;
}
var RADIAN = Math.PI / 180;
var eps = 1e-5;
var AXIS_TYPE = "angleAxis";
function SetAngleAxisSettings(props) {
  var dispatch = useAppDispatch();
  var settings = useMemo(() => {
    var {
      children
    } = props, rest = _objectWithoutProperties(props, _excluded);
    return rest;
  }, [props]);
  var synchronizedSettings = useAppSelector((state) => selectAngleAxis(state, settings.id));
  var settingsAreSynchronized = settings === synchronizedSettings;
  useEffect(() => {
    dispatch(addAngleAxis(settings));
    return () => {
      dispatch(removeAngleAxis(settings));
    };
  }, [dispatch, settings]);
  if (settingsAreSynchronized) {
    return props.children;
  }
  return null;
}
var getTickLineCoord = (data, props) => {
  var {
    cx,
    cy,
    radius,
    orientation,
    tickSize
  } = props;
  var tickLineSize = tickSize || 8;
  var p1 = polarToCartesian(cx, cy, radius, data.coordinate);
  var p2 = polarToCartesian(cx, cy, radius + (orientation === "inner" ? -1 : 1) * tickLineSize, data.coordinate);
  return {
    x1: p1.x,
    y1: p1.y,
    x2: p2.x,
    y2: p2.y
  };
};
var getTickTextAnchor = (data, orientation) => {
  var cos = Math.cos(-data.coordinate * RADIAN);
  if (cos > eps) {
    return orientation === "outer" ? "start" : "end";
  }
  if (cos < -eps) {
    return orientation === "outer" ? "end" : "start";
  }
  return "middle";
};
var AxisLine = (props) => {
  var {
    cx,
    cy,
    radius,
    axisLineType,
    axisLine,
    ticks
  } = props;
  if (!axisLine) {
    return null;
  }
  var axisLineProps = _objectSpread(_objectSpread({}, svgPropertiesNoEvents(props)), {}, {
    fill: "none"
  }, filterProps(axisLine, false));
  if (axisLineType === "circle") {
    return /* @__PURE__ */ React.createElement(Dot, _extends({
      className: "recharts-polar-angle-axis-line"
    }, axisLineProps, {
      cx,
      cy,
      r: radius
    }));
  }
  var points = ticks.map((entry) => polarToCartesian(cx, cy, radius, entry.coordinate));
  return /* @__PURE__ */ React.createElement(Polygon, _extends({
    className: "recharts-polar-angle-axis-line"
  }, axisLineProps, {
    points
  }));
};
var TickItemText = (_ref) => {
  var {
    tick,
    tickProps,
    value
  } = _ref;
  if (!tick) {
    return null;
  }
  if (/* @__PURE__ */ React.isValidElement(tick)) {
    return /* @__PURE__ */ React.cloneElement(tick, tickProps);
  }
  if (typeof tick === "function") {
    return tick(tickProps);
  }
  return /* @__PURE__ */ React.createElement(Text, _extends({}, tickProps, {
    className: "recharts-polar-angle-axis-tick-value"
  }), value);
};
var Ticks = (props) => {
  var {
    tick,
    tickLine,
    tickFormatter,
    stroke,
    ticks
  } = props;
  var axisProps = svgPropertiesNoEvents(props);
  var customTickProps = filterProps(tick, false);
  var tickLineProps = _objectSpread(_objectSpread({}, axisProps), {}, {
    fill: "none"
  }, filterProps(tickLine, false));
  var items = ticks.map((entry, i) => {
    var lineCoord = getTickLineCoord(entry, props);
    var textAnchor = getTickTextAnchor(entry, props.orientation);
    var tickProps = _objectSpread(_objectSpread(_objectSpread({}, axisProps), {}, {
      textAnchor,
      stroke: "none",
      fill: stroke
    }, customTickProps), {}, {
      index: i,
      payload: entry,
      x: lineCoord.x2,
      y: lineCoord.y2
    });
    return /* @__PURE__ */ React.createElement(Layer, _extends({
      className: clsx("recharts-polar-angle-axis-tick", getTickClassName(tick)),
      key: "tick-".concat(entry.coordinate)
    }, adaptEventsOfChild(props, entry, i)), tickLine && /* @__PURE__ */ React.createElement("line", _extends({
      className: "recharts-polar-angle-axis-tick-line"
    }, tickLineProps, lineCoord)), /* @__PURE__ */ React.createElement(TickItemText, {
      tick,
      tickProps,
      value: tickFormatter ? tickFormatter(entry.value, i) : entry.value
    }));
  });
  return /* @__PURE__ */ React.createElement(Layer, {
    className: "recharts-polar-angle-axis-ticks"
  }, items);
};
var PolarAngleAxisWrapper = (defaultsAndInputs) => {
  var {
    angleAxisId
  } = defaultsAndInputs;
  var viewBox = useAppSelector(selectPolarViewBox);
  var scale = useAppSelector((state) => selectPolarAxisScale(state, "angleAxis", angleAxisId));
  var isPanorama = useIsPanorama();
  var ticks = useAppSelector((state) => selectPolarAxisTicks(state, "angleAxis", angleAxisId, isPanorama));
  if (viewBox == null || !ticks || !ticks.length) {
    return null;
  }
  var props = _objectSpread(_objectSpread(_objectSpread({}, defaultsAndInputs), {}, {
    scale
  }, viewBox), {}, {
    radius: viewBox.outerRadius
  });
  return /* @__PURE__ */ React.createElement(Layer, {
    className: clsx("recharts-polar-angle-axis", AXIS_TYPE, props.className)
  }, /* @__PURE__ */ React.createElement(AxisLine, _extends({}, props, {
    ticks
  })), /* @__PURE__ */ React.createElement(Ticks, _extends({}, props, {
    ticks
  })));
};
class PolarAngleAxis extends PureComponent {
  render() {
    if (this.props.radius <= 0) return null;
    return /* @__PURE__ */ React.createElement(SetAngleAxisSettings, {
      id: this.props.angleAxisId,
      scale: this.props.scale,
      type: this.props.type,
      dataKey: this.props.dataKey,
      unit: void 0,
      name: this.props.name,
      allowDuplicatedCategory: false,
      allowDataOverflow: false,
      reversed: this.props.reversed,
      includeHidden: false,
      allowDecimals: this.props.allowDecimals,
      tickCount: this.props.tickCount,
      ticks: this.props.ticks,
      tick: this.props.tick,
      domain: this.props.domain
    }, /* @__PURE__ */ React.createElement(PolarAngleAxisWrapper, this.props));
  }
}
_defineProperty(PolarAngleAxis, "displayName", "PolarAngleAxis");
_defineProperty(PolarAngleAxis, "axisType", AXIS_TYPE);
_defineProperty(PolarAngleAxis, "defaultProps", defaultPolarAngleAxisProps);
export {
  PolarAngleAxis as P,
  selectPolarAxisTicks as a,
  selectPolarGraphicalItemAxisTicks as b,
  PolarRadiusAxis as c,
  selectPolarAxisScale as s
};
