"use strict";exports.__esModule = true;exports.useDrop = void 0;
var _react = require("react");

var _globalObject = require("@skbkontur/global-object");












var useDrop = exports.useDrop = function useDrop(props) {if (props === void 0) {props = {};}
  var _props = props,onDrop = _props.onDrop;

  var droppableRef = (0, _react.useRef)(null);
  var overRef = (0, _react.useRef)(false);
  var timerId = (0, _react.useRef)();
  var _useState = (0, _react.useState)(false),isDraggable = _useState[0],setIsDraggable = _useState[1];

  var clearTimer = (0, _react.useCallback)(function () {
    timerId.current && _globalObject.globalObject.clearTimeout(timerId.current);
  }, []);

  var handleDragOver = (0, _react.useCallback)(
    function (event) {
      event.preventDefault();
      setIsDraggable(true);

      clearTimer();
      timerId.current = _globalObject.globalObject.setTimeout(function () {
        overRef.current = false;
        setIsDraggable(false);
      }, 200);
    },
    [clearTimer]
  );

  var preventDefault = (0, _react.useCallback)(function (event) {
    event.preventDefault();
    event.stopPropagation();
  }, []);

  var handleDrop = (0, _react.useCallback)(
    function (event) {
      preventDefault(event);
      setIsDraggable(false);
      overRef.current = false;

      onDrop == null || onDrop(event);
    },
    [preventDefault, onDrop]
  );

  (0, _react.useEffect)(function () {
    var ref = droppableRef.current;

    if (!ref) {
      return;
    }

    ref.addEventListener('dragenter', preventDefault);
    ref.addEventListener('dragleave', preventDefault);
    ref.addEventListener('dragover', handleDragOver);
    ref.addEventListener('drop', handleDrop);

    return function () {
      ref.removeEventListener('dragenter', preventDefault);
      ref.removeEventListener('dragleave', preventDefault);
      ref.removeEventListener('dragover', handleDragOver);
      ref.removeEventListener('drop', handleDrop);
    };
  }, [handleDrop, handleDragOver, preventDefault]);

  return { isDraggable: isDraggable, ref: droppableRef };
};