'use strict';

const React = require('react');
const dndUtils = require('../context/dnd-utils.cjs');
const autoScrollConstants = require('../context/auto-scroll-constants.cjs');

const useAutoScroll = () => {
  const autoScrollStateRef = React.useRef({
    isScrolling: false,
    directions: { left: false, right: false, top: false, bottom: false },
    speeds: { horizontal: 0, vertical: 0 }
  });
  const mousePositionRef = React.useRef(null);
  const animationFrameRef = React.useRef(null);
  const performAutoScroll = React.useCallback(() => {
    const iframeElements = dndUtils.getIframeElements();
    if (!iframeElements || !mousePositionRef.current) {
      return;
    }
    const { iframe, window: iframeWindow } = iframeElements;
    const iframeRect = iframe.getBoundingClientRect();
    const iframeMouseX = mousePositionRef.current.x - iframeRect.left;
    const iframeMouseY = mousePositionRef.current.y - iframeRect.top;
    const scrollableWidth = iframeRect.width;
    const scrollableHeight = iframeRect.height;
    const distanceFromLeft = iframeMouseX;
    const distanceFromRight = scrollableWidth - iframeMouseX;
    const distanceFromTop = iframeMouseY;
    const distanceFromBottom = scrollableHeight - iframeMouseY;
    let shouldScrollLeft = distanceFromLeft < autoScrollConstants.AUTO_SCROLL_THRESHOLD;
    let shouldScrollRight = distanceFromRight < autoScrollConstants.AUTO_SCROLL_THRESHOLD;
    let shouldScrollUp = distanceFromTop < autoScrollConstants.AUTO_SCROLL_THRESHOLD;
    let shouldScrollDown = distanceFromBottom < autoScrollConstants.AUTO_SCROLL_THRESHOLD;
    if (shouldScrollLeft && shouldScrollRight) {
      if (distanceFromLeft < distanceFromRight) {
        shouldScrollRight = false;
      } else {
        shouldScrollLeft = false;
      }
    }
    if (shouldScrollUp && shouldScrollDown) {
      if (distanceFromTop < distanceFromBottom) {
        shouldScrollDown = false;
      } else {
        shouldScrollUp = false;
      }
    }
    const leftSpeed = shouldScrollLeft ? autoScrollConstants.calculateScrollSpeed(Math.max(0, distanceFromLeft)) : 0;
    const rightSpeed = shouldScrollRight ? autoScrollConstants.calculateScrollSpeed(Math.max(0, distanceFromRight)) : 0;
    const upSpeed = shouldScrollUp ? autoScrollConstants.calculateScrollSpeed(Math.max(0, distanceFromTop)) : 0;
    const downSpeed = shouldScrollDown ? autoScrollConstants.calculateScrollSpeed(Math.max(0, distanceFromBottom)) : 0;
    const state = autoScrollStateRef.current;
    state.directions = {
      left: shouldScrollLeft,
      right: shouldScrollRight,
      top: shouldScrollUp,
      bottom: shouldScrollDown
    };
    state.speeds = {
      horizontal: leftSpeed || rightSpeed,
      vertical: upSpeed || downSpeed
    };
    state.isScrolling = shouldScrollLeft || shouldScrollRight || shouldScrollUp || shouldScrollDown;
    if (state.isScrolling) {
      let scrollX = 0;
      let scrollY = 0;
      if (shouldScrollLeft) scrollX = -leftSpeed;
      else if (shouldScrollRight) scrollX = rightSpeed;
      if (shouldScrollUp) scrollY = -upSpeed;
      else if (shouldScrollDown) scrollY = downSpeed;
      if (scrollX !== 0 || scrollY !== 0) {
        try {
          iframeWindow.scrollBy(scrollX, scrollY);
        } catch (error) {
          console.warn("Auto-scroll failed:", error);
        }
      }
      animationFrameRef.current = requestAnimationFrame(performAutoScroll);
    } else {
      if (animationFrameRef.current) {
        cancelAnimationFrame(animationFrameRef.current);
        animationFrameRef.current = null;
      }
    }
  }, []);
  const handleParentMouseMove = React.useCallback((event, isDragging) => {
    if (!isDragging) return;
    mousePositionRef.current = { x: event.clientX, y: event.clientY };
    if (!animationFrameRef.current) {
      animationFrameRef.current = requestAnimationFrame(performAutoScroll);
    }
  }, [performAutoScroll]);
  const handleIframeMouseMove = React.useCallback((event, isDragging) => {
    if (!isDragging) return;
    const iframeElements = dndUtils.getIframeElements();
    if (!iframeElements) return;
    const { iframe } = iframeElements;
    const iframeRect = iframe.getBoundingClientRect();
    const parentX = event.clientX + iframeRect.left;
    const parentY = event.clientY + iframeRect.top;
    mousePositionRef.current = { x: parentX, y: parentY };
    if (!animationFrameRef.current) {
      animationFrameRef.current = requestAnimationFrame(performAutoScroll);
    }
  }, [performAutoScroll]);
  const stopAutoScroll = React.useCallback(() => {
    if (animationFrameRef.current) {
      cancelAnimationFrame(animationFrameRef.current);
      animationFrameRef.current = null;
    }
    autoScrollStateRef.current = {
      isScrolling: false,
      directions: { left: false, right: false, top: false, bottom: false },
      speeds: { horizontal: 0, vertical: 0 }
    };
    mousePositionRef.current = null;
  }, []);
  return {
    handleParentMouseMove,
    handleIframeMouseMove,
    stopAutoScroll,
    autoScrollState: autoScrollStateRef.current
  };
};

exports.useAutoScroll = useAutoScroll;
