"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/grid/grid-item-resizer.js
var grid_item_resizer_exports = {};
__export(grid_item_resizer_exports, {
  GridItemResizer: () => GridItemResizer
});
module.exports = __toCommonJS(grid_item_resizer_exports);
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_use_block_refs = require("../block-list/use-block-props/use-block-refs");
var import_cover = __toESM(require("../block-popover/cover"));
var import_utils = require("./utils");
var import_jsx_runtime = require("react/jsx-runtime");
function GridItemResizer({
  clientId,
  bounds,
  onChange,
  parentLayout
}) {
  const blockElement = (0, import_use_block_refs.useBlockElement)(clientId);
  const rootBlockElement = blockElement?.parentElement;
  const { isManualPlacement } = parentLayout;
  if (!blockElement || !rootBlockElement) {
    return null;
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    GridItemResizerInner,
    {
      clientId,
      bounds,
      blockElement,
      rootBlockElement,
      onChange,
      isManualGrid: isManualPlacement && window.__experimentalEnableGridInteractivity
    }
  );
}
function GridItemResizerInner({
  clientId,
  bounds,
  blockElement,
  rootBlockElement,
  onChange,
  isManualGrid
}) {
  const [resizeDirection, setResizeDirection] = (0, import_element.useState)(null);
  const [enableSide, setEnableSide] = (0, import_element.useState)({
    top: false,
    bottom: false,
    left: false,
    right: false
  });
  (0, import_element.useEffect)(() => {
    const observer = new window.ResizeObserver(() => {
      const blockClientRect = blockElement.getBoundingClientRect();
      const rootBlockClientRect = rootBlockElement.getBoundingClientRect();
      setEnableSide({
        top: blockClientRect.top > rootBlockClientRect.top,
        bottom: blockClientRect.bottom < rootBlockClientRect.bottom,
        left: blockClientRect.left > rootBlockClientRect.left,
        right: blockClientRect.right < rootBlockClientRect.right
      });
    });
    observer.observe(blockElement);
    return () => observer.disconnect();
  }, [blockElement, rootBlockElement]);
  const justification = {
    right: "left",
    left: "right"
  };
  const alignment = {
    top: "flex-end",
    bottom: "flex-start"
  };
  const styles = {
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
    ...justification[resizeDirection] && {
      justifyContent: justification[resizeDirection]
    },
    ...alignment[resizeDirection] && {
      alignItems: alignment[resizeDirection]
    }
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_cover.default,
    {
      className: "block-editor-grid-item-resizer",
      clientId,
      __unstablePopoverSlot: "__unstable-block-tools-after",
      additionalStyles: styles,
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_components.ResizableBox,
        {
          className: "block-editor-grid-item-resizer__box",
          size: {
            width: "100%",
            height: "100%"
          },
          enable: {
            bottom: enableSide.bottom,
            bottomLeft: false,
            bottomRight: false,
            left: enableSide.left,
            right: enableSide.right,
            top: enableSide.top,
            topLeft: false,
            topRight: false
          },
          bounds,
          boundsByDirection: true,
          onPointerDown: ({ target, pointerId }) => {
            target.setPointerCapture(pointerId);
          },
          onResizeStart: (event, direction) => {
            setResizeDirection(direction);
          },
          onResizeStop: (event, direction, boxElement) => {
            const columnGap = parseFloat(
              (0, import_utils.getComputedCSS)(rootBlockElement, "column-gap")
            );
            const rowGap = parseFloat(
              (0, import_utils.getComputedCSS)(rootBlockElement, "row-gap")
            );
            const gridColumnTracks = (0, import_utils.getGridTracks)(
              (0, import_utils.getComputedCSS)(
                rootBlockElement,
                "grid-template-columns"
              ),
              columnGap
            );
            const gridRowTracks = (0, import_utils.getGridTracks)(
              (0, import_utils.getComputedCSS)(
                rootBlockElement,
                "grid-template-rows"
              ),
              rowGap
            );
            const rect = new window.DOMRect(
              blockElement.offsetLeft + boxElement.offsetLeft,
              blockElement.offsetTop + boxElement.offsetTop,
              boxElement.offsetWidth,
              boxElement.offsetHeight
            );
            const columnStart = (0, import_utils.getClosestTrack)(gridColumnTracks, rect.left) + 1;
            const rowStart = (0, import_utils.getClosestTrack)(gridRowTracks, rect.top) + 1;
            const columnEnd = (0, import_utils.getClosestTrack)(gridColumnTracks, rect.right, "end") + 1;
            const rowEnd = (0, import_utils.getClosestTrack)(gridRowTracks, rect.bottom, "end") + 1;
            onChange({
              columnSpan: columnEnd - columnStart + 1,
              rowSpan: rowEnd - rowStart + 1,
              columnStart: isManualGrid ? columnStart : void 0,
              rowStart: isManualGrid ? rowStart : void 0
            });
          }
        }
      )
    }
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  GridItemResizer
});
//# sourceMappingURL=grid-item-resizer.js.map
