'use strict';

const core_esm = require('../../../../../../node_modules/.pnpm/@dnd-kit_core@6.3.1_react-dom@19.2.0_react@19.2.0__react@19.2.0/node_modules/@dnd-kit/core/dist/core.esm.cjs');
const dndUtils = require('./dnd-utils.cjs');
require('./collision-debug.cjs');

process.env.NODE_ENV === "development" && false;
const createTransformAwareCollisionDetection = () => {
  return ({ active, droppableRects, droppableContainers, pointerCoordinates, collisionRect }) => {
    if (!pointerCoordinates) {
      return [];
    }
    const transformState = getTransformState();
    const iframeElements = dndUtils.getIframeElements();
    let iframeScrollOffset = { x: 0, y: 0 };
    let iframeRect = { left: 0, top: 0 };
    if (iframeElements) {
      const { iframe, window: iframeWindow } = iframeElements;
      if (iframeWindow) {
        iframeScrollOffset = {
          x: iframeWindow.pageXOffset || iframeWindow.scrollX || 0,
          y: iframeWindow.pageYOffset || iframeWindow.scrollY || 0
        };
      }
      const rect = iframe.getBoundingClientRect();
      iframeRect = { left: rect.left, top: rect.top };
    }
    const freshDroppableRects = new Map(droppableRects);
    const isAtTop = Math.abs(iframeScrollOffset.y) < 5;
    const needsFreshRects = Math.abs(iframeScrollOffset.y) > 10 || isAtTop;
    if (needsFreshRects) {
      droppableContainers.forEach((container) => {
        const element = container.node.current;
        if (element) {
          try {
            const rect = element.getBoundingClientRect();
            if (isAtTop) {
              freshDroppableRects.set(container.id, {
                top: rect.top - iframeRect.top + iframeScrollOffset.y,
                left: rect.left - iframeRect.left + iframeScrollOffset.x,
                bottom: rect.bottom - iframeRect.top + iframeScrollOffset.y,
                right: rect.right - iframeRect.left + iframeScrollOffset.x,
                width: rect.width,
                height: rect.height
              });
            } else {
              freshDroppableRects.set(container.id, {
                top: rect.top - iframeRect.top + iframeScrollOffset.y,
                left: rect.left - iframeRect.left + iframeScrollOffset.x,
                bottom: rect.bottom - iframeRect.top + iframeScrollOffset.y,
                right: rect.right - iframeRect.left + iframeScrollOffset.x,
                width: rect.width,
                height: rect.height
              });
            }
          } catch (error) {
            console.warn("Failed to get fresh rectangle for:", container.id, error);
          }
        }
      });
    }
    const iframeRelativeX = pointerCoordinates.x - iframeRect.left;
    const iframeRelativeY = pointerCoordinates.y - iframeRect.top;
    const transformAdjustedX = (iframeRelativeX - transformState.positionX) / transformState.scale;
    const transformAdjustedY = (iframeRelativeY - transformState.positionY) / transformState.scale;
    let adjustedPointerCoordinates;
    if (isAtTop) {
      adjustedPointerCoordinates = {
        x: transformAdjustedX,
        y: transformAdjustedY
      };
    } else {
      adjustedPointerCoordinates = {
        x: transformAdjustedX + iframeScrollOffset.x,
        y: transformAdjustedY + iframeScrollOffset.y
      };
    }
    const simpleAdjustedCoordinates = {
      x: pointerCoordinates.x - iframeRect.left + iframeScrollOffset.x,
      y: pointerCoordinates.y - iframeRect.top + iframeScrollOffset.y
    };
    iframeScrollOffset.y > 0;
    ({
      scrollY: iframeScrollOffset.y,
      containerCount: droppableContainers.length
    });
    if (isNaN(adjustedPointerCoordinates.x) || isNaN(adjustedPointerCoordinates.y)) {
      console.warn("Invalid adjusted coordinates, falling back to original");
      const fallbackArgs = {
        droppableRects,
        droppableContainers,
        pointerCoordinates};
      return core_esm.pointerWithin(fallbackArgs);
    }
    const adjustedArgs = {
      droppableRects: freshDroppableRects,
      droppableContainers,
      pointerCoordinates: adjustedPointerCoordinates,
      collisionRect
    };
    const pointerCollisions = core_esm.pointerWithin(adjustedArgs);
    if (pointerCollisions.length > 0) {
      return pointerCollisions;
    }
    const rectCollisions = core_esm.rectIntersection(adjustedArgs);
    if (rectCollisions.length > 0) {
      return rectCollisions;
    }
    const simpleArgs = {
      droppableRects: freshDroppableRects,
      droppableContainers,
      pointerCoordinates: simpleAdjustedCoordinates};
    const simpleCollisions = core_esm.pointerWithin(simpleArgs);
    if (simpleCollisions.length > 0) {
      return simpleCollisions;
    }
    const cachedArgs = {
      droppableRects,
      droppableContainers,
      pointerCoordinates: adjustedPointerCoordinates};
    const cachedCollisions = core_esm.pointerWithin(cachedArgs);
    if (cachedCollisions.length > 0) {
      return cachedCollisions;
    }
    const originalArgs = {
      droppableRects,
      droppableContainers,
      pointerCoordinates};
    const originalCollisions = core_esm.pointerWithin(originalArgs);
    return originalCollisions;
  };
};
const getTransformState = () => {
  const transformComponent = document.querySelector('[data-testid="transform-component"]');
  let transformState = { scale: 1, positionX: 0, positionY: 0 };
  if (transformComponent) {
    const computedStyle = window.getComputedStyle(transformComponent);
    const transform = computedStyle.transform;
    if (transform && transform !== "none") {
      const matrixMatch = transform.match(/matrix\(([^)]*)\)/);
      if (matrixMatch && matrixMatch[1]) {
        const values = matrixMatch[1].split(",").map((v) => parseFloat(v.trim()));
        if (values.length >= 6) {
          const scale = values[0];
          const positionX = values[4];
          const positionY = values[5];
          if (scale !== void 0 && positionX !== void 0 && positionY !== void 0) {
            transformState = {
              scale,
              // scaleX
              positionX,
              // translateX
              positionY
              // translateY
            };
          }
        }
      }
    }
  }
  return transformState;
};

exports.createTransformAwareCollisionDetection = createTransformAwareCollisionDetection;
