"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Swipe", {
    enumerable: true,
    get: function() {
        return Swipe;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _usetouch = require("../../hooks/use-touch");
var _useclientrect = require("../../hooks/use-client-rect");
function preventDefault(event, isStopPropagation) {
    if (typeof event.cancelable !== 'boolean' || event.cancelable) {
        event.preventDefault();
    }
    if (isStopPropagation) {
        event.stopPropagation();
    }
}
var defaultProps = {
    name: ''
};
var Swipe = /*#__PURE__*/ (0, _react.forwardRef)(function(props, instanceRef) {
    var classPrefix = 'nut-swipe';
    var touch = (0, _usetouch.useTouch)();
    var _$_object_spread = (0, _object_spread._)({}, defaultProps, props), children = _$_object_spread.children, className = _$_object_spread.className, style = _$_object_spread.style;
    var root = (0, _react.useRef)();
    var opened = (0, _react.useRef)(false);
    var lockClick = (0, _react.useRef)(false);
    var startOffset = (0, _react.useRef)(0);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)({
        offset: 0,
        dragging: false
    }), 2), state = _useState[0], setState = _useState[1];
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)({
        left: 0,
        right: 0
    }), 2), actionWidth = _useState1[0], setActionWidth = _useState1[1];
    var wrapperStyle = {
        transform: "translate3d(".concat(state.offset, "px, 0, 0)"),
        transitionDuration: state.dragging ? '0s' : '.6s'
    };
    var leftWidth = actionWidth.left;
    var rightWidth = actionWidth.right;
    var onTouchStart = function onTouchStart(event) {
        if (!props.disabled) {
            var _props_onTouchStart;
            startOffset.current = state.offset;
            touch.start(event);
            (_props_onTouchStart = props.onTouchStart) === null || _props_onTouchStart === void 0 ? void 0 : _props_onTouchStart.call(props, event);
        }
    };
    var onTouchMove = function onTouchMove(event) {
        var _props_onTouchMove;
        if (props.disabled) {
            return;
        }
        touch.move(event);
        (_props_onTouchMove = props.onTouchMove) === null || _props_onTouchMove === void 0 ? void 0 : _props_onTouchMove.call(props, event);
        if (touch.isHorizontal()) {
            lockClick.current = true;
            var newState = (0, _object_spread_props._)((0, _object_spread._)({}, state), {
                dragging: true
            });
            var isEdge = !opened || touch.deltaX.current * startOffset.current < 0;
            if (isEdge) {
                preventDefault(event, true);
            }
            newState.offset = rangeCalculation(touch.deltaX.current + startOffset.current, -rightWidth, leftWidth);
            setState(newState);
        }
    };
    var onTouchEnd = function onTouchEnd(event) {
        if (state.dragging) {
            var _props_onTouchEnd;
            setState(function(v) {
                return (0, _object_spread_props._)((0, _object_spread._)({}, v), {
                    dragging: false
                });
            });
            toggle(state.offset > 0 ? 'left' : 'right');
            setTimeout(function() {
                lockClick.current = false;
            }, 0);
            (_props_onTouchEnd = props.onTouchEnd) === null || _props_onTouchEnd === void 0 ? void 0 : _props_onTouchEnd.call(props, event);
        }
    };
    var toggle = function toggle(side) {
        var offset = Math.abs(state.offset);
        var base = 0.3;
        var baseNum = opened ? 1 - base : base;
        var width = side === 'left' ? leftWidth : rightWidth;
        if (width && offset > Number(width) * baseNum) {
            open(side);
        } else {
            close(side);
        }
    };
    var open = function open(side) {
        var _props_onOpen;
        opened.current = true;
        var offset = side === 'left' ? leftWidth : -rightWidth;
        var name = props.name;
        (_props_onOpen = props.onOpen) === null || _props_onOpen === void 0 ? void 0 : _props_onOpen.call(props, {
            name: name,
            position: side
        });
        setState(function(v) {
            return (0, _object_spread_props._)((0, _object_spread._)({}, v), {
                offset: Number(offset) || 0
            });
        });
    };
    var close = function close(position) {
        if (opened.current) {
            var _props_onClose;
            opened.current = false;
            (_props_onClose = props.onClose) === null || _props_onClose === void 0 ? void 0 : _props_onClose.call(props, {
                name: props.name,
                position: position || 'left'
            });
        }
        setState(function(v) {
            return (0, _object_spread_props._)((0, _object_spread._)({}, v), {
                offset: 0
            });
        });
    };
    var rangeCalculation = function rangeCalculation(num, min, max) {
        return Math.min(Math.max(Number(num), Number(min)), Number(max));
    };
    var getNodeWidth = function getNodeWidth(node) {
        if (node) {
            var ele = (0, _useclientrect.getRect)(node);
            return ele.width;
        }
        return 0;
    };
    var leftRef = (0, _react.useCallback)(function(node) {
        if (node !== null) {
            setActionWidth(function(v) {
                return (0, _object_spread_props._)((0, _object_spread._)({}, v), {
                    left: getNodeWidth(node)
                });
            });
        }
    }, [
        props.leftAction
    ]);
    var rightRef = (0, _react.useCallback)(function(node) {
        if (node !== null) {
            setActionWidth(function(v) {
                return (0, _object_spread_props._)((0, _object_spread._)({}, v), {
                    right: getNodeWidth(node)
                });
            });
        }
    }, [
        props.rightAction
    ]);
    var renderActionContent = function renderActionContent(side, measuredRef) {
        if (props["".concat(side, "Action")]) {
            return /*#__PURE__*/ _react.default.createElement("div", {
                ref: measuredRef,
                className: "".concat(classPrefix, "-").concat(side),
                onClick: function onClick(e) {
                    return handleOperate(e, side);
                }
            }, props["".concat(side, "Action")]);
        }
        return null;
    };
    var handleOperate = function handleOperate(event, position) {
        event.stopPropagation();
        if (props.beforeClose) {
            props.beforeClose(position);
        } else {
            props.onActionClick && props.onActionClick(event, position);
        }
    };
    (0, _react.useImperativeHandle)(instanceRef, function() {
        return {
            open: open,
            close: function close1() {
                return close();
            }
        };
    });
    (0, _react.useEffect)(function() {
        var handler = function handler(event) {
            var targets = [
                root
            ];
            if (targets.some(function(targetItem) {
                var targetElement = targetItem.current || targetItem;
                return !targetElement || (targetElement === null || targetElement === void 0 ? void 0 : targetElement.contains(event.target));
            })) {
                return;
            }
            close();
        };
        document.addEventListener('touchstart', handler);
        return function() {
            document.removeEventListener('touchstart', handler);
        };
    }, []);
    return /*#__PURE__*/ _react.default.createElement("div", {
        ref: root,
        className: (0, _classnames.default)(classPrefix, className),
        onTouchStart: function onTouchStart1(e) {
            return onTouchStart(e);
        },
        onTouchMove: function onTouchMove1(e) {
            return onTouchMove(e);
        },
        onTouchEnd: function onTouchEnd1(e) {
            return onTouchEnd(e);
        },
        style: style
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-wrapper"),
        style: wrapperStyle
    }, renderActionContent('left', leftRef), children, renderActionContent('right', rightRef)));
});
Swipe.displayName = 'NutSwipe';
