import "../../chunks/chunk.QU3DSPNU.js";
const viewportBreakpoints = {
  "0": "0px",
  "2xs": "320px",
  xs: "425px",
  s: "768px",
  m: "1024px",
  l: "1280px",
  xl: "1440px",
  "2xl": "2560px",
  "3xl": "3840px",
  "4xl": "4320px",
  "5xl": "6016px",
  "6xl": "7680px"
};
const breakpointValueRegex = /^([<|>]=?)?([0-9a-z]+)/;
const controlTokens = ["{", "}", ";", ":", ","];
const whitespace = [" ", "/n"];
function tokenize(source = "") {
  const lexemes = [];
  let scanned = "";
  for (let i = 0; i < source.length; i++) {
    const c = source[i];
    if (!whitespace.includes(c)) scanned += c;
    if (controlTokens.includes(c)) {
      lexemes.push(scanned.slice(0, -1));
      lexemes.push(c);
      scanned = "";
      continue;
    }
    if (whitespace.includes(c) || i === source.length - 1) {
      lexemes.push(scanned);
      scanned = "";
      continue;
    }
  }
  return lexemes.filter((l) => l !== "");
}
function parse(tokens) {
  const tree = [];
  let bpScope = { breakpoint: "-", values: [] };
  const getEmptyBucket = () => ({ sel: "", values: [] });
  let valueBucket = getEmptyBucket();
  for (const t of tokens) {
    if (!controlTokens.includes(t)) {
      valueBucket.values.push(t);
      continue;
    }
    if (t === "{") {
      bpScope = { breakpoint: valueBucket.values.join(","), values: [] };
      valueBucket = getEmptyBucket();
    }
    if (t === ";") {
      if (tree.length === 0) {
        tree.push(bpScope);
      }
      if (valueBucket.values.length > 0) {
        bpScope.values.push(valueBucket);
        valueBucket = getEmptyBucket();
      }
    }
    if (t === ":") {
      const sel = valueBucket.values.pop() ?? "";
      valueBucket.sel = sel;
    }
    if (bpScope && t === "}") {
      bpScope.values.push(valueBucket);
      valueBucket = getEmptyBucket();
      tree.push(bpScope);
    }
  }
  if (valueBucket.values.length > 0) {
    bpScope.values.push(valueBucket);
  }
  if (tree.length === 0) {
    tree.push(bpScope);
  }
  return tree;
}
function parseBreakpoint(bp) {
  const bps = bp.split(",");
  return bps.map((b) => {
    const match = b.trim().match(breakpointValueRegex);
    if (!match) {
      throw new Error(`Invalid breakpoint specifier: ${b}`);
    }
    return { condition: match[1], value: match[2] };
  });
}
function toCss(selector, property, tree, valueTemplate = (v) => v, styleTemplate = (p, vs) => `${p}: ${vs.join(" ")};`) {
  let css = "";
  for (const bp of tree) {
    const bpSpecs = bp.breakpoint === "-" ? [{ condition: ">=", value: "0" }] : parseBreakpoint(bp.breakpoint);
    const query = bpSpecs.map(
      (b) => `(${b.condition?.includes("<") ? "max-width" : "min-width"}: ${viewportBreakpoints[b.value] ?? b.value})`
    ).join(" and ");
    const mq = `@media ${query} {${bp.values.map((bpValues) => {
      let sel = selector;
      if (bpValues.sel.length > 0)
        sel = selector === ":host" ? `:host(:${bpValues.sel})` : `${selector}:${bpValues.sel}`;
      const style = styleTemplate(
        property,
        bpValues.values.map(valueTemplate)
      );
      if (bpValues.sel === "hover")
        return `@media (hover: hover) {${sel}{${style}}}`;
      else return `${sel}{${style}}`;
    }).join("")}}`;
    css += mq;
  }
  return css;
}
export {
  parse,
  toCss,
  tokenize
};
