import "../../node_modules/postcss/lib/postcss.js";
import discardDuplicates from "../../_virtual/index.js";
import postcss from "../../_virtual/postcss.js";
function createBlock(selector, properties) {
  const rules = createRules(properties);
  return rules ? `${selector} {
${rules}
}` : "";
}
function createClassSelector(className) {
  return Array.isArray(className) ? className.map((name) => `.${name}`).join(", ") : `.${className}`;
}
function combineQueryConditions(outerQuery, innerQuery) {
  outerQuery = processMediaQueryString(outerQuery);
  innerQuery = processMediaQueryString(innerQuery);
  const outerType = extractQueryType(outerQuery);
  const innerType = extractQueryType(innerQuery);
  const outerCondition = outerQuery.replace(new RegExp(`^${outerType}\\s*`), "").trim();
  const innerCondition = innerQuery.replace(new RegExp(`^${innerType}\\s*`), "").trim();
  if (outerType !== innerType && innerType !== "") {
    return innerQuery;
  }
  if (outerCondition.includes("${") || innerCondition.includes("${")) {
    const queryType2 = outerType || innerType || "@media";
    return `${queryType2} ${outerCondition} and ${innerCondition}`;
  }
  const queryType = outerType || innerType || "@media";
  return `${queryType} ${outerCondition} and ${innerCondition}`;
}
function createRules(properties) {
  return Object.entries(properties).filter(
    ([_, value]) => value != null && (typeof value !== "object" || Array.isArray(value))
  ).map(([prop, value]) => `  ${toKebabCase(prop)}: ${formatValue(value)};`).join("\n");
}
function combineSelectorWithParent(parentSelector, childSelector) {
  return childSelector.includes("&") ? childSelector.replace(/&/g, parentSelector) : childSelector.startsWith(":") ? `${parentSelector}${childSelector}` : `${parentSelector} ${childSelector}`;
}
function extractQueryType(query) {
  if (query.startsWith("@media")) return "@media";
  if (query.startsWith("@container")) return "@container";
  return "";
}
function formatValue(value) {
  if (Array.isArray(value)) {
    return value.join(" ");
  }
  if (typeof value === "number" && value !== 0) {
    return `${value}px`;
  }
  return String(value);
}
function toKebabCase(property) {
  if (property.startsWith("--")) {
    const variableName = property.substring(2);
    return "--" + variableName.replace(/([A-Z])/g, "-$1").replace(/^-/, "").toLowerCase();
  }
  return property.replace(/([A-Z])/g, "-$1").replace(/^-/, "").toLowerCase();
}
function isPlainObject(value) {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isValidQueryCondition(obj) {
  return typeof obj !== "object" || obj === null || Array.isArray(obj);
}
function processMediaQueryString(queryString) {
  if (!queryString.includes("${")) {
    return queryString;
  }
  return queryString;
}
function processQuery(query, selector, properties) {
  let cleanQuery = processMediaQueryString(query);
  if (!cleanQuery.startsWith("@media") && !cleanQuery.startsWith("@container")) {
    cleanQuery = cleanQuery.includes("container") ? `@container ${cleanQuery}` : `@media ${cleanQuery}`;
  }
  const directProps = {};
  const nestedQueries = [];
  const nestedSelectors = [];
  Object.entries(properties).forEach(([prop, value]) => {
    if (isPlainObject(value)) {
      if (prop.startsWith("@media") || prop.startsWith("@container")) {
        nestedQueries.push([
          processMediaQueryString(prop),
          value
        ]);
      } else {
        nestedSelectors.push([prop, value]);
      }
    } else {
      directProps[prop] = value;
    }
  });
  const rules = createRules(directProps);
  const mainBlock = rules ? `${cleanQuery} {
  ${selector} {
${rules.split("\n").map((line) => `  ${line}`).join("\n")}
  }
}` : "";
  const nestedSelectorBlocks = nestedSelectors.map(
    ([nestedSelector, nestedStyles]) => {
      const baseSelectors = selector.split(",").map((s) => s.trim());
      const nestedSelectorParts = nestedSelector.split(",").map((s) => s.trim());
      const combinedSelectors = [];
      nestedSelectorParts.forEach((nestedPart) => {
        baseSelectors.forEach((baseSel) => {
          combinedSelectors.push(
            combineSelectorWithParent(baseSel, nestedPart)
          );
        });
      });
      const expandedSelector = combinedSelectors.join(", ");
      const nestedRules = createRules(nestedStyles);
      return nestedRules ? `${cleanQuery} {
  ${expandedSelector} {
${nestedRules.split("\n").map((line) => `  ${line}`).join("\n")}
  }
}` : "";
    }
  );
  const nestedQueryBlocks = nestedQueries.map(
    ([nestedQuery, nestedQueryStyles]) => {
      const combinedQuery = combineQueryConditions(cleanQuery, nestedQuery);
      return processQuery(combinedQuery, selector, nestedQueryStyles);
    }
  );
  return [mainBlock, ...nestedSelectorBlocks, ...nestedQueryBlocks].filter(Boolean).join("\n\n");
}
function processNestedSelector(baseSelector, nestedSelector, properties) {
  const baseSelectors = baseSelector.split(",").map((s) => s.trim());
  const nestedSelectorParts = nestedSelector.split(",").map((s) => s.trim());
  const combinedSelectors = [];
  nestedSelectorParts.forEach((nestedPart) => {
    baseSelectors.forEach((baseSel) => {
      combinedSelectors.push(combineSelectorWithParent(baseSel, nestedPart));
    });
  });
  const fullSelector = combinedSelectors.join(", ");
  const directProps = {};
  const queries = [];
  const nestedSelectors = [];
  Object.entries(properties).forEach(([prop, value]) => {
    if ((prop.startsWith("@media") || prop.startsWith("@container")) && isPlainObject(value)) {
      queries.push([
        processMediaQueryString(prop),
        value
      ]);
    } else if (isPlainObject(value)) {
      nestedSelectors.push([prop, value]);
    } else {
      directProps[prop] = value;
    }
  });
  const mainBlock = createBlock(fullSelector, directProps);
  const queryBlocks = queries.map(
    ([query, styles]) => processQuery(query, fullSelector, styles)
  );
  const nestedBlocks = nestedSelectors.map(
    ([selector, styles]) => processNestedSelector(fullSelector, selector, styles)
  );
  return [mainBlock, ...queryBlocks, ...nestedBlocks].filter(Boolean).join("\n\n");
}
const removeDuplicates = async (css) => {
  const result = await postcss([discardDuplicates()]).process(css, {
    from: void 0
  });
  return result.css;
};
export {
  combineQueryConditions,
  combineSelectorWithParent,
  createBlock,
  createClassSelector,
  createRules,
  extractQueryType,
  formatValue,
  isPlainObject,
  isValidQueryCondition,
  processMediaQueryString,
  processNestedSelector,
  processQuery,
  removeDuplicates,
  toKebabCase
};
//# sourceMappingURL=css.js.map
