"use strict";

import React, { useEffect } from "react";
import { View } from "react-native";
import Animated, { useSharedValue, useAnimatedStyle, withRepeat, withTiming, Easing } from "react-native-reanimated";
import Svg, { Defs, G, LinearGradient, Path, Stop } from "react-native-svg";
import { useIOTheme } from "../../context";
import { IOColors } from "../../core/IOColors";

/**
 * Size scale, 76 is kept for backward compatibility with the old design system but 48 is enough for the new one.
 * It will be removed in the future.
 */
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const strokeMap = {
  24: 3,
  48: 5,
  76: 7
};
export const LoadingSpinner = ({
  color: customColor,
  size = 24,
  durationMs = 850,
  accessibilityHint,
  accessibilityLabel,
  testID = "LoadingSpinnerTestID"
}) => {
  const theme = useIOTheme();
  const rotation = useSharedValue(0);
  const stroke = strokeMap[size];
  const color = customColor ?? IOColors[theme["interactiveElem-default"]];
  useEffect(() => {
    // eslint-disable-next-line functional/immutable-data
    rotation.value = withRepeat(withTiming(360, {
      duration: durationMs,
      easing: Easing.linear
    }), -1, false);
  }, [durationMs, rotation]);
  const animatedStyle = useAnimatedStyle(() => ({
    transform: [{
      rotateZ: `${rotation.value}deg`
    }]
  }));
  return /*#__PURE__*/_jsx(View, {
    style: {
      width: size,
      height: size
    },
    accessible: true,
    accessibilityRole: "progressbar",
    accessibilityHint: accessibilityHint,
    accessibilityLabel: accessibilityLabel,
    testID: testID,
    children: /*#__PURE__*/_jsx(Animated.View, {
      testID: "LoadingSpinnerAnimatedTestID",
      style: animatedStyle,
      children: /*#__PURE__*/_jsxs(Svg, {
        width: size,
        height: size,
        viewBox: `0 0 ${size} ${size}`,
        fill: "none",
        children: [/*#__PURE__*/_jsxs(Defs, {
          children: [/*#__PURE__*/_jsxs(LinearGradient, {
            id: "spinner-secondHalf",
            children: [/*#__PURE__*/_jsx(Stop, {
              offset: "0%",
              stopOpacity: "0",
              stopColor: color
            }), /*#__PURE__*/_jsx(Stop, {
              offset: "100%",
              stopOpacity: "1",
              stopColor: color
            })]
          }), /*#__PURE__*/_jsxs(LinearGradient, {
            id: "spinner-firstHalf",
            children: [/*#__PURE__*/_jsx(Stop, {
              offset: "0%",
              stopOpacity: "1",
              stopColor: color
            }), /*#__PURE__*/_jsx(Stop, {
              offset: "100%",
              stopOpacity: "1",
              stopColor: color
            })]
          })]
        }), /*#__PURE__*/_jsxs(G, {
          strokeWidth: stroke,
          children: [/*#__PURE__*/_jsx(Path, {
            stroke: "url(#spinner-secondHalf)",
            d: `M ${stroke / 2} ${size / 2} A ${size / 2 - stroke / 2} ${size / 2 - stroke / 2} 0 0 1 ${size - stroke / 2} ${size / 2}`
          }), /*#__PURE__*/_jsx(Path, {
            stroke: "url(#spinner-firstHalf)",
            d: `M ${size - stroke / 2} ${size / 2} A ${size / 2 - stroke / 2} ${size / 2 - stroke / 2} 0 0 1 ${stroke / 2} ${size / 2}`
          }), /*#__PURE__*/_jsx(Path, {
            stroke: color,
            strokeLinecap: "round",
            d: `M ${stroke / 2} ${size / 2} A ${size / 2 - stroke / 2} ${size / 2 - stroke / 2} 0 0 1 ${stroke / 2} ${size / 2 - stroke / 4}`
          })]
        })]
      })
    })
  });
};
//# sourceMappingURL=LoadingSpinner.js.map