// packages/block-editor/src/hooks/block-style-state.js
import { createContext, useContext } from "@wordpress/element";
import { cleanEmptyObject } from "./utils.mjs";
import { getValueFromObjectPath, setImmutably } from "../utils/object.mjs";
var DEFAULT_STATE_VALUE = "default";
var DEFAULT_BLOCK_STYLE_STATE = {
  viewport: DEFAULT_STATE_VALUE,
  pseudo: DEFAULT_STATE_VALUE
};
var BlockStyleStateContext = createContext(DEFAULT_BLOCK_STYLE_STATE);
var BlockStyleStateProvider = BlockStyleStateContext.Provider;
function useBlockStyleState() {
  return 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 getValueFromObjectPath(style, path);
}
function setStyleForState(style, selectedState, newStyle) {
  const path = getStyleStatePath(selectedState);
  if (!path.length) {
    return cleanEmptyObject(newStyle);
  }
  return cleanEmptyObject(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
      )
    };
  };
}
export {
  BlockStyleStateProvider,
  DEFAULT_BLOCK_STYLE_STATE,
  getStyleForState,
  hasPseudoBlockStyleState,
  hasViewportBlockStyleState,
  isDefaultBlockStyleState,
  scopeResetAllFilterToState,
  setStyleForState,
  useBlockStyleState
};
//# sourceMappingURL=block-style-state.mjs.map
