"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _postcss = _interopRequireWildcard(require("postcss"));
var _postcssPrefixwrap = _interopRequireDefault(require("postcss-prefixwrap"));
var _postcssUrlrebase = _interopRequireDefault(require("postcss-urlrebase"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
 * External dependencies
 */

const cacheByWrapperSelector = new Map();
function transformStyle({
  css,
  ignoredSelectors = [],
  baseURL
}, wrapperSelector = '') {
  // When there is no wrapper selector or base URL, there is no need
  // to transform the CSS. This is most cases because in the default
  // iframed editor, no wrapping is needed, and not many styles
  // provide a base URL.
  if (!wrapperSelector && !baseURL) {
    return css;
  }
  const postcssFriendlyCSS = css.replace(/:root :where\(body\)/g, 'body').replace(/:where\(body\)/g, 'body');
  try {
    return (0, _postcss.default)([wrapperSelector && (0, _postcssPrefixwrap.default)(wrapperSelector, {
      ignoredSelectors: [...ignoredSelectors, wrapperSelector]
    }), baseURL && (0, _postcssUrlrebase.default)({
      rootUrl: baseURL
    })].filter(Boolean)).process(postcssFriendlyCSS, {}).css; // use sync PostCSS API
  } catch (error) {
    if (error instanceof _postcss.CssSyntaxError) {
      // eslint-disable-next-line no-console
      console.warn('wp.blockEditor.transformStyles Failed to transform CSS.', error.message + '\n' + error.showSourceCode(false));
    } else {
      // eslint-disable-next-line no-console
      console.warn('wp.blockEditor.transformStyles Failed to transform CSS.', error);
    }
    return null;
  }
}

/**
 * Applies a series of CSS rule transforms to wrap selectors inside a given class and/or rewrite URLs depending on the parameters passed.
 *
 * @typedef {Object} EditorStyle
 * @property {string}        css              the CSS block(s), as a single string.
 * @property {?string}       baseURL          the base URL to be used as the reference when rewritting urls.
 * @property {?string[]}     ignoredSelectors the selectors not to wrap.
 *
 * @param    {EditorStyle[]} styles           CSS rules.
 * @param    {string}        wrapperSelector  Wrapper selector.
 * @return {Array} converted rules.
 */
const transformStyles = (styles, wrapperSelector = '') => {
  let cache = cacheByWrapperSelector.get(wrapperSelector);
  if (!cache) {
    cache = new WeakMap();
    cacheByWrapperSelector.set(wrapperSelector, cache);
  }
  return styles.map(style => {
    let css = cache.get(style);
    if (!css) {
      css = transformStyle(style, wrapperSelector);
      cache.set(style, css);
    }
    return css;
  });
};
var _default = exports.default = transformStyles;
//# sourceMappingURL=index.js.map