"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/hooks/state-utils.js
var state_utils_exports = {};
__export(state_utils_exports, {
  buildCanvasStateSelector: () => buildCanvasStateSelector,
  buildPseudoStyleStateSelector: () => buildPseudoStyleStateSelector,
  buildRootStyleStateSelector: () => buildRootStyleStateSelector,
  buildScopedBlockSelector: () => buildScopedBlockSelector,
  buildStateSelector: () => buildStateSelector,
  getRelativeRootSelector: () => getRelativeRootSelector
});
module.exports = __toCommonJS(state_utils_exports);
var import_blocks = require("@wordpress/blocks");
var import_global_styles_engine = require("@wordpress/global-styles-engine");
function getRelativeRootSelector(rootSelector) {
  const match = rootSelector.trim().match(/^[^ >+~]+[ >+~](.*)$/);
  if (!match) {
    return null;
  }
  const rest = match[1].trim();
  return rest || null;
}
function buildScopedBlockSelector(baseSelector, blockSelector, suffix = "") {
  if (typeof blockSelector !== "string" || !blockSelector) {
    return (0, import_global_styles_engine.splitSelectorList)(baseSelector).map((selector) => `${selector.trim()}${suffix}`).join(", ");
  }
  const baseSelectors = (0, import_global_styles_engine.splitSelectorList)(baseSelector).filter(
    (selector) => selector.trim()
  );
  const selectors = (0, import_global_styles_engine.splitSelectorList)(blockSelector).filter(
    (selector) => selector.trim()
  );
  if (!selectors.length) {
    return baseSelectors.map((selector) => `${selector.trim()}${suffix}`).join(", ");
  }
  return selectors.map((selector) => {
    selector = selector.trim();
    const match = selector.match(/^([.#]?[-_a-zA-Z0-9]+|\[[^\]]+\])/);
    if (match) {
      return baseSelectors.map(
        (base) => `${base.trim()}${selector.slice(
          match[0].length
        )}${suffix}`
      ).join(", ");
    }
    return baseSelectors.map((base) => `${base.trim()}${suffix}`).join(", ");
  }).join(", ");
}
function buildRootStyleStateSelector(baseSelector, name) {
  const rootSelector = (0, import_blocks.getBlockType)(name)?.selectors?.root;
  return buildScopedBlockSelector(baseSelector, rootSelector);
}
function buildPseudoStyleStateSelector(baseSelector, name, state) {
  return `${buildRootStyleStateSelector(baseSelector, name)}${state}`;
}
function buildStateSelector(baseSelector, name, state) {
  const rootSelector = (0, import_blocks.getBlockType)(name)?.selectors?.root;
  return buildScopedBlockSelector(baseSelector, rootSelector, state);
}
function buildCanvasStateSelector(clientId, name) {
  const rootSelector = (0, import_blocks.getBlockType)(name)?.selectors?.root;
  return buildScopedBlockSelector(
    `[data-block="${clientId}"]`,
    rootSelector
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildCanvasStateSelector,
  buildPseudoStyleStateSelector,
  buildRootStyleStateSelector,
  buildScopedBlockSelector,
  buildStateSelector,
  getRelativeRootSelector
});
//# sourceMappingURL=state-utils.cjs.map
