"use client";
'use strict';

var react = require('react');

function subscribePrefersReducedMotion(onStoreChange) {
  const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
  mq.addEventListener("change", onStoreChange);
  return () => mq.removeEventListener("change", onStoreChange);
}
function getPrefersReducedMotionSnapshot() {
  return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
}
function getPrefersReducedMotionServerSnapshot() {
  return false;
}
function useAtlasSummaryTypewriter(targetText, options) {
  var _a;
  const msPerChar = (_a = options == null ? void 0 : options.msPerChar) != null ? _a : 8;
  const reduceMotion = react.useSyncExternalStore(
    subscribePrefersReducedMotion,
    getPrefersReducedMotionSnapshot,
    getPrefersReducedMotionServerSnapshot
  );
  const [charCount, setCharCount] = react.useState(0);
  const previousTargetRef = react.useRef(null);
  react.useEffect(() => {
    let cancelled = false;
    const frameId = requestAnimationFrame(() => {
      if (cancelled) return;
      if (!targetText) {
        previousTargetRef.current = null;
        setCharCount(0);
        return;
      }
      if (targetText !== previousTargetRef.current) {
        previousTargetRef.current = targetText;
        setCharCount(0);
      }
    });
    return () => {
      cancelled = true;
      cancelAnimationFrame(frameId);
    };
  }, [targetText]);
  react.useEffect(() => {
    if (!targetText || !reduceMotion) {
      return;
    }
    let cancelled = false;
    const frameId = requestAnimationFrame(() => {
      if (!cancelled) {
        setCharCount(targetText.length);
      }
    });
    return () => {
      cancelled = true;
      cancelAnimationFrame(frameId);
    };
  }, [targetText, reduceMotion]);
  react.useEffect(() => {
    if (!targetText || reduceMotion) {
      return;
    }
    if (charCount >= targetText.length) {
      return;
    }
    const timeoutId = window.setTimeout(() => {
      setCharCount((current) => Math.min(current + 1, targetText.length));
    }, msPerChar);
    return () => window.clearTimeout(timeoutId);
  }, [targetText, charCount, msPerChar, reduceMotion]);
  const visibleText = targetText == null ? "" : reduceMotion ? targetText : targetText.slice(0, charCount);
  const isComplete = Boolean(targetText && visibleText.length >= targetText.length);
  return { visibleText, isComplete };
}

exports.useAtlasSummaryTypewriter = useAtlasSummaryTypewriter;
//# sourceMappingURL=use-atlas-summary-typewriter.cjs.map
//# sourceMappingURL=use-atlas-summary-typewriter.cjs.map