// packages/block-editor/src/utils/transform-styles/index.ts
import * as parsel from "parsel-js";
import Processor from "postcss/lib/processor";
import CssSyntaxError from "postcss/lib/css-syntax-error";
import prefixSelector from "postcss-prefix-selector";
import rebaseUrl from "postcss-urlrebase";
var cacheByWrapperSelector = /* @__PURE__ */ new Map();
var ROOT_SELECTOR_TOKENS = [
  { type: "type", content: "body" },
  { type: "type", content: "html" },
  { type: "pseudo-class", content: ":root" },
  { type: "pseudo-class", content: ":where(body)" },
  { type: "pseudo-class", content: ":where(:root)" },
  { type: "pseudo-class", content: ":where(html)" }
];
function prefixRootSelector(prefix, selector) {
  const tokenized = parsel.tokenize(selector);
  const lastRootIndex = tokenized.findLastIndex(({ content, type }) => {
    return ROOT_SELECTOR_TOKENS.some(
      (rootSelector) => content === rootSelector.content && type === rootSelector.type
    );
  });
  let insertionPoint = -1;
  for (let i = lastRootIndex + 1; i < tokenized.length; i++) {
    if (tokenized[i].type === "combinator") {
      insertionPoint = i;
      break;
    }
  }
  const tokenizedPrefix = parsel.tokenize(prefix);
  tokenized.splice(
    // Insert at the insertion point, or the end.
    insertionPoint === -1 ? tokenized.length : insertionPoint,
    0,
    {
      type: "combinator",
      content: " ",
      pos: [0, 0]
    },
    ...tokenizedPrefix
  );
  return parsel.stringify(tokenized);
}
function transformStyle({ css, ignoredSelectors = [], baseURL }, wrapperSelector = "", transformOptions) {
  if (!wrapperSelector && !baseURL) {
    return css;
  }
  try {
    const excludedSelectors = [
      ...ignoredSelectors,
      ...transformOptions?.ignoredSelectors ?? [],
      wrapperSelector
    ];
    return new Processor(
      [
        wrapperSelector && prefixSelector({
          prefix: wrapperSelector,
          transform(prefix, selector, prefixedSelector) {
            if (excludedSelectors.some(
              (excludedSelector) => excludedSelector instanceof RegExp ? selector.match(excludedSelector) : selector.includes(excludedSelector)
            )) {
              return selector;
            }
            const hasRootSelector = ROOT_SELECTOR_TOKENS.some(
              (rootSelector) => selector.startsWith(rootSelector.content)
            );
            if (hasRootSelector) {
              return prefixRootSelector(prefix, selector);
            }
            return prefixedSelector;
          }
        }),
        baseURL && rebaseUrl({ rootUrl: baseURL })
      ].filter(Boolean)
    ).process(css, {}).css;
  } catch (error) {
    if (error instanceof CssSyntaxError) {
      console.warn(
        "wp.blockEditor.transformStyles Failed to transform CSS.",
        error.message + "\n" + error.showSourceCode(false)
      );
    } else {
      console.warn(
        "wp.blockEditor.transformStyles Failed to transform CSS.",
        error
      );
    }
    return null;
  }
}
var transformStyles = (styles, wrapperSelector = "", transformOptions) => {
  let cache = cacheByWrapperSelector.get(wrapperSelector);
  if (!cache) {
    cache = /* @__PURE__ */ new WeakMap();
    cacheByWrapperSelector.set(wrapperSelector, cache);
  }
  return styles.map((style) => {
    let css = cache.get(style);
    if (!css) {
      css = transformStyle(style, wrapperSelector, transformOptions);
      cache.set(style, css);
    }
    return css;
  });
};
var transform_styles_default = transformStyles;
export {
  transform_styles_default as default
};
//# sourceMappingURL=index.mjs.map
