// packages/block-editor/src/hooks/dimensions.js
import clsx from "clsx";
import { useState, useEffect, useCallback } from "@wordpress/element";
import { useDispatch, useSelect } from "@wordpress/data";
import { getBlockSupport } from "@wordpress/blocks";
import deprecated from "@wordpress/deprecated";
import InspectorControls from "../components/inspector-controls/index.mjs";
import {
  DimensionsPanel as StylesDimensionsPanel,
  useHasDimensionsPanel
} from "../components/global-styles/index.mjs";
import { MarginVisualizer, PaddingVisualizer } from "./spacing-visualizer.mjs";
import { store as blockEditorStore } from "../store/index.mjs";
import { unlock } from "../lock-unlock.mjs";
import { cleanEmptyObject, shouldSkipSerialization } from "./utils.mjs";
import {
  getStyleForState,
  isDefaultBlockStyleState,
  setStyleForState,
  useBlockStyleState
} from "./block-style-state.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var DIMENSIONS_SUPPORT_KEY = "dimensions";
var SPACING_SUPPORT_KEY = "spacing";
var ALL_SIDES = ["top", "right", "bottom", "left"];
var AXIAL_SIDES = ["vertical", "horizontal"];
function useVisualizer() {
  const [property, setProperty] = useState(false);
  const { hideBlockInterface, showBlockInterface } = unlock(
    useDispatch(blockEditorStore)
  );
  useEffect(() => {
    if (!property) {
      showBlockInterface();
    } else {
      hideBlockInterface();
    }
  }, [property, showBlockInterface, hideBlockInterface]);
  return [property, setProperty];
}
function DimensionsInspectorControl({ children, resetAllFilter }) {
  const attributesResetAllFilter = useCallback(
    (attributes) => {
      const existingStyle = attributes.style;
      const updatedStyle = resetAllFilter(existingStyle);
      return {
        ...attributes,
        style: updatedStyle
      };
    },
    [resetAllFilter]
  );
  return /* @__PURE__ */ jsx(
    InspectorControls,
    {
      group: "dimensions",
      resetAllFilter: attributesResetAllFilter,
      children
    }
  );
}
function DimensionsPanel({ clientId, name, setAttributes, settings }) {
  const selectedState = useBlockStyleState();
  const isStateSelected = !isDefaultBlockStyleState(selectedState);
  const isEnabled = useHasDimensionsPanel(settings, selectedState);
  const style = useSelect(
    (select) => {
      if (!isEnabled) {
        return void 0;
      }
      return select(blockEditorStore).getBlockAttributes(clientId)?.style;
    },
    [clientId, isEnabled]
  );
  const [visualizedProperty, setVisualizedProperty] = useVisualizer();
  const value = isStateSelected ? getStyleForState(style, selectedState) : style;
  const onChange = isStateSelected ? (newStyle) => {
    setAttributes({
      style: setStyleForState(style, selectedState, newStyle)
    });
  } : (newStyle) => {
    setAttributes({
      style: cleanEmptyObject(newStyle)
    });
  };
  if (!isEnabled) {
    return null;
  }
  const defaultDimensionsControls = getBlockSupport(name, [
    DIMENSIONS_SUPPORT_KEY,
    "__experimentalDefaultControls"
  ]);
  const defaultSpacingControls = getBlockSupport(name, [
    SPACING_SUPPORT_KEY,
    "__experimentalDefaultControls"
  ]);
  const defaultControls = {
    // In the block inspector, minHeight and minWidth should not
    // be shown by default unless the block explicitly opts in.
    minHeight: false,
    minWidth: false,
    ...defaultDimensionsControls,
    ...defaultSpacingControls
  };
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(
      StylesDimensionsPanel,
      {
        as: DimensionsInspectorControl,
        panelId: clientId,
        settings,
        value,
        onChange,
        defaultControls,
        styleState: selectedState,
        onVisualize: isStateSelected ? void 0 : setVisualizedProperty
      }
    ),
    !isStateSelected && !!settings?.spacing?.padding && visualizedProperty === "padding" && /* @__PURE__ */ jsx(
      PaddingVisualizer,
      {
        forceShow: visualizedProperty === "padding",
        clientId,
        value
      }
    ),
    !isStateSelected && !!settings?.spacing?.margin && visualizedProperty === "margin" && /* @__PURE__ */ jsx(
      MarginVisualizer,
      {
        forceShow: visualizedProperty === "margin",
        clientId,
        value
      }
    )
  ] });
}
function hasDimensionsSupport(blockName, feature = "any") {
  const support = getBlockSupport(blockName, DIMENSIONS_SUPPORT_KEY);
  if (support === true) {
    return true;
  }
  if (feature === "any") {
    return !!(support?.aspectRatio || !!support?.height || !!support?.minHeight || !!support?.width || !!support?.minWidth);
  }
  return !!support?.[feature];
}
function isExplicitAspectRatio(aspectRatio) {
  if (!aspectRatio) {
    return false;
  }
  return `${aspectRatio}`.trim().toLowerCase() !== "auto";
}
var dimensions_default = {
  useBlockProps,
  attributeKeys: ["height", "minHeight", "width", "style"],
  hasSupport(name) {
    return hasDimensionsSupport(name);
  }
};
function useBlockProps({ name, height, minHeight, style }) {
  if (!hasDimensionsSupport(name, "aspectRatio") || shouldSkipSerialization(name, DIMENSIONS_SUPPORT_KEY, "aspectRatio")) {
    return {};
  }
  const hasExplicitAspectRatio = isExplicitAspectRatio(
    style?.dimensions?.aspectRatio
  );
  const className = clsx({
    "has-aspect-ratio": hasExplicitAspectRatio
  });
  const inlineStyleOverrides = {};
  if (hasExplicitAspectRatio) {
    inlineStyleOverrides.minHeight = "unset";
    inlineStyleOverrides.height = "unset";
  } else if (minHeight || style?.dimensions?.minHeight || height || style?.dimensions?.height) {
    inlineStyleOverrides.aspectRatio = "unset";
  }
  return { className, style: inlineStyleOverrides };
}
function useCustomSides() {
  deprecated("wp.blockEditor.__experimentalUseCustomSides", {
    since: "6.3",
    version: "6.4"
  });
}
export {
  ALL_SIDES,
  AXIAL_SIDES,
  DIMENSIONS_SUPPORT_KEY,
  DimensionsPanel,
  SPACING_SUPPORT_KEY,
  dimensions_default as default,
  hasDimensionsSupport,
  isExplicitAspectRatio,
  useCustomSides
};
//# sourceMappingURL=dimensions.mjs.map
