// packages/block-editor/src/hooks/state-utils.js
import { getBlockType } from "@wordpress/blocks";
import { splitSelectorList } from "@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 splitSelectorList(baseSelector).map((selector) => `${selector.trim()}${suffix}`).join(", ");
  }
  const baseSelectors = splitSelectorList(baseSelector).filter(
    (selector) => selector.trim()
  );
  const selectors = 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 = 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 = getBlockType(name)?.selectors?.root;
  return buildScopedBlockSelector(baseSelector, rootSelector, state);
}
function buildCanvasStateSelector(clientId, name) {
  const rootSelector = getBlockType(name)?.selectors?.root;
  return buildScopedBlockSelector(
    `[data-block="${clientId}"]`,
    rootSelector
  );
}
export {
  buildCanvasStateSelector,
  buildPseudoStyleStateSelector,
  buildRootStyleStateSelector,
  buildScopedBlockSelector,
  buildStateSelector,
  getRelativeRootSelector
};
//# sourceMappingURL=state-utils.mjs.map
