import "../../node_modules/postcss/lib/postcss.js";
import "../../node_modules/postcss-js/index.js";
import creator from "../../node_modules/postcss-nesting/dist/index.js";
import { createClassSelector } from "./css.js";
import postcss from "../../_virtual/postcss.js";
import index from "../../_virtual/index2.js";
const formatWithClassSelector = (originalObject) => {
  const newFormat = {};
  for (const [key, value] of Object.entries(originalObject)) {
    const { className, ...rest } = value;
    if (Array.isArray(className)) {
      className.forEach((name) => {
        const typographyKey = `.${name}`;
        newFormat[typographyKey] = { ...rest };
      });
    } else {
      const typographyKey = `.${className}`;
      newFormat[typographyKey] = { ...rest };
    }
  }
  return newFormat;
};
const formatNestedObjects = (obj) => {
  const result = {};
  const process = (value) => {
    if (!value || typeof value !== "object" || value === null) return;
    if ("className" in value) {
      const transformed = formatWithClassSelector({ key: value });
      Object.assign(result, transformed);
    } else if ("selector" in value) {
      const { selector, ...properties } = value;
      result[selector] = properties;
    } else {
      Object.values(value).forEach(process);
    }
  };
  process(obj);
  return result;
};
const convertFontToCss = (fontObject) => {
  if (!fontObject.fontFamily || !fontObject.src) {
    console.error("Font object must contain fontFamily and src properties");
    return "";
  }
  const wrapper = {
    "@font-face": {
      fontFamily: fontObject.fontFamily,
      src: fontObject.src,
      ...fontObject.fontWeight ? { fontWeight: fontObject.fontWeight } : {},
      ...fontObject.fontStyle ? { fontStyle: fontObject.fontStyle } : {},
      ...fontObject.fontDisplay ? { fontDisplay: fontObject.fontDisplay } : {}
    }
  };
  return postcss([]).process(wrapper, {
    parser: index
  }).css;
};
const convertToCss = (styles, selector) => {
  if (!styles || !selector) {
    return "";
  }
  const processedStyles = { ...styles };
  Object.entries(processedStyles).forEach(([key, value]) => {
    if (value == null || key === "className") {
      delete processedStyles[key];
    }
  });
  try {
    if (processedStyles.self) {
      const selfProps = processedStyles.self;
      delete processedStyles.self;
      Object.assign(processedStyles, selfProps);
    }
    const transformedStyles = {};
    const keyframes = {};
    Object.entries(processedStyles).forEach(([key, value]) => {
      if (key.startsWith("@keyframes")) {
        keyframes[key] = value;
      } else if (key.startsWith("@")) {
        transformedStyles[key] = value;
      } else if (typeof value === "object" && value !== null) {
        if (key.startsWith(":")) {
          transformedStyles[`&${key}`] = value;
        } else if (!key.startsWith("&")) {
          transformedStyles[`& ${key}`] = value;
        } else {
          transformedStyles[key] = value;
        }
      } else {
        transformedStyles[key] = value;
      }
    });
    const wrapper = {
      ...keyframes,
      [selector]: transformedStyles
    };
    const result = postcss([
      creator({ noIsPseudoSelector: true })
    ]).process(wrapper, {
      parser: index
    }).css;
    return result;
  } catch (error) {
    console.error("Error processing CSS with PostCSS:", error);
    const properties = Object.entries(processedStyles).filter(([key, value]) => typeof value !== "object" && value !== null).map(([key, value]) => {
      const kebabKey = key.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, "$1-$2").toLowerCase();
      return `  ${kebabKey}: ${value};`;
    }).join("\n");
    return `${selector} {
${properties}
}`;
  }
};
const convertToSelectorCSS = (styles, selector) => {
  if (!styles || typeof styles !== "object") {
    return "";
  }
  return convertToCss(styles, selector);
};
const convertToClassSelectorCss = (jssObject) => {
  if (!jssObject || typeof jssObject !== "object" || !("className" in jssObject)) {
    return "";
  }
  const { className, ...styles } = jssObject;
  const selector = createClassSelector(className);
  return convertToCss(styles, selector);
};
export {
  convertFontToCss,
  convertToClassSelectorCss,
  convertToCss,
  convertToSelectorCSS,
  formatNestedObjects,
  formatWithClassSelector
};
//# sourceMappingURL=jss.js.map
