"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimeDayPicker = void 0;
var react_1 = __importStar(require("react"));
var react_native_1 = require("react-native");
var react_native_gesture_handler_1 = require("react-native-gesture-handler");
var TextNative_1 = require("../TextNative");
function TimeDayPicker() {
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(react_native_1.View, { style: { flexDirection: 'row' } },
            react_1.default.createElement(react_native_1.View, { style: { flex: 1 } },
                react_1.default.createElement(TimeClock, { sizeNumber: 24 })),
            react_1.default.createElement(react_native_1.View, { style: { width: 1, backgroundColor: '#eee' } }),
            react_1.default.createElement(react_native_1.View, { style: { flex: 1 } },
                react_1.default.createElement(TimeClock, { sizeNumber: 60 })))));
}
exports.TimeDayPicker = TimeDayPicker;
function TimeClock(_a) {
    var sizeNumber = _a.sizeNumber;
    var panGestureHandlertranslateY = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var posRelativeNumber = react_1.useRef(Array.from({ length: sizeNumber }, function (v, k) { return new react_native_1.Animated.Value(0); })).current;
    var size = 30;
    var offsetOpacity = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var _b = react_1.useState(Array.from({ length: sizeNumber }, function (v, k) { return k; })), items = _b[0], setItems = _b[1];
    var scrollOffsetY = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var animationDecay = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var animationScrollY = react_1.useRef(react_native_1.Animated.multiply(react_native_1.Animated.add(panGestureHandlertranslateY, scrollOffsetY), animationDecay)).current;
    var animation = react_1.useRef();
    var scrollY = react_1.useRef(0);
    var scrollYDecay = react_1.useRef(0);
    function updateOffsetNumbers() {
        var scrollPos = scrollYDecay.current + scrollY.current + size * 20;
        posRelativeNumber.forEach(function (n, i) {
            var x = Math.floor((scrollPos + i * size) / (sizeNumber * size));
            posRelativeNumber[i].setValue(x * (sizeNumber * size));
        });
    }
    function setPosCorrect() {
        var x = Math.abs(((scrollYDecay.current + scrollY.current) % (size * sizeNumber)) % size);
        if (scrollYDecay.current + scrollY.current > 0) {
            if (scrollY.current > size / 3) {
                scrollY.current -= x;
            }
            else {
                scrollY.current += size - x;
            }
        }
        else if (scrollY.current > size / 3) {
            scrollY.current -= size - x;
        }
        else {
            scrollY.current += x;
        }
        react_native_1.Animated.timing(scrollOffsetY, {
            toValue: scrollY.current,
            duration: 100,
            useNativeDriver: true,
        }).start();
    }
    function updateRelativePos(addValue) {
        if (addValue === void 0) { addValue = 0; }
        if (scrollYDecay.current + scrollY.current >= 0) {
            offsetOpacity.setValue(200 +
                size * sizeNumber -
                Math.abs((scrollYDecay.current + addValue + scrollY.current) % (size * sizeNumber)));
        }
        else {
            offsetOpacity.setValue(200 + Math.abs((scrollYDecay.current + addValue + scrollY.current) % (size * sizeNumber)));
        }
    }
    react_1.useEffect(function () {
        animationDecay.addListener(function (_a) {
            var _b;
            var value = _a.value;
            if (Math.abs(value - scrollYDecay.current) < 1) {
                (_b = animation.current) === null || _b === void 0 ? void 0 : _b.stop();
                if (value > 0) {
                    setPosCorrect();
                }
                else {
                    setPosCorrect();
                }
            }
            scrollYDecay.current = value;
            updateRelativePos();
            updateOffsetNumbers();
        });
        panGestureHandlertranslateY.addListener(function (_a) {
            var value = _a.value;
            updateRelativePos(value);
            updateOffsetNumbers();
        });
    }, []);
    var animatedEvent = react_1.useRef(react_native_1.Animated.event([
        {
            nativeEvent: {
                translationY: panGestureHandlertranslateY,
            },
        },
    ], {
        useNativeDriver: true,
    })).current;
    var onHandlerStateChange = function (event) {
        var _a;
        var _b = event.nativeEvent, translationY = _b.translationY, velocityY = _b.velocityY, y = _b.y;
        if (event.nativeEvent.oldState === react_native_gesture_handler_1.State.UNDETERMINED) {
            (_a = animation.current) === null || _a === void 0 ? void 0 : _a.stop();
        }
        scrollY.current += translationY;
        if (event.nativeEvent.oldState === react_native_gesture_handler_1.State.ACTIVE) {
            panGestureHandlertranslateY.setValue(0);
            scrollOffsetY.setValue(scrollY.current);
            if (Math.abs(velocityY / 1500) > 0.15) {
                animation.current = react_native_1.Animated.decay(animationDecay, {
                    velocity: velocityY / 1500,
                    deceleration: 0.99,
                    useNativeDriver: true,
                });
                animation.current.start();
            }
            else {
                updateRelativePos();
                setPosCorrect();
            }
        }
        updateRelativePos();
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(react_native_gesture_handler_1.PanGestureHandler, { maxPointers: 1, onGestureEvent: animatedEvent, onHandlerStateChange: onHandlerStateChange },
            react_1.default.createElement(react_native_1.Animated.View, { style: {
                    overflow: 'hidden',
                    height: size * 3,
                } }, items.map(function (m, i) {
                var A1 = 200 + (i - 1) * size - size * 0.6;
                var B1 = 200 + (i - 1) * size - size * 0.3;
                var A2 = 200 + (i - 1) * size;
                var B2 = 200 + (i - 1) * size + size * 0.3;
                var A3 = 200 + (i - 1) * size + size * 0.6;
                return (react_1.default.createElement(react_native_1.Animated.View, { key: i, style: {
                        flexDirection: 'row',
                        alignItems: 'center',
                        justifyContent: 'center',
                        height: size,
                        opacity: offsetOpacity.interpolate({
                            inputRange: [A1, B1, A2, B2, A3],
                            outputRange: [0.2, 0.9, 1, 0.9, 0.2],
                            extrapolate: 'clamp',
                        }),
                        transform: [
                            {
                                translateY: react_native_1.Animated.subtract(animationScrollY, posRelativeNumber[i]),
                            },
                        ],
                    } },
                    react_1.default.createElement(TextNative_1.TextNative, { size: 20, style: { justifyContent: 'center' } }, m)));
            })))));
}
//# sourceMappingURL=Timerpicker.js.map