import {
  __spreadProps,
  __spreadValues
} from "./chunk.S3NI7NKU.js";

// src/internal/animate.ts
function cssVar(expression, el) {
  const match = /var\((--[\w-]+)(?:,\s*([^)]+))?\)/.exec(expression);
  if (!match) return expression;
  const [, variable, fallback] = match;
  const value = getComputedStyle(el).getPropertyValue(variable).trim();
  return value || fallback || null;
}
function animateTo(el, keyframes, options) {
  return new Promise((resolve) => {
    var _a;
    if ((options == null ? void 0 : options.duration) === Infinity) {
      throw new Error("Promise-based animations must be finite.");
    }
    const reducedMotion = prefersReducedMotion();
    const duration = typeof options.duration === "string" ? parseDuration((_a = cssVar(options.duration, el)) != null ? _a : "") : options.duration;
    const animation = el.animate(keyframes, __spreadProps(__spreadValues({}, options), {
      duration: reducedMotion && (options == null ? void 0 : options.reducedMotion) !== "allow" ? 0 : duration
    }));
    animation.addEventListener("cancel", resolve, { once: true });
    animation.addEventListener("finish", resolve, { once: true });
  });
}
function parseDuration(delay) {
  delay = delay.toString().toLowerCase();
  if (delay.indexOf("ms") > -1) {
    return parseFloat(delay);
  }
  if (delay.indexOf("s") > -1) {
    return parseFloat(delay) * 1e3;
  }
  return parseFloat(delay);
}
function prefersReducedMotion() {
  const query = window.matchMedia("(prefers-reduced-motion: reduce)");
  return query.matches;
}
function stopAnimations(el) {
  return Promise.all(
    el.getAnimations().map((animation) => {
      return new Promise((resolve) => {
        const handleAnimationEvent = requestAnimationFrame(resolve);
        animation.addEventListener("cancel", () => handleAnimationEvent, { once: true });
        animation.addEventListener("finish", () => handleAnimationEvent, { once: true });
        animation.cancel();
      });
    })
  );
}
function shimKeyframesHeightAuto(keyframes, calculatedHeight) {
  return keyframes.map((keyframe) => __spreadProps(__spreadValues({}, keyframe), {
    height: keyframe.height === "auto" ? `${calculatedHeight}px` : keyframe.height
  }));
}

export {
  cssVar,
  animateTo,
  parseDuration,
  prefersReducedMotion,
  stopAnimations,
  shimKeyframesHeightAuto
};
