// packages/block-library/src/spacer/edit.js
import clsx from "clsx";
import {
  useBlockProps,
  getCustomValueFromPreset,
  getSpacingPresetCssVar,
  store as blockEditorStore,
  privateApis as blockEditorPrivateApis,
  useBlockEditingMode
} from "@wordpress/block-editor";
import { ResizableBox } from "@wordpress/components";
import { useState, useEffect } from "@wordpress/element";
import { View } from "@wordpress/primitives";
import { useSelect, useDispatch } from "@wordpress/data";
import { unlock } from "../lock-unlock";
import SpacerControls from "./controls";
import { MIN_SPACER_SIZE } from "./constants";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var { useSpacingSizes } = unlock(blockEditorPrivateApis);
var ResizableSpacer = ({
  orientation,
  onResizeStart,
  onResize,
  onResizeStop,
  isSelected,
  isResizing,
  setIsResizing,
  ...props
}) => {
  const getCurrentSize = (elt) => {
    return orientation === "horizontal" ? elt.clientWidth : elt.clientHeight;
  };
  const getNextVal = (elt) => {
    return `${getCurrentSize(elt)}px`;
  };
  return /* @__PURE__ */ jsx(
    ResizableBox,
    {
      className: clsx("block-library-spacer__resize-container", {
        "resize-horizontal": orientation === "horizontal",
        "is-resizing": isResizing,
        "is-selected": isSelected
      }),
      onResizeStart: (_event, _direction, elt) => {
        const nextVal = getNextVal(elt);
        onResizeStart(nextVal);
        onResize(nextVal);
      },
      onResize: (_event, _direction, elt) => {
        onResize(getNextVal(elt));
        if (!isResizing) {
          setIsResizing(true);
        }
      },
      onResizeStop: (_event, _direction, elt) => {
        const nextVal = getCurrentSize(elt);
        onResizeStop(`${nextVal}px`);
        setIsResizing(false);
      },
      __experimentalShowTooltip: true,
      __experimentalTooltipProps: {
        axis: orientation === "horizontal" ? "x" : "y",
        position: "corner",
        isVisible: isResizing
      },
      showHandle: isSelected,
      ...props
    }
  );
};
var SpacerEdit = ({
  attributes,
  isSelected,
  setAttributes,
  toggleSelection,
  context,
  __unstableParentLayout: parentLayout,
  className
}) => {
  const disableCustomSpacingSizes = useSelect((select) => {
    const editorSettings = select(blockEditorStore).getSettings();
    return editorSettings?.disableCustomSpacingSizes;
  });
  const { orientation } = context;
  const {
    orientation: parentOrientation,
    type,
    default: { type: defaultType } = {}
  } = parentLayout || {};
  const isFlexLayout = type === "flex" || !type && defaultType === "flex";
  const inheritedOrientation = !parentOrientation && isFlexLayout ? "horizontal" : parentOrientation || orientation;
  const { height, width, style: blockStyle = {} } = attributes;
  const { layout = {} } = blockStyle;
  const { selfStretch, flexSize } = layout;
  const spacingSizes = useSpacingSizes();
  const [isResizing, setIsResizing] = useState(false);
  const [temporaryHeight, setTemporaryHeight] = useState(null);
  const [temporaryWidth, setTemporaryWidth] = useState(null);
  const onResizeStart = () => toggleSelection(false);
  const onResizeStop = () => toggleSelection(true);
  const { __unstableMarkNextChangeAsNotPersistent } = useDispatch(blockEditorStore);
  const handleOnVerticalResizeStop = (newHeight) => {
    onResizeStop();
    if (isFlexLayout) {
      setAttributes({
        style: {
          ...blockStyle,
          layout: {
            ...layout,
            flexSize: newHeight,
            selfStretch: "fixed"
          }
        }
      });
    }
    setAttributes({ height: newHeight });
    setTemporaryHeight(null);
  };
  const handleOnHorizontalResizeStop = (newWidth) => {
    onResizeStop();
    if (isFlexLayout) {
      setAttributes({
        style: {
          ...blockStyle,
          layout: {
            ...layout,
            flexSize: newWidth,
            selfStretch: "fixed"
          }
        }
      });
    }
    setAttributes({ width: newWidth });
    setTemporaryWidth(null);
  };
  const getHeightForVerticalBlocks = () => {
    if (isFlexLayout) {
      return void 0;
    }
    return temporaryHeight || getSpacingPresetCssVar(height) || void 0;
  };
  const getWidthForHorizontalBlocks = () => {
    if (isFlexLayout) {
      return void 0;
    }
    return temporaryWidth || getSpacingPresetCssVar(width) || void 0;
  };
  const sizeConditionalOnOrientation = inheritedOrientation === "horizontal" ? temporaryWidth || flexSize : temporaryHeight || flexSize;
  const style = {
    height: inheritedOrientation === "horizontal" ? 24 : getHeightForVerticalBlocks(),
    width: inheritedOrientation === "horizontal" ? getWidthForHorizontalBlocks() : void 0,
    // In vertical flex containers, the spacer shrinks to nothing without a minimum width.
    minWidth: inheritedOrientation === "vertical" && isFlexLayout ? 48 : void 0,
    // Add flex-basis so temporary sizes are respected.
    flexBasis: isFlexLayout ? sizeConditionalOnOrientation : void 0,
    // Remove flex-grow when resizing.
    flexGrow: isFlexLayout && isResizing ? 0 : void 0
  };
  const resizableBoxWithOrientation = (blockOrientation) => {
    if (blockOrientation === "horizontal") {
      return /* @__PURE__ */ jsx(
        ResizableSpacer,
        {
          minWidth: MIN_SPACER_SIZE,
          enable: {
            top: false,
            right: true,
            bottom: false,
            left: false,
            topRight: false,
            bottomRight: false,
            bottomLeft: false,
            topLeft: false
          },
          orientation: blockOrientation,
          onResizeStart,
          onResize: setTemporaryWidth,
          onResizeStop: handleOnHorizontalResizeStop,
          isSelected,
          isResizing,
          setIsResizing
        }
      );
    }
    return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(
      ResizableSpacer,
      {
        minHeight: MIN_SPACER_SIZE,
        enable: {
          top: false,
          right: false,
          bottom: true,
          left: false,
          topRight: false,
          bottomRight: false,
          bottomLeft: false,
          topLeft: false
        },
        orientation: blockOrientation,
        onResizeStart,
        onResize: setTemporaryHeight,
        onResizeStop: handleOnVerticalResizeStop,
        isSelected,
        isResizing,
        setIsResizing
      }
    ) });
  };
  useEffect(() => {
    const setAttributesCovertly = (nextAttributes) => {
      __unstableMarkNextChangeAsNotPersistent();
      setAttributes(nextAttributes);
    };
    if (isFlexLayout && selfStretch !== "fill" && selfStretch !== "fit" && flexSize === void 0) {
      if (inheritedOrientation === "horizontal") {
        const newSize = getCustomValueFromPreset(width, spacingSizes) || getCustomValueFromPreset(height, spacingSizes) || "100px";
        setAttributesCovertly({
          width: "0px",
          style: {
            ...blockStyle,
            layout: {
              ...layout,
              flexSize: newSize,
              selfStretch: "fixed"
            }
          }
        });
      } else {
        const newSize = getCustomValueFromPreset(height, spacingSizes) || getCustomValueFromPreset(width, spacingSizes) || "100px";
        setAttributesCovertly({
          height: "0px",
          style: {
            ...blockStyle,
            layout: {
              ...layout,
              flexSize: newSize,
              selfStretch: "fixed"
            }
          }
        });
      }
    } else if (isFlexLayout && (selfStretch === "fill" || selfStretch === "fit")) {
      setAttributesCovertly(
        inheritedOrientation === "horizontal" ? { width: void 0 } : { height: void 0 }
      );
    } else if (!isFlexLayout && (selfStretch || flexSize)) {
      setAttributesCovertly({
        ...inheritedOrientation === "horizontal" ? { width: flexSize } : { height: flexSize },
        style: {
          ...blockStyle,
          layout: {
            ...layout,
            flexSize: void 0,
            selfStretch: void 0
          }
        }
      });
    }
  }, [
    blockStyle,
    flexSize,
    height,
    inheritedOrientation,
    isFlexLayout,
    layout,
    selfStretch,
    setAttributes,
    spacingSizes,
    width,
    __unstableMarkNextChangeAsNotPersistent
  ]);
  const blockEditingMode = useBlockEditingMode();
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(
      View,
      {
        ...useBlockProps({
          style,
          className: clsx(className, {
            "custom-sizes-disabled": disableCustomSpacingSizes
          })
        }),
        children: blockEditingMode === "default" && resizableBoxWithOrientation(inheritedOrientation)
      }
    ),
    !isFlexLayout && /* @__PURE__ */ jsx(
      SpacerControls,
      {
        setAttributes,
        height: temporaryHeight || height,
        width: temporaryWidth || width,
        orientation: inheritedOrientation,
        isResizing
      }
    )
  ] });
};
var edit_default = SpacerEdit;
export {
  edit_default as default
};
//# sourceMappingURL=edit.js.map
