import { resolveModulePath } from 'exsolve';

const VARIANT_PREFIX_RE = /^((?:sm|md|lg|xl|2xl|dark):)(.+)$/;
const CSS_PROP_TO_UTILITY_PREFIX = {
  "background-color": "bg",
  "color": "text",
  "border-color": "border",
  "font-size": "text",
  "font-weight": "font",
  "line-height": "leading",
  "letter-spacing": "tracking",
  "opacity": "opacity",
  "width": "w",
  "height": "h",
  "max-width": "max-w",
  "max-height": "max-h",
  "min-width": "min-w",
  "min-height": "min-h",
  "border-radius": "rounded",
  "gap": "gap",
  "padding": "p",
  "padding-top": "pt",
  "padding-right": "pr",
  "padding-bottom": "pb",
  "padding-left": "pl",
  "margin": "m",
  "margin-top": "mt",
  "margin-right": "mr",
  "margin-bottom": "mb",
  "margin-left": "ml",
  "display": ""
};
function stylesToArbitraryClass(styles) {
  const entries = Object.entries(styles);
  if (entries.length !== 1)
    return void 0;
  const [prop, value] = entries[0];
  if (prop === "display")
    return value === "none" ? "hidden" : value;
  const prefix = CSS_PROP_TO_UTILITY_PREFIX[prop];
  if (!prefix)
    return void 0;
  return `${prefix}-[${value}]`;
}
function extractVariantBaseClasses(classes) {
  const baseClasses = /* @__PURE__ */ new Set();
  for (const cls of classes) {
    const m = cls.match(VARIANT_PREFIX_RE);
    if (m?.[2])
      baseClasses.add(m[2]);
  }
  return baseClasses;
}
function resolveVariantPrefixes(classes, resolvedStyles) {
  const prefixOverrideProps = /* @__PURE__ */ new Set();
  for (const cls of classes) {
    const prefixMatch = cls.match(VARIANT_PREFIX_RE);
    if (prefixMatch) {
      const baseStyles = resolvedStyles.get(prefixMatch[2]);
      if (baseStyles) {
        for (const prop of Object.keys(baseStyles))
          prefixOverrideProps.add(prop);
      }
    }
  }
  const result = {};
  for (const cls of classes) {
    const prefixMatch = cls.match(VARIANT_PREFIX_RE);
    if (prefixMatch) {
      const [, prefix, baseClass] = prefixMatch;
      const baseStyles = resolvedStyles.get(baseClass);
      if (baseStyles) {
        const arbitrary = stylesToArbitraryClass(baseStyles);
        result[cls] = arbitrary ? `${prefix}${arbitrary}` : cls;
      } else {
        result[cls] = cls;
      }
      continue;
    }
    const styles = resolvedStyles.get(cls);
    if (styles) {
      const hasConflict = Object.keys(styles).some((prop) => prefixOverrideProps.has(prop));
      if (hasConflict) {
        const arbitrary = stylesToArbitraryClass(styles);
        result[cls] = arbitrary || cls;
      } else {
        result[cls] = styles;
      }
    }
  }
  return result;
}
function detectCssProvider(nuxt) {
  const modules = nuxt.options.modules.flat();
  if (modules.some((m) => typeof m === "string" && m.includes("@unocss/nuxt")))
    return "unocss";
  if (resolveModulePath("tailwindcss", { from: nuxt.options.rootDir, try: true }))
    return "tailwind";
  return null;
}

export { VARIANT_PREFIX_RE, detectCssProvider, extractVariantBaseClasses, resolveVariantPrefixes, stylesToArbitraryClass };
