var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
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;
};
import React, { useContext, useEffect, useMemo, useState } from "react";
import classnames from "classnames";
import { isObject, getSizeStyle } from "../utils";
import { cancelRaf, raf } from "../utils/raf";
import useMergedState from "../hooks/use-merged-state";
import ConfigProviderContext from "../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
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("circle", prefixCls);
  const id = `rc-circle-${uid++}`;
  const [currentRate, setCurrentRate] = useState(() => props.defaultRate || 0);
  const [rate] = useMergedState({
    defaultValue: props.defaultRate,
    value: props.rate
  });
  const viewBoxSize = useMemo(() => +props.strokeWidth + 1e3, [props.strokeWidth]);
  const path = useMemo(() => getPath(props.clockwise, viewBoxSize), [props.clockwise, viewBoxSize]);
  const svgStyle = useMemo(() => {
    const angleValue = ROTATE_ANGLE_MAP[props.startPosition];
    if (angleValue) {
      return {
        transform: `rotate(${angleValue}deg)`
      };
    }
    return void 0;
  }, [props.startPosition]);
  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 = raf(animate);
      }
    };
    if (props.speed) {
      if (rafId) {
        cancelRaf(rafId);
      }
      rafId = raf(animate);
    } else {
      setCurrentRate(endRate);
      (_a = props.onChange) == null ? void 0 : _a.call(props, endRate);
    }
    return () => {
      cancelRaf(rafId);
    };
  }, [rate]);
  const renderHover = () => {
    const PERIMETER = 3140;
    const {
      strokeWidth,
      strokeLinecap
    } = props;
    const offset = PERIMETER * currentRate / 100;
    const color = isObject(props.color) ? `url(#${id})` : props.color;
    const style = {
      stroke: color,
      strokeWidth: `${+strokeWidth + 1}px`,
      strokeLinecap,
      strokeDasharray: `${offset}px ${PERIMETER}px`
    };
    return /* @__PURE__ */ React.createElement("path", {
      d: path,
      style,
      className: classnames(bem("hover")),
      stroke: color
    });
  };
  const renderLayer = () => {
    const style = {
      fill: props.fill,
      stroke: props.layerColor,
      strokeWidth: `${props.strokeWidth}px`
    };
    return /* @__PURE__ */ React.createElement("path", {
      className: classnames(bem("layer")),
      style,
      d: path
    });
  };
  const renderGradient = () => {
    const {
      color
    } = props;
    if (!isObject(color)) {
      return null;
    }
    const Stops = Object.keys(color).sort((a, b) => parseFloat(a) - parseFloat(b)).map((key, index) => /* @__PURE__ */ React.createElement("stop", {
      key: index,
      offset: key,
      stopColor: color[key]
    }));
    return /* @__PURE__ */ React.createElement("defs", null, /* @__PURE__ */ React.createElement("linearGradient", {
      className: "linearGradient",
      id,
      x1: "100%",
      y1: "0%",
      x2: "0%",
      y2: "0%"
    }, Stops));
  };
  const renderText = () => {
    if (props.text) {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("text"))
      }, props.text);
    }
    return props.children;
  };
  return /* @__PURE__ */ React.createElement("div", {
    className: classnames(bem(), props.className),
    style: __spreadValues(__spreadValues({}, props.style), getSizeStyle(props.size))
  }, /* @__PURE__ */ React.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;
export {
  stdin_default as default
};
