// packages/block-library/src/utils/style-state.js
import { privateApis as blockEditorPrivateApis } from "@wordpress/block-editor";
import { unlock } from "../lock-unlock.mjs";
var { cleanEmptyObject, getStyleForState, setStyleForState } = unlock(
  blockEditorPrivateApis
);
function getStateStyle(style, selectedState) {
  return getStyleForState(style, selectedState) || {};
}
function getMappedDimensions(dimensions, dimensionKeyMap = {}) {
  return Object.fromEntries(
    Object.entries(dimensions).map(([key, value]) => [
      dimensionKeyMap[key] || key,
      value
    ])
  );
}
function getControlledDimensions(dimensions, dimensionKeys) {
  if (!dimensionKeys) {
    return dimensions;
  }
  return Object.fromEntries(
    dimensionKeys.map((key) => [key, dimensions[key]])
  );
}
function getStyleStateKey(selectedState) {
  return [
    selectedState?.viewport || "default",
    selectedState?.pseudo || "default"
  ].join(":");
}
function getStateDimensions(style, selectedState) {
  return getStateStyle(style, selectedState)?.dimensions || {};
}
function getActiveDimensionValue(options = {}) {
  const {
    attributes = {},
    style = attributes?.style,
    selectedState,
    hasSelectedStyleState,
    attributeKey,
    styleKey = attributeKey,
    rootValue
  } = options;
  if (hasSelectedStyleState) {
    return getStateDimensions(style, selectedState)?.[styleKey];
  }
  if (Object.hasOwn(options, "rootValue")) {
    return rootValue;
  }
  return attributes?.[attributeKey];
}
function setStateDimensions(style, selectedState, nextDimensions) {
  const stateStyle = getStateStyle(style, selectedState);
  return setStyleForState(
    style,
    selectedState,
    cleanEmptyObject({
      ...stateStyle,
      dimensions: cleanEmptyObject({
        ...stateStyle?.dimensions,
        ...nextDimensions
      })
    })
  );
}
function getDimensionUpdateAttributes({
  style,
  selectedState,
  hasSelectedStyleState,
  nextDimensions,
  dimensionKeyMap,
  dimensionKeys
}) {
  const controlledDimensions = getControlledDimensions(
    nextDimensions,
    dimensionKeys
  );
  if (!hasSelectedStyleState) {
    return controlledDimensions;
  }
  return {
    style: setStateDimensions(
      style,
      selectedState,
      getMappedDimensions(controlledDimensions, dimensionKeyMap)
    )
  };
}
function resetDimensions(style, keys) {
  const dimensionsReset = Object.fromEntries(
    keys.map((key) => [key, void 0])
  );
  return cleanEmptyObject({
    ...style,
    dimensions: cleanEmptyObject({
      ...style?.dimensions,
      ...dimensionsReset
    })
  });
}
function resetStateDimensions(style, selectedState, keys) {
  return setStyleForState(
    style,
    selectedState,
    resetDimensions(getStateStyle(style, selectedState), keys)
  );
}
function getDimensionResetAttributes({
  attributes = {},
  style = attributes?.style,
  selectedState,
  hasSelectedStyleState,
  keys,
  defaultAttributes = {}
}) {
  return {
    ...hasSelectedStyleState ? {} : { ...attributes, ...defaultAttributes },
    style: hasSelectedStyleState ? resetStateDimensions(style, selectedState, keys) : resetDimensions(style, keys)
  };
}
export {
  getActiveDimensionValue,
  getDimensionResetAttributes,
  getDimensionUpdateAttributes,
  getStateDimensions,
  getStyleStateKey,
  resetDimensions,
  resetStateDimensions,
  setStateDimensions
};
//# sourceMappingURL=style-state.mjs.map
