"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCoordinates = void 0;
var react_1 = __importDefault(require("react"));
// TODO: функция вызывается на TouchEvent и PointerEvent хотя больше от этого путанницы
// Тут надо отдельно рефачить и типы и имплементацию чтоб ничего не сломать
exports.getCoordinates = function (_a) {
    var touches = _a.touches, changedTouches = _a.changedTouches, clientX = _a.clientX, clientY = _a.clientY;
    return ((touches && changedTouches)
        ? { clientX: (touches[0] || changedTouches[0]).clientX, clientY: (touches[0] || changedTouches[0]).clientY }
        : { clientX: clientX, clientY: clientY });
};
var Swipeable = /** @class */ (function (_super) {
    __extends(Swipeable, _super);
    function Swipeable() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.swipeStartX = 0;
        _this.swipeStartY = 0;
        _this.deltaX = 0;
        _this.deltaY = 0;
        _this.handleMouseDown = function (event) {
            if (_this.props.children.props.onMouseDown) {
                _this.props.children.props.onMouseDown(event);
            }
            _this.handleSwipeStart(event);
            document.addEventListener('mouseup', _this.handleSwipeEnd);
            document.addEventListener('mouseup', _this.removeListeners);
        };
        _this.handleTouchStart = function (event) {
            if (event.touches && event.touches.length > 1) {
                return;
            }
            if (_this.props.children.props.onTouchStart) {
                _this.props.children.props.onTouchStart(event);
            }
            _this.handleSwipeStart(event);
            document.addEventListener('touchend', _this.handleSwipeEnd);
            document.addEventListener('touchend', _this.removeListeners);
            document.addEventListener('touchcancel', _this.removeListeners);
        };
        _this.handleSwipeStart = function (event) {
            var _a = exports.getCoordinates(event), clientX = _a.clientX, clientY = _a.clientY;
            _this.swipeStartX = clientX;
            _this.swipeStartY = clientY;
        };
        _this.handleSwipeEnd = function (event) {
            var _a = _this.props, delta = _a.delta, onSwipe = _a.onSwipe;
            var _b = exports.getCoordinates(event), clientX = _b.clientX, clientY = _b.clientY;
            _this.deltaX = _this.swipeStartX - clientX;
            _this.deltaY = _this.swipeStartY - clientY;
            if (typeof onSwipe === 'function') {
                if (_this.deltaX > delta) {
                    onSwipe('left');
                }
                else if (_this.deltaX < -delta) {
                    onSwipe('right');
                }
                else if (_this.deltaY > delta) {
                    onSwipe('top');
                }
                else if (_this.deltaY < -delta) {
                    onSwipe('bottom');
                }
            }
        };
        _this.removeListeners = function () {
            document.removeEventListener('mouseup', _this.handleSwipeEnd);
            document.removeEventListener('mouseup', _this.removeListeners);
            document.removeEventListener('touchend', _this.handleSwipeEnd);
            document.removeEventListener('touchend', _this.removeListeners);
            document.removeEventListener('touchcancel', _this.removeListeners);
            _this.swipeStartX = 0;
            _this.swipeStartY = 0;
            _this.deltaX = 0;
            _this.deltaY = 0;
        };
        return _this;
    }
    Swipeable.prototype.componentWillUnmount = function () {
        this.removeListeners();
    };
    Swipeable.prototype.render = function () {
        return react_1.default.cloneElement(this.props.children, {
            onMouseDown: this.handleMouseDown,
            onTouchStart: this.handleTouchStart,
        });
    };
    Swipeable.defaultProps = {
        delta: 100,
    };
    return Swipeable;
}(react_1.default.PureComponent));
exports.default = Swipeable;

//# sourceMappingURL=swipeable.js.map
