"use client";
'use strict';

var react = require('react');

var SHUFFLE_TEXT_DEFAULT_SYMBOLS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
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 getShuffleTextAtFrame(text, frame, {
  charStartFrameOffset = 2,
  shuffleFrameCount = 15,
  pickSymbol
}) {
  const resolveSymbol = pickSymbol != null ? pickSymbol : (() => SHUFFLE_TEXT_DEFAULT_SYMBOLS[Math.floor(Math.random() * SHUFFLE_TEXT_DEFAULT_SYMBOLS.length)]);
  const chars = text.split("");
  let isComplete = true;
  const resolved = chars.map((char, index) => {
    if (char === " ") {
      return " ";
    }
    const startFrame = index * charStartFrameOffset;
    const endFrame = startFrame + shuffleFrameCount;
    if (frame < startFrame) {
      isComplete = false;
      return "";
    }
    if (frame < endFrame) {
      isComplete = false;
      return resolveSymbol();
    }
    return char;
  });
  return { chars: resolved, isComplete };
}
function useShuffleText(text, options) {
  var _a, _b, _c, _d, _e, _f;
  const charStartFrameOffset = (_a = options == null ? void 0 : options.charStartFrameOffset) != null ? _a : 2;
  const shuffleFrameCount = (_b = options == null ? void 0 : options.shuffleFrameCount) != null ? _b : 15;
  const frameMs = (_c = options == null ? void 0 : options.frameMs) != null ? _c : 30;
  const startDelayMs = (_d = options == null ? void 0 : options.startDelayMs) != null ? _d : 0;
  const symbols = (_e = options == null ? void 0 : options.symbols) != null ? _e : SHUFFLE_TEXT_DEFAULT_SYMBOLS;
  const enabled = (_f = options == null ? void 0 : options.enabled) != null ? _f : true;
  const reduceMotion = react.useSyncExternalStore(
    subscribePrefersReducedMotion,
    getPrefersReducedMotionSnapshot,
    getPrefersReducedMotionServerSnapshot
  );
  const [chars, setChars] = react.useState(() => text.split(""));
  const [isComplete, setIsComplete] = react.useState(true);
  const pickSymbolRef = react.useRef(() => {
    var _a2;
    return (_a2 = symbols[0]) != null ? _a2 : "";
  });
  react.useEffect(() => {
    pickSymbolRef.current = () => symbols[Math.floor(Math.random() * symbols.length)];
  }, [symbols]);
  react.useEffect(() => {
    if (!enabled || reduceMotion) {
      const nextChars = text.split("");
      let cancelled2 = false;
      const frameId = requestAnimationFrame(() => {
        if (!cancelled2) {
          setChars(nextChars);
          setIsComplete(true);
        }
      });
      return () => {
        cancelled2 = true;
        cancelAnimationFrame(frameId);
      };
    }
    let cancelled = false;
    let frame = 0;
    let startTimeoutId = 0;
    let frameTimeoutId = 0;
    const update = () => {
      if (cancelled) return;
      const result = getShuffleTextAtFrame(text, frame, {
        charStartFrameOffset,
        shuffleFrameCount,
        pickSymbol: () => pickSymbolRef.current()
      });
      setChars(result.chars);
      setIsComplete(result.isComplete);
      frame += 1;
      if (!result.isComplete) {
        frameTimeoutId = window.setTimeout(() => {
          requestAnimationFrame(update);
        }, frameMs);
      }
    };
    setChars(text.split("").map((char) => char === " " ? " " : ""));
    setIsComplete(false);
    startTimeoutId = window.setTimeout(() => {
      update();
    }, startDelayMs);
    return () => {
      cancelled = true;
      window.clearTimeout(startTimeoutId);
      window.clearTimeout(frameTimeoutId);
    };
  }, [
    charStartFrameOffset,
    enabled,
    frameMs,
    reduceMotion,
    shuffleFrameCount,
    startDelayMs,
    text
  ]);
  return { chars, isComplete };
}

exports.SHUFFLE_TEXT_DEFAULT_SYMBOLS = SHUFFLE_TEXT_DEFAULT_SYMBOLS;
exports.getShuffleTextAtFrame = getShuffleTextAtFrame;
exports.useShuffleText = useShuffleText;
//# sourceMappingURL=use-shuffle-text.cjs.map
//# sourceMappingURL=use-shuffle-text.cjs.map