"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var theme_color_token_exports = {};
__export(theme_color_token_exports, {
  THEME_COLOR_SLOT_MAP: () => THEME_COLOR_SLOT_MAP,
  getThemeColorTokenOpacity: () => getThemeColorTokenOpacity,
  isThemeColorToken: () => isThemeColorToken,
  resolveDocumentColorToCss: () => resolveDocumentColorToCss,
  resolveDocumentColorToHex: () => resolveDocumentColorToHex,
  setThemeColorTokenOpacity: () => setThemeColorTokenOpacity,
  stripThemeColorTokenOpacity: () => stripThemeColorTokenOpacity,
  themeColorTokenToCssVar: () => themeColorTokenToCssVar,
  themeColorTokenToHex: () => themeColorTokenToHex
});
module.exports = __toCommonJS(theme_color_token_exports);
const THEME_COLOR_SLOTS = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
const THEME_COLOR_SLOT_MAP = {
  "1": "bgColor",
  "2": "textColor",
  "3": "accent1",
  "4": "accent2",
  "5": "accent3",
  "6": "accent4",
  "7": "shade1",
  "8": "shade2",
  "9": "shade3"
};
const THEME_COLOR_TOKEN_REGEXP = /^\$theme-color-([1-9])(?:-(100|[1-9]\d|[0-9]))?$/;
const HEX_COLOR_WITH_OPTIONAL_ALPHA_RE = /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
function resolveDocumentColorToCss(color) {
  if (!color) {
    return void 0;
  }
  const token = parseThemeColorToken(color);
  if (!token) {
    return color;
  }
  if (token.opacity === 100) {
    return themeColorTokenToCssVar(token.baseToken);
  }
  const tupleVar = themeColorTokenToCssVar(token.baseToken, "rgbTuple");
  return `rgba(${tupleVar}, ${token.opacity / 100})`;
}
function resolveDocumentColorToHex(color, palette) {
  if (!color) {
    return void 0;
  }
  if (!isThemeColorToken(color)) {
    return color;
  }
  return themeColorTokenToHex(color, palette) ?? color;
}
function isThemeColorToken(value) {
  return !!parseThemeColorToken(value);
}
function stripThemeColorTokenOpacity(token) {
  return parseThemeColorToken(token)?.baseToken ?? token;
}
function getThemeColorTokenOpacity(token) {
  return parseThemeColorToken(token)?.opacity ?? 100;
}
function setThemeColorTokenOpacity(token, opacity) {
  const parsed = parseThemeColorToken(token);
  if (!parsed) {
    return token;
  }
  const normalizedOpacity = Math.max(0, Math.min(100, Math.round(opacity)));
  return normalizedOpacity === 100 ? parsed.baseToken : `${parsed.baseToken}-${normalizedOpacity}`;
}
function themeColorTokenToCssVar(token, format) {
  const baseToken = stripThemeColorTokenOpacity(token);
  const varName = `--ricos-${baseToken.slice(1)}`;
  return format === "rgbTuple" ? `var(${varName}-tuple)` : `var(${varName})`;
}
function themeColorTokenToHex(token, palette) {
  const parsed = parseThemeColorToken(token);
  if (!parsed) {
    return void 0;
  }
  const field = THEME_COLOR_SLOT_MAP[parsed.slot];
  const value = field && palette[field];
  if (!value) {
    return void 0;
  }
  const hex = palette.format === "rgbTuple" ? rgbTupleToHex(value) : value;
  return applyOpacityToHexColor(hex, parsed.opacity);
}
function parseThemeColorToken(value) {
  if (typeof value !== "string") {
    return void 0;
  }
  const match = value.match(THEME_COLOR_TOKEN_REGEXP);
  if (!match) {
    return void 0;
  }
  const slot = match[1];
  const opacity = match[2] ? Number(match[2]) : 100;
  return {
    slot,
    opacity,
    baseToken: `$theme-color-${slot}`
  };
}
function rgbTupleToHex(tuple) {
  const parts = tuple.split(",").map((s) => parseInt(s.trim(), 10));
  return "#" + parts.slice(0, 3).map((n) => n.toString(16).padStart(2, "0")).join("");
}
function applyOpacityToHexColor(color, opacity) {
  if (opacity === 100) {
    return color;
  }
  const parsedHex = parseHexColor(color);
  if (!parsedHex) {
    return color;
  }
  const alpha = Math.round(opacity / 100 * 255).toString(16).padStart(2, "0").toUpperCase();
  return `${parsedHex.baseHex}${alpha}`;
}
function parseHexColor(color) {
  const normalized = color.trim();
  const match = normalized.match(HEX_COLOR_WITH_OPTIONAL_ALPHA_RE);
  if (!match) {
    return void 0;
  }
  const digits = match[1];
  if (digits.length === 3 || digits.length === 4) {
    const expanded = digits.split("").map((char) => char + char).join("");
    const baseHex = `#${expanded.slice(0, 6)}`;
    const alphaHex = expanded.slice(6, 8);
    return {
      baseHex,
      opacity: alphaHex ? Math.round(parseInt(alphaHex, 16) / 255 * 100) : 100
    };
  }
  return {
    baseHex: `#${digits.slice(0, 6)}`,
    opacity: digits.length === 8 ? Math.round(parseInt(digits.slice(6, 8), 16) / 255 * 100) : 100
  };
}
//# sourceMappingURL=theme-color-token.js.map
