var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_utils = require("../utils");
var import_raf = require("../utils/raf");
var import_use_merged_state = __toESM(require("../hooks/use-merged-state"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
let uid = 0;
function format(rate) {
  return Math.min(Math.max(+rate, 0), 100);
}
function getPath(clockwise, viewBoxSize) {
  const sweepFlag = clockwise ? 1 : 0;
  return `M ${viewBoxSize / 2} ${viewBoxSize / 2} m 0, -500 a 500, 500 0 1, ${sweepFlag} 0, 1000 a 500, 500 0 1, ${sweepFlag} 0, -1000`;
}
const ROTATE_ANGLE_MAP = {
  top: 0,
  right: 90,
  bottom: 180,
  left: 270
};
const Circle = (props) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("circle", prefixCls);
  const id = `rc-circle-${uid++}`;
  const [currentRate, setCurrentRate] = (0, import_react.useState)(() => props.defaultRate || 0);
  const [rate] = (0, import_use_merged_state.default)({
    defaultValue: props.defaultRate,
    value: props.rate
  });
  const viewBoxSize = (0, import_react.useMemo)(() => +props.strokeWidth + 1e3, [props.strokeWidth]);
  const path = (0, import_react.useMemo)(() => getPath(props.clockwise, viewBoxSize), [props.clockwise, viewBoxSize]);
  const svgStyle = (0, import_react.useMemo)(() => {
    const angleValue = ROTATE_ANGLE_MAP[props.startPosition];
    if (angleValue) {
      return {
        transform: `rotate(${angleValue}deg)`
      };
    }
    return void 0;
  }, [props.startPosition]);
  (0, import_react.useEffect)(() => {
    var _a;
    let rafId;
    const startTime = Date.now();
    const startRate = currentRate;
    const endRate = format(rate);
    const duration = Math.abs((startRate - endRate) * 1e3 / +props.speed);
    const animate = () => {
      var _a2;
      const now = Date.now();
      const progress = Math.min((now - startTime) / duration, 1);
      const tempRate = progress * (endRate - startRate) + startRate;
      const crate = format(parseFloat(tempRate.toFixed(1)));
      setCurrentRate(crate);
      (_a2 = props.onChange) == null ? void 0 : _a2.call(props, crate);
      if (endRate > currentRate ? tempRate < endRate : tempRate > endRate) {
        rafId = (0, import_raf.raf)(animate);
      }
    };
    if (props.speed) {
      if (rafId) {
        (0, import_raf.cancelRaf)(rafId);
      }
      rafId = (0, import_raf.raf)(animate);
    } else {
      setCurrentRate(endRate);
      (_a = props.onChange) == null ? void 0 : _a.call(props, endRate);
    }
    return () => {
      (0, import_raf.cancelRaf)(rafId);
    };
  }, [rate]);
  const renderHover = () => {
    const PERIMETER = 3140;
    const {
      strokeWidth,
      strokeLinecap
    } = props;
    const offset = PERIMETER * currentRate / 100;
    const color = (0, import_utils.isObject)(props.color) ? `url(#${id})` : props.color;
    const style = {
      stroke: color,
      strokeWidth: `${+strokeWidth + 1}px`,
      strokeLinecap,
      strokeDasharray: `${offset}px ${PERIMETER}px`
    };
    return /* @__PURE__ */ import_react.default.createElement("path", {
      d: path,
      style,
      className: (0, import_classnames.default)(bem("hover")),
      stroke: color
    });
  };
  const renderLayer = () => {
    const style = {
      fill: props.fill,
      stroke: props.layerColor,
      strokeWidth: `${props.strokeWidth}px`
    };
    return /* @__PURE__ */ import_react.default.createElement("path", {
      className: (0, import_classnames.default)(bem("layer")),
      style,
      d: path
    });
  };
  const renderGradient = () => {
    const {
      color
    } = props;
    if (!(0, import_utils.isObject)(color)) {
      return null;
    }
    const Stops = Object.keys(color).sort((a, b) => parseFloat(a) - parseFloat(b)).map((key, index) => /* @__PURE__ */ import_react.default.createElement("stop", {
      key: index,
      offset: key,
      stopColor: color[key]
    }));
    return /* @__PURE__ */ import_react.default.createElement("defs", null, /* @__PURE__ */ import_react.default.createElement("linearGradient", {
      className: "linearGradient",
      id,
      x1: "100%",
      y1: "0%",
      x2: "0%",
      y2: "0%"
    }, Stops));
  };
  const renderText = () => {
    if (props.text) {
      return /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("text"))
      }, props.text);
    }
    return props.children;
  };
  return /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem(), props.className),
    style: __spreadValues(__spreadValues({}, props.style), (0, import_utils.getSizeStyle)(props.size))
  }, /* @__PURE__ */ import_react.default.createElement("svg", {
    viewBox: `0 0 ${viewBoxSize} ${viewBoxSize}`,
    style: svgStyle
  }, renderGradient(), renderLayer(), renderHover()), renderText());
};
Circle.defaultProps = {
  clockwise: true,
  speed: 100,
  fill: "none",
  strokeWidth: 40,
  startPosition: "top"
};
var stdin_default = Circle;
