{"version":3,"sources":["../src/use-radial-chart.ts"],"sourcesContent":["import type {\n  CSSUIObject,\n  CSSUIProps,\n  PropGetter,\n  RequiredPropGetter,\n} from \"@yamada-ui/core\"\nimport type { Dict } from \"@yamada-ui/utils\"\nimport type { ComponentPropsWithoutRef } from \"react\"\nimport type * as Recharts from \"recharts\"\nimport type { RadialBarProps, RadialChartProps } from \"./chart.types\"\nimport { getVar, useTheme } from \"@yamada-ui/core\"\nimport { cx } from \"@yamada-ui/utils\"\nimport { useCallback, useMemo, useState } from \"react\"\nimport { getClassName, getComponentProps } from \"./chart-utils\"\nimport {\n  radialBarProperties,\n  radialChartProperties,\n} from \"./rechart-properties\"\n\nexport interface UseRadialChartOptions {\n  /**\n   * Chart data.\n   */\n  data: Dict[]\n  /**\n   * The key of a group of data which should be unique in an chart.\n   *\n   * @default 'value'\n   */\n  dataKey?: string\n  /**\n   * Controls angle at which chart ends.\n   *\n   * @default -180\n   */\n  endAngle?: number\n  /**\n   * Controls fill opacity of all pies.\n   *\n   * @default 1\n   */\n  fillOpacity?: [number, number] | number\n  /**\n   * Controls innerRadius of the chart segments.\n   * If it is a number, it is the width of the radius.\n   * For example, `60` means the radius is `60px` and the diameter is `120px`.\n   *\n   * @default '10%'\n   */\n  innerRadius?: number | string\n  /**\n   * Controls thickness of the chart segments. If it is a number, it is calculated as px.\n   * If it is a number, it is the width of the radius.\n   * For example, `60` means the radius is `60px` and the diameter is `120px`.\n   *\n   * @default '80%'\n   */\n  outerRadius?: number | string\n  /**\n   * Controls angle at which chart starts.\n   *\n   * @default 90\n   */\n  startAngle?: number\n  /**\n   * Props passed down to recharts `RadialBarChart` component.\n   */\n  chartProps?: RadialChartProps\n  /**\n   * Props for the radialBar.\n   */\n  radialBarProps?: Partial<RadialBarProps>\n}\n\ninterface UseRadialChartProps extends UseRadialChartOptions {\n  styles: Dict<CSSUIObject | undefined>\n}\n\nexport const useRadialChart = ({\n  data: dataProp,\n  dataKey = \"value\",\n  endAngle = -180,\n  fillOpacity = 1,\n  innerRadius = \"10%\",\n  outerRadius = \"80%\",\n  startAngle = 90,\n  styles,\n  ...rest\n}: UseRadialChartProps) => {\n  const { theme } = useTheme()\n  const [highlightedArea, setHighlightedArea] = useState<null | string>(null)\n  const shouldHighlight = highlightedArea !== null\n  const {\n    background: backgroundProps = {},\n    dimRadialBar: dimRadialBarProps = {},\n    ...computedRadialBarProps\n  } = rest.radialBarProps ?? {}\n\n  const radialVars: CSSUIProps[\"vars\"] = useMemo(\n    () =>\n      dataProp.map(({ color }, index) => ({\n        name: `radial-bar-${index}`,\n        token: \"colors\",\n        value: color ?? \"transparent\",\n      })),\n    [dataProp],\n  )\n\n  const dimRadialBarClassName = useMemo(() => {\n    const resolvedDimRadialBar = {\n      fillOpacity: 0.3,\n      strokeOpacity: 0,\n      ...dimRadialBarProps,\n    }\n\n    return getClassName(resolvedDimRadialBar)(theme)\n  }, [dimRadialBarProps, theme])\n\n  const [chartProps, chartClassName] = useMemo(\n    () =>\n      getComponentProps<Dict, string>(\n        [rest.chartProps ?? {}, radialChartProperties],\n        styles.chart,\n      )(theme),\n    [rest.chartProps, styles.chart, theme],\n  )\n\n  const background = useMemo(\n    () =>\n      getComponentProps<Dict, string>(\n        [backgroundProps, radialBarProperties],\n        styles.background,\n      )(theme, true),\n    [backgroundProps, styles.background, theme],\n  )\n\n  const [radialBarProps, radialBarClassName] = useMemo(\n    () =>\n      getComponentProps<Dict, string>(\n        [computedRadialBarProps, radialBarProperties],\n        styles.radialBar,\n      )(theme),\n    [computedRadialBarProps, styles.radialBar, theme],\n  )\n\n  const data = useMemo(\n    () =>\n      dataProp.map((props, index) => {\n        const {\n          name,\n          color,\n          dimRadialBar = {},\n          value,\n          ...computedProps\n        } = props\n        const dimmed = shouldHighlight && highlightedArea !== name\n        const resolvedProps = {\n          ...computedProps,\n          ...(dimmed ? dimRadialBar : { fillOpacity }),\n        }\n\n        const className = getClassName(\n          resolvedProps,\n          dimmed ? dimRadialBarClassName : undefined,\n        )(theme)\n\n        return {\n          name,\n          className,\n          color,\n          fill: getVar(`radial-bar-${index}`)(theme),\n          value,\n        }\n      }),\n    [\n      dataProp,\n      dimRadialBarClassName,\n      fillOpacity,\n      highlightedArea,\n      shouldHighlight,\n      theme,\n    ],\n  )\n\n  const getRadialChartProps: PropGetter<\n    ComponentPropsWithoutRef<typeof Recharts.RadialBarChart>,\n    ComponentPropsWithoutRef<typeof Recharts.RadialBarChart>\n  > = useCallback(\n    ({ className, ...props } = {}, ref = null) => ({\n      ref,\n      className: cx(className, chartClassName),\n      data,\n      endAngle,\n      innerRadius,\n      outerRadius,\n      startAngle,\n      ...props,\n      ...chartProps,\n    }),\n    [\n      chartClassName,\n      chartProps,\n      data,\n      endAngle,\n      innerRadius,\n      outerRadius,\n      startAngle,\n    ],\n  )\n\n  const getRadialBarProps: RequiredPropGetter<\n    Partial<Recharts.RadialBarProps>,\n    Omit<Recharts.RadialBarProps, \"ref\">\n  > = useCallback(\n    ({ className, ...props }, ref = null) => ({\n      ref,\n      className: cx(className, radialBarClassName),\n      background,\n      dataKey,\n      isAnimationActive: false,\n      ...props,\n      ...radialBarProps,\n    }),\n    [background, dataKey, radialBarClassName, radialBarProps],\n  )\n\n  return {\n    radialVars,\n    setHighlightedArea,\n    getRadialBarProps,\n    getRadialChartProps,\n  }\n}\n\nexport type UseRadialChartReturn = ReturnType<typeof useRadialChart>\n"],"mappings":";;;;;;;;;;;AAUA,SAAS,QAAQ,gBAAgB;AACjC,SAAS,UAAU;AACnB,SAAS,aAAa,SAAS,gBAAgB;AAkExC,IAAM,iBAAiB,CAAC;AAAA,EAC7B,MAAM;AAAA,EACN,UAAU;AAAA,EACV,WAAW;AAAA,EACX,cAAc;AAAA,EACd,cAAc;AAAA,EACd,cAAc;AAAA,EACd,aAAa;AAAA,EACb;AAAA,EACA,GAAG;AACL,MAA2B;AAxF3B;AAyFE,QAAM,EAAE,MAAM,IAAI,SAAS;AAC3B,QAAM,CAAC,iBAAiB,kBAAkB,IAAI,SA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