"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.ActionSheet = 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 services_1 = require("../../services");
var native_1 = require("../Flex/native");
var ActionSheet_style_1 = require("./ActionSheet.style");
var operators_1 = require("rxjs/operators");
var ActionSheetObs = new services_1.Event({});
exports.ActionSheet = function (props) {
    var offset = 0;
    var windowHeight = react_native_1.Dimensions.get('window').height;
    var _a = react_1.useState(false), opened = _a[0], setOpened = _a[1];
    var _b = react_1.useState({}), items = _b[0], setItems = _b[1];
    var _c = react_1.useState(0), heightItems = _c[0], setHeightItems = _c[1];
    var add = react_1.useRef(new react_native_1.Animated.Value(1)).current;
    var translateY = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var close = function () {
        react_native_1.Animated.timing(add, {
            toValue: 1,
            duration: 200,
            useNativeDriver: true,
        }).start(function () {
            setOpened(false);
        });
    };
    react_1.useEffect(function () {
        var obs = ActionSheetObs.observable.pipe(operators_1.skip(1)).subscribe(function (value) {
            setItems(value);
            setOpened(true);
            react_native_1.Animated.timing(add, {
                toValue: 0,
                duration: 200,
                useNativeDriver: true,
            }).start();
        });
        return function () { return obs.unsubscribe(); };
    }, []);
    react_1.useEffect(function () {
        var backAction = function () {
            if (opened) {
                close();
                return true;
            }
            return false;
        };
        var backHandler = react_native_1.BackHandler.addEventListener('hardwareBackPress', backAction);
        return function () { return backHandler.remove(); };
    }, [opened]);
    var animatedEvent = react_native_1.Animated.event([
        {
            nativeEvent: {
                translationY: translateY,
            },
        },
    ], {
        useNativeDriver: true,
    });
    var onHandlerStateChange = function (event) {
        var _a = event.nativeEvent, translationY = _a.translationY, velocityY = _a.velocityY;
        offset += translationY;
        if (event.nativeEvent.oldState === react_native_gesture_handler_1.State.ACTIVE) {
            if (velocityY > 2000 || translationY > heightItems * 0.4) {
                react_native_1.Animated.timing(translateY, {
                    toValue: windowHeight,
                    duration: 200,
                    useNativeDriver: true,
                }).start(function () {
                    add.setValue(1);
                    setOpened(false);
                });
            }
            else {
                react_native_1.Animated.timing(translateY, {
                    toValue: 0,
                    duration: 200,
                    useNativeDriver: true,
                }).start(function () {
                    translateY.setValue(0);
                });
            }
        }
    };
    var onPress = function (el, index) {
        items.onPress(el, index);
        close();
    };
    return (react_1.default.createElement(react_1.default.Fragment, null, opened && (react_1.default.createElement(ActionSheet_style_1.Container, { testID: "action-sheet" },
        react_1.default.createElement(ActionSheet_style_1.Opacity, { style: {
                opacity: react_native_1.Animated.multiply(translateY.interpolate({
                    inputRange: [0, heightItems * 1.5],
                    outputRange: [0.6, 0.0],
                    extrapolate: 'clamp',
                }), add.interpolate({
                    inputRange: [0, 1],
                    outputRange: [1, 0],
                })),
            } }),
        react_1.default.createElement(react_native_gesture_handler_1.PanGestureHandler, { maxPointers: 1, onGestureEvent: animatedEvent, onHandlerStateChange: onHandlerStateChange },
            react_1.default.createElement(ActionSheet_style_1.PanGesture, { style: {
                    transform: [
                        {
                            translateY: react_native_1.Animated.add(translateY.interpolate({
                                inputRange: [-windowHeight, 0, windowHeight],
                                outputRange: [-20, 0, windowHeight],
                                extrapolate: 'clamp',
                            }), add.interpolate({
                                inputRange: [0, 1],
                                outputRange: [0, heightItems || windowHeight],
                                extrapolate: 'clamp',
                            })),
                        },
                    ],
                } },
                react_1.default.createElement(react_native_1.TouchableWithoutFeedback, { onPress: close },
                    react_1.default.createElement(react_native_1.View, { style: {
                            position: 'absolute',
                            width: '100%',
                            height: '100%',
                        } })),
                react_1.default.createElement(ActionSheet_style_1.ItemsContainer, { onLayout: function (event) {
                        var height = event.nativeEvent.layout.height;
                        setHeightItems(height);
                    } }, items.options &&
                    items.options.map(function (el, index) { return (react_1.default.createElement(ActionSheet_style_1.Item, { key: "action-sheet-item-" + index, testID: "action-sheet-item-" + index, onPress: function () { return onPress(el, index); } },
                        react_1.default.createElement(native_1.Flex, { horizontal: true },
                            el.icon && react_1.default.createElement(ActionSheet_style_1.FontAwesomeIconItem, { icon: el.icon, color: el.iconColor || '#888' }),
                            react_1.default.createElement(ActionSheet_style_1.TextItem, null, el.text)))); }))))))));
};
exports.ActionSheet.show = function (options) {
    ActionSheetObs.set(options);
};
//# sourceMappingURL=ActionSheet.js.map