import a from "../utils/index.js";
import o from "../channels/reusable.js";
const d = {
  /* VARIABLES */
  re: /^hsla?\(\s*?(-?(?:\d+(?:\.\d+)?|(?:\.\d+))(?:e-?\d+)?(?:deg|grad|rad|turn)?)\s*?(?:,|\s)\s*?(-?(?:\d+(?:\.\d+)?|(?:\.\d+))(?:e-?\d+)?%)\s*?(?:,|\s)\s*?(-?(?:\d+(?:\.\d+)?|(?:\.\d+))(?:e-?\d+)?%)(?:\s*?(?:,|\/)\s*?\+?(-?(?:\d+(?:\.\d+)?|(?:\.\d+))(?:e-?\d+)?(%)?))?\s*?\)$/i,
  hueRe: /^(.+?)(deg|grad|rad|turn)$/i,
  /* HELPERS */
  _hue2deg: (r) => {
    const n = r.match(d.hueRe);
    if (n) {
      const [, e, s] = n;
      switch (s) {
        case "grad":
          return a.channel.clamp.h(parseFloat(e) * 0.9);
        case "rad":
          return a.channel.clamp.h(parseFloat(e) * 180 / Math.PI);
        case "turn":
          return a.channel.clamp.h(parseFloat(e) * 360);
      }
    }
    return a.channel.clamp.h(parseFloat(r));
  },
  /* API */
  parse: (r) => {
    const n = r.charCodeAt(0);
    if (n !== 104 && n !== 72)
      return;
    const e = r.match(d.re);
    if (!e)
      return;
    const [, s, t, c, l, h] = e;
    return o.set({
      h: d._hue2deg(s),
      s: a.channel.clamp.s(parseFloat(t)),
      l: a.channel.clamp.l(parseFloat(c)),
      a: l ? a.channel.clamp.a(h ? parseFloat(l) / 100 : parseFloat(l)) : 1
    }, r);
  },
  stringify: (r) => {
    const { h: n, s: e, l: s, a: t } = r;
    return t < 1 ? `hsla(${a.lang.round(n)}, ${a.lang.round(e)}%, ${a.lang.round(s)}%, ${t})` : `hsl(${a.lang.round(n)}, ${a.lang.round(e)}%, ${a.lang.round(s)}%)`;
  }
};
export {
  d as default
};
