"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/utils/transform-styles/index.js
var transform_styles_exports = {};
__export(transform_styles_exports, {
  default: () => transform_styles_default
});
module.exports = __toCommonJS(transform_styles_exports);
var parsel = __toESM(require("parsel-js"));
var import_processor = __toESM(require("postcss/lib/processor"));
var import_css_syntax_error = __toESM(require("postcss/lib/css-syntax-error"));
var import_postcss_prefix_selector = __toESM(require("postcss-prefix-selector"));
var import_postcss_urlrebase = __toESM(require("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: " "
    },
    ...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 import_processor.default(
      [
        wrapperSelector && (0, import_postcss_prefix_selector.default)({
          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 && (0, import_postcss_urlrebase.default)({ rootUrl: baseURL })
      ].filter(Boolean)
    ).process(css, {}).css;
  } catch (error) {
    if (error instanceof import_css_syntax_error.default) {
      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;
//# sourceMappingURL=index.js.map
