import "../../../../@dnd-kit_state@0.1.21/node_modules/@dnd-kit/state/dist/index.js";
import { Modifier, configurator } from "../../../../@dnd-kit_abstract@0.1.21/node_modules/@dnd-kit/abstract/index.js";
import { restrictShapeToBoundingRectangle } from "../../../../@dnd-kit_abstract@0.1.21/node_modules/@dnd-kit/abstract/modifiers.js";
import { Rectangle } from "../../../../@dnd-kit_geometry@0.1.21/node_modules/@dnd-kit/geometry/dist/index.js";
import { getBoundingRectangle } from "./utilities.js";
import { signal as d, effect as E } from "../../../../@preact_signals-core@1.12.0/node_modules/@preact/signals-core/dist/signals-core.module.js";
var _RestrictToElement = class _RestrictToElement2 extends Modifier {
  constructor(manager, options) {
    super(manager, options);
    this.boundingRectangle = d(null);
    this.destroy = E(() => {
      if (!this.options) {
        return;
      }
      const { dragOperation } = manager;
      const { status } = dragOperation;
      if (status.initialized) {
        const { element } = this.options;
        const target = typeof element === "function" ? element(dragOperation) : element;
        if (!target) {
          return;
        }
        let timeout;
        const updateBoundingRectangle = () => {
          this.boundingRectangle.value = getBoundingRectangle(target);
        };
        const handleScroll = () => {
          if (timeout) {
            return;
          }
          timeout = setTimeout(() => {
            updateBoundingRectangle();
            timeout = void 0;
          }, 25);
        };
        const resizeObserver = new ResizeObserver(updateBoundingRectangle);
        resizeObserver.observe(target);
        document.addEventListener("scroll", handleScroll, {
          passive: true,
          capture: true
        });
        return () => {
          document.removeEventListener("scroll", handleScroll, {
            capture: true
          });
          resizeObserver.disconnect();
          this.boundingRectangle.value = null;
        };
      }
    });
  }
  apply(operation) {
    const { shape, transform } = operation;
    if (!shape) {
      return transform;
    }
    const boundingRectangle = this.boundingRectangle.value;
    if (!boundingRectangle) {
      return transform;
    }
    const { initial, current } = shape;
    const { height, width } = current.boundingRectangle;
    const left = initial.center.x - width / 2;
    const top = initial.center.y - height / 2;
    const restrictedTransform = restrictShapeToBoundingRectangle(
      new Rectangle(left, top, width, height),
      transform,
      boundingRectangle
    );
    return restrictedTransform;
  }
};
_RestrictToElement.configure = configurator(_RestrictToElement);
var RestrictToElement = _RestrictToElement;
export {
  RestrictToElement
};
//# sourceMappingURL=modifiers.js.map
