import { ColorError, guard, hsla, parseToHsla } from "color2k";
const colorScale = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
const lScale = [
  0.95,
  0.86,
  0.76,
  0.66,
  0.56,
  0.46,
  0.39,
  0.32,
  0.25,
  0.18,
  0.15,
];
// https://github.com/ricokahler/color2k/blob/main/src/parseToRgba.ts
const hslaRegex = /^hsla?\(\s*([\d.]+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%(?:\s*,\s*([\d.]+))?\s*\)$/i;
function niceParseToHsla(color) {
  const normalizedColor = color.trim();
  const hslaMatch = hslaRegex.exec(normalizedColor);
  if (hslaMatch) {
    const [h, s, l, a] = Array.from(hslaMatch).slice(1).map(parseFloat);
    if (guard(0, 100, s) !== s)
      throw new ColorError(color);
    if (guard(0, 100, l) !== l)
      throw new ColorError(color);
    return [h, s / 100, l, a || 1];
  }
  // Warning -- converts to RGB first, so is lossy
  return parseToHsla(color);
}
function generateScale(color, name = "primary") {
  const [h, s, l, a] = niceParseToHsla(color);
  return lScale
    .map((l) => hsla(h, s, l, a))
    .map((color, index) => {
    return `--sl-color-${name}-${colorScale[index]}: ${color};\n`;
  });
}
/**
 * Generated a Shoelace color scale css string from a base color
 *
 * @param color
 * @param name
 * @returns
 */
export function autoColorScaleCss(color, name = "primary") {
  return generateScale(color, name).join("\n");
}
