"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-library/src/utils/style-state.js
var style_state_exports = {};
__export(style_state_exports, {
  getActiveDimensionValue: () => getActiveDimensionValue,
  getDimensionResetAttributes: () => getDimensionResetAttributes,
  getDimensionUpdateAttributes: () => getDimensionUpdateAttributes,
  getStateDimensions: () => getStateDimensions,
  getStyleStateKey: () => getStyleStateKey,
  resetDimensions: () => resetDimensions,
  resetStateDimensions: () => resetStateDimensions,
  setStateDimensions: () => setStateDimensions
});
module.exports = __toCommonJS(style_state_exports);
var import_block_editor = require("@wordpress/block-editor");
var import_lock_unlock = require("../lock-unlock.cjs");
var { cleanEmptyObject, getStyleForState, setStyleForState } = (0, import_lock_unlock.unlock)(
  import_block_editor.privateApis
);
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)
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getActiveDimensionValue,
  getDimensionResetAttributes,
  getDimensionUpdateAttributes,
  getStateDimensions,
  getStyleStateKey,
  resetDimensions,
  resetStateDimensions,
  setStateDimensions
});
//# sourceMappingURL=style-state.cjs.map
