"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const errors = require("../motion-utils/dist-es-errors.cjs");
const isNumericalString = require("./dist-es-utils-is-numerical-string.cjs");
const isCssVariable = require("./dist-es-render-dom-utils-is-css-variable.cjs");
const splitCSSVariableRegex = (
  // eslint-disable-next-line redos-detector/no-unsafe-regex -- false positive, as it can match a lot of words
  /^var\(--(?:([\w-]+)|([\w-]+), ?([a-zA-Z\d ()%#.,-]+))\)/u
);
function parseCSSVariable(current) {
  const match = splitCSSVariableRegex.exec(current);
  if (!match)
    return [,];
  const [, token1, token2, fallback] = match;
  return [`--${token1 !== null && token1 !== void 0 ? token1 : token2}`, fallback];
}
const maxDepth = 4;
function getVariableValue(current, element, depth = 1) {
  errors.invariant(depth <= maxDepth, `Max CSS variable fallback depth detected in property "${current}". This may indicate a circular fallback dependency.`);
  const [token, fallback] = parseCSSVariable(current);
  if (!token)
    return;
  const resolved = window.getComputedStyle(element).getPropertyValue(token);
  if (resolved) {
    const trimmed = resolved.trim();
    return isNumericalString.isNumericalString(trimmed) ? parseFloat(trimmed) : trimmed;
  }
  return isCssVariable.isCSSVariableToken(fallback) ? getVariableValue(fallback, element, depth + 1) : fallback;
}
exports.getVariableValue = getVariableValue;
exports.parseCSSVariable = parseCSSVariable;
//# sourceMappingURL=dist-es-render-dom-utils-css-variables-conversion.cjs.map
