import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
const Draggable = (param)=>{
    let { children, updateTransform = (transformStr, tx, ty, tdom)=>{
        tdom.style.transform = transformStr;
    } } = param;
    const ref = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)();
    const positionRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)({
        startX: 0,
        startY: 0,
        dx: 0,
        dy: 0,
        tx: 0,
        ty: 0
    });
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        const node = ref.current;
        if (!node) return;
        const start = (event)=>{
            if (0 !== event.button) return;
            document.addEventListener('mousemove', docMove);
            positionRef.current.startX = event.pageX - positionRef.current.dx;
            positionRef.current.startY = event.pageY - positionRef.current.dy;
        };
        const docMove = (event)=>{
            const tx = event.pageX - positionRef.current.startX;
            const ty = event.pageY - positionRef.current.startY;
            const transformStr = "translate(".concat(tx, "px,").concat(ty, "px)");
            updateTransform(transformStr, tx, ty, node);
            positionRef.current.dx = tx;
            positionRef.current.dy = ty;
        };
        const docMouseUp = (event)=>{
            document.removeEventListener('mousemove', docMove);
        };
        node.addEventListener('mousedown', start);
        document.addEventListener('mouseup', docMouseUp);
        return ()=>{
            node.removeEventListener('mousedown', start);
            document.removeEventListener('mouseup', docMouseUp);
        };
    }, [
        updateTransform
    ]);
    const newStyle = {
        ...children.props.style,
        cursor: 'move',
        userSelect: 'none'
    };
    return /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].cloneElement(__WEBPACK_EXTERNAL_MODULE_react__["default"].Children.only(children), {
        ref,
        style: newStyle
    });
};
export { Draggable };
