import { __name } from './chunk-CGILA3WO.mjs';
import { useState, useRef, useEffect } from 'react';

var animationCache = /* @__PURE__ */ new Map();
function useLottie(options) {
  const { src, cache = true } = options;
  const [animationData, setAnimationData] = useState(null);
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState(null);
  const [retryCount, setRetryCount] = useState(0);
  const isMountedRef = useRef(true);
  useEffect(() => {
    isMountedRef.current = true;
    return () => {
      isMountedRef.current = false;
    };
  }, []);
  useEffect(() => {
    if (typeof src === "object" && src !== null) {
      setAnimationData(src);
      setIsLoading(false);
      setError(null);
      return;
    }
    if (typeof src === "string") {
      const loadAnimation = /* @__PURE__ */ __name(async () => {
        if (cache && animationCache.has(src)) {
          if (isMountedRef.current) {
            setAnimationData(animationCache.get(src));
            setIsLoading(false);
            setError(null);
          }
          return;
        }
        if (isMountedRef.current) {
          setIsLoading(true);
          setError(null);
        }
        try {
          const response = await fetch(src);
          if (!response.ok) {
            throw new Error(`Failed to load animation: ${response.status} ${response.statusText}`);
          }
          const data = await response.json();
          if (!data || typeof data !== "object" || !data.v || !data.layers) {
            throw new Error("Invalid Lottie animation data");
          }
          if (cache) {
            animationCache.set(src, data);
          }
          if (isMountedRef.current) {
            setAnimationData(data);
            setIsLoading(false);
          }
        } catch (err) {
          if (isMountedRef.current) {
            setError(err instanceof Error ? err : new Error("Failed to load animation"));
            setIsLoading(false);
          }
        }
      }, "loadAnimation");
      loadAnimation();
    }
  }, [src, cache, retryCount]);
  const retry = /* @__PURE__ */ __name(() => {
    setRetryCount((prev) => prev + 1);
  }, "retry");
  return {
    animationData,
    isLoading,
    error,
    retry
  };
}
__name(useLottie, "useLottie");

export { useLottie };
//# sourceMappingURL=chunk-6JTB2X72.mjs.map
//# sourceMappingURL=chunk-6JTB2X72.mjs.map