"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 color_utils_exports = {};
__export(color_utils_exports, {
  calculateShades: () => calculateShades,
  combineHexWithOpacity: () => combineHexWithOpacity,
  getHexColorFromAlpha: () => getHexColorFromAlpha,
  getOpacityFromHex: () => getOpacityFromHex,
  hexToRgba: () => hexToRgba
});
module.exports = __toCommonJS(color_utils_exports);
var import_theme_color_token = require("./../_shared/theme-color-token.js");
const SHADES_RATIOS = { shade1: 0.1, shade2: 0.3, shade3: 0.5 };
const HEX_COLOR_RE = /^#?([0-9a-fA-F]{6})$/;
const parseHexToRgb = (hex) => {
  const match = hex.match(HEX_COLOR_RE);
  if (!match) {
    return { r: 0, g: 0, b: 0 };
  }
  const h = match[1];
  return {
    r: parseInt(h.substring(0, 2), 16),
    g: parseInt(h.substring(2, 4), 16),
    b: parseInt(h.substring(4, 6), 16)
  };
};
const rgbToHexString = ({ r, g, b }) => {
  const toHex = (v) => Math.round(v).toString(16).padStart(2, "0");
  return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
};
const clampChannel = (v) => Math.round(Math.min(255, Math.max(0, v)));
const mixChannels = (dark, light, darkRatio) => ({
  r: clampChannel(darkRatio * dark.r + (1 - darkRatio) * light.r),
  g: clampChannel(darkRatio * dark.g + (1 - darkRatio) * light.g),
  b: clampChannel(darkRatio * dark.b + (1 - darkRatio) * light.b)
});
const calculateShades = (lightHex, darkHex) => {
  const light = parseHexToRgb(lightHex);
  const dark = parseHexToRgb(darkHex);
  return [
    rgbToHexString(mixChannels(dark, light, SHADES_RATIOS.shade1)),
    rgbToHexString(mixChannels(dark, light, SHADES_RATIOS.shade2)),
    rgbToHexString(mixChannels(dark, light, SHADES_RATIOS.shade3))
  ];
};
const hexToRgba = (alphaHex, opacity) => {
  if (!alphaHex) {
    return "";
  }
  if ((0, import_theme_color_token.isThemeColorToken)(alphaHex)) {
    const effectiveOpacity = opacity ?? (0, import_theme_color_token.getThemeColorTokenOpacity)(alphaHex);
    if (effectiveOpacity < 100) {
      const tupleVar = (0, import_theme_color_token.themeColorTokenToCssVar)(alphaHex, "rgbTuple");
      return `rgba(${tupleVar}, ${effectiveOpacity / 100})`;
    }
    return (0, import_theme_color_token.themeColorTokenToCssVar)(alphaHex);
  }
  const opacityPercentage = opacity ?? getOpacityFromHex(alphaHex);
  let hexColor = getHexColorFromAlpha(alphaHex)?.replace(/^#/, "") ?? "";
  if (hexColor.length === 3) {
    hexColor = hexColor.split("").map((char) => char + char).join("");
  }
  const bigint = parseInt(hexColor, 16);
  const r = bigint >> 16 & 255;
  const g = bigint >> 8 & 255;
  const b = bigint & 255;
  return `rgba(${r}, ${g}, ${b}${opacityPercentage !== void 0 ? `, ${opacityPercentage / 100}` : ""})`;
};
const combineHexWithOpacity = (hexColor, opacityPercentage) => {
  if (!hexColor) {
    return;
  }
  if ((0, import_theme_color_token.isThemeColorToken)(hexColor)) {
    return (0, import_theme_color_token.setThemeColorTokenOpacity)(hexColor, opacityPercentage);
  }
  const alpha = Math.round(opacityPercentage / 100 * 255);
  const alphaHex = alpha.toString(16).padStart(2, "0").toUpperCase();
  return `${hexColor?.slice(0, 7)}${alphaHex}`;
};
const getOpacityFromHex = (hexColorWithAlpha) => {
  if ((0, import_theme_color_token.isThemeColorToken)(hexColorWithAlpha)) {
    return (0, import_theme_color_token.getThemeColorTokenOpacity)(hexColorWithAlpha);
  }
  if (!hexColorWithAlpha || hexColorWithAlpha.length === 7) {
    return 100;
  }
  const hex = hexColorWithAlpha.replace("#", "");
  const alphaHex = hex.slice(6, 8);
  const alphaDecimal = parseInt(alphaHex, 16);
  return Math.round(alphaDecimal / 255 * 100);
};
const getHexColorFromAlpha = (hexColorWithAlpha) => {
  if ((0, import_theme_color_token.isThemeColorToken)(hexColorWithAlpha)) {
    return (0, import_theme_color_token.stripThemeColorTokenOpacity)(hexColorWithAlpha);
  }
  if (!hexColorWithAlpha || hexColorWithAlpha.length === 7) {
    return hexColorWithAlpha;
  }
  return hexColorWithAlpha.slice(0, 7);
};
//# sourceMappingURL=color-utils.js.map
