"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/block-style-state.js
var block_style_state_exports = {};
__export(block_style_state_exports, {
  BlockStyleStateProvider: () => BlockStyleStateProvider,
  DEFAULT_BLOCK_STYLE_STATE: () => DEFAULT_BLOCK_STYLE_STATE,
  getStyleForState: () => getStyleForState,
  hasPseudoBlockStyleState: () => hasPseudoBlockStyleState,
  hasViewportBlockStyleState: () => hasViewportBlockStyleState,
  isDefaultBlockStyleState: () => isDefaultBlockStyleState,
  scopeResetAllFilterToState: () => scopeResetAllFilterToState,
  setStyleForState: () => setStyleForState,
  useBlockStyleState: () => useBlockStyleState
});
module.exports = __toCommonJS(block_style_state_exports);
var import_element = require("@wordpress/element");
var import_utils = require("./utils.cjs");
var import_object = require("../utils/object.cjs");
var DEFAULT_STATE_VALUE = "default";
var DEFAULT_BLOCK_STYLE_STATE = {
  viewport: DEFAULT_STATE_VALUE,
  pseudo: DEFAULT_STATE_VALUE
};
var BlockStyleStateContext = (0, import_element.createContext)(DEFAULT_BLOCK_STYLE_STATE);
var BlockStyleStateProvider = BlockStyleStateContext.Provider;
function useBlockStyleState() {
  return (0, import_element.useContext)(BlockStyleStateContext);
}
function hasViewportBlockStyleState(selectedState) {
  return !!selectedState?.viewport && selectedState.viewport !== DEFAULT_STATE_VALUE;
}
function hasPseudoBlockStyleState(selectedState) {
  return !!selectedState?.pseudo && selectedState.pseudo !== DEFAULT_STATE_VALUE;
}
function isDefaultBlockStyleState(selectedState) {
  return !hasViewportBlockStyleState(selectedState) && !hasPseudoBlockStyleState(selectedState);
}
function getStyleStatePath(selectedState) {
  if (isDefaultBlockStyleState(selectedState)) {
    return [];
  }
  return [selectedState.viewport, selectedState.pseudo].filter(
    (state) => state && state !== DEFAULT_STATE_VALUE
  );
}
function getStyleForState(style, selectedState) {
  const path = getStyleStatePath(selectedState);
  if (!path.length) {
    return style;
  }
  return (0, import_object.getValueFromObjectPath)(style, path);
}
function setStyleForState(style, selectedState, newStyle) {
  const path = getStyleStatePath(selectedState);
  if (!path.length) {
    return (0, import_utils.cleanEmptyObject)(newStyle);
  }
  return (0, import_utils.cleanEmptyObject)((0, import_object.setImmutably)(style, path, newStyle));
}
function scopeResetAllFilterToState(selectedState, resetAllFilter) {
  if (!resetAllFilter || isDefaultBlockStyleState(selectedState)) {
    return resetAllFilter;
  }
  return (attributes) => {
    const existingStateStyle = getStyleForState(attributes?.style, selectedState) || {};
    const updatedStateAttributes = resetAllFilter({
      style: existingStateStyle
    });
    const updatedStateStyle = updatedStateAttributes && typeof updatedStateAttributes === "object" && !Array.isArray(updatedStateAttributes) && Object.prototype.hasOwnProperty.call(
      updatedStateAttributes,
      "style"
    ) ? updatedStateAttributes.style : updatedStateAttributes;
    return {
      style: setStyleForState(
        attributes?.style,
        selectedState,
        updatedStateStyle
      )
    };
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BlockStyleStateProvider,
  DEFAULT_BLOCK_STYLE_STATE,
  getStyleForState,
  hasPseudoBlockStyleState,
  hasViewportBlockStyleState,
  isDefaultBlockStyleState,
  scopeResetAllFilterToState,
  setStyleForState,
  useBlockStyleState
});
//# sourceMappingURL=block-style-state.cjs.map
