"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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.PageListing = void 0;
var react_1 = __importStar(require("react"));
var react_native_1 = require("react-native");
var native_1 = require("../../NavActions/native");
var RFValue_1 = require("../../RFValue");
var CardsListingContainer_1 = require("./CardsListingContainer");
var FiltersContainer_1 = require("./FiltersContainer");
var HeaderListingContainer_1 = require("./HeaderListingContainer");
var PageListing_style_1 = require("./PageListing.style");
var SubHeaderListingContainer_1 = require("./SubHeaderListingContainer");
exports.PageListing = function (props) {
    var FiltersComponent = props.FiltersComponent, navActions = props.navActions, viewList = props.viewList;
    var registerOnSearch = viewList.registerOnSearch, starting = viewList.starting, filters = viewList.filters;
    var tabsSize = RFValue_1.RSValue(50);
    var navigationBarSize = RFValue_1.RSValue(55);
    var resultsSize = RFValue_1.RSValue(45);
    var _a = react_1.useState(RFValue_1.RSValue(0)), tagsFilterSize = _a[0], setTagsFilterSize = _a[1];
    var paddingScroll = tabsSize + navigationBarSize + resultsSize + tagsFilterSize;
    var navActionsSize = RFValue_1.RSValue(65);
    var animateScrollY = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var animatedNavActions = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var animatedNavActions2 = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var scrollTop = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var animateHeaderTranslateY = react_1.useRef(react_native_1.Animated.add(react_native_1.Animated.multiply(react_native_1.Animated.diffClamp(animateScrollY.interpolate({
        inputRange: [0, 1],
        outputRange: [0, 1],
        extrapolateLeft: 'clamp',
    }), 0, navigationBarSize + tabsSize), -1), scrollTop)).current;
    react_1.useEffect(function () {
        registerOnSearch(function () {
            if (navActions) {
                navActions === null || navActions === void 0 ? void 0 : navActions.onUnselectAll();
            }
        });
    }, []);
    var animateTop = react_1.useRef(new react_native_1.Animated.Value(0)).current;
    var navactionEnabled = react_1.useRef(false);
    var navactionEnabled2 = react_1.useRef(false);
    react_1.useLayoutEffect(function () {
        if (navActions === null || navActions === void 0 ? void 0 : navActions.selected) {
            if (!navactionEnabled2.current) {
                navactionEnabled2.current = true;
                animatedNavActions.setValue(1);
                react_native_1.Animated.timing(animatedNavActions2, {
                    toValue: 1,
                    duration: 200,
                    useNativeDriver: true,
                }).start();
                if (scrollPosRef.current < paddingScroll) {
                    animateTop.setValue(paddingScroll - scrollPosRef.current - navActionsSize);
                    react_native_1.Animated.timing(animateTop, {
                        toValue: 0,
                        duration: 200,
                        useNativeDriver: true,
                    }).start();
                }
            }
        }
        else if (navactionEnabled2.current) {
            navactionEnabled2.current = false;
            var newScrollY = scrollPosRef.current + paddingScroll - navActionsSize;
            animatedNavActions.setValue(0);
            react_native_1.Animated.timing(animatedNavActions2, {
                toValue: 0,
                duration: 200,
                useNativeDriver: true,
            }).start();
        }
    }, [navActions === null || navActions === void 0 ? void 0 : navActions.selected]);
    var forceScroll = react_1.useRef(-1);
    react_1.useLayoutEffect(function () {
        var _a, _b, _c;
        forceScroll.current = -1;
        if (navActions === null || navActions === void 0 ? void 0 : navActions.selected) {
            var newScrollY = scrollPosRef.current - paddingScroll + navActionsSize;
            if (!navactionEnabled.current) {
                navactionEnabled.current = true;
                (_a = scrollListingRef === null || scrollListingRef === void 0 ? void 0 : scrollListingRef.current) === null || _a === void 0 ? void 0 : _a.scrollToOffset({
                    animated: false,
                    offset: newScrollY,
                });
                forceScroll.current = newScrollY;
            }
        }
        else if (navactionEnabled.current) {
            navactionEnabled.current = false;
            var newScrollY = scrollPosRef.current + paddingScroll - navActionsSize;
            if (scrollPosRef.current >= scrollCardHeightRef.current - ListingHeightRef.current - paddingScroll) {
                var MaxScroll = scrollCardHeightRef.current - ListingHeightRef.current;
                var rest = scrollCardHeightRef.current - ListingHeightRef.current - scrollPosRef.current;
                (_b = scrollListingRef === null || scrollListingRef === void 0 ? void 0 : scrollListingRef.current) === null || _b === void 0 ? void 0 : _b.scrollToOffset({
                    animated: false,
                    offset: MaxScroll + paddingScroll - navActionsSize - rest,
                });
                forceScroll.current = MaxScroll + paddingScroll - navActionsSize - rest;
            }
            else {
                (_c = scrollListingRef === null || scrollListingRef === void 0 ? void 0 : scrollListingRef.current) === null || _c === void 0 ? void 0 : _c.scrollToOffset({
                    animated: false,
                    offset: newScrollY,
                });
                forceScroll.current = newScrollY;
            }
        }
    }, [paddingScroll, navActions === null || navActions === void 0 ? void 0 : navActions.selected]);
    react_1.useEffect(function () {
        var _a;
        if (forceScroll.current != -1) {
            (_a = scrollListingRef === null || scrollListingRef === void 0 ? void 0 : scrollListingRef.current) === null || _a === void 0 ? void 0 : _a.scrollToOffset({
                animated: false,
                offset: forceScroll.current,
            });
        }
    }, [paddingScroll, navActions === null || navActions === void 0 ? void 0 : navActions.selected]);
    react_1.useLayoutEffect(function () {
        if (!(filters === null || filters === void 0 ? void 0 : filters.offset) || (filters === null || filters === void 0 ? void 0 : filters.offset) == 0) {
            react_native_1.Animated.timing(animateScrollY, {
                toValue: -200,
                duration: 200,
                useNativeDriver: true,
            }).start();
        }
    }, [filters]);
    var tabsRef = react_1.useRef();
    var formSortRef = react_1.useRef();
    var tagsFiltersRef = react_1.useRef();
    var scrollListingRef = react_1.useRef();
    var scrollPosRef = react_1.useRef(0);
    var scrollCardHeightRef = react_1.useRef(0);
    var ListingHeightRef = react_1.useRef(0);
    var _b = react_1.useState(false), filterMobileOpen = _b[0], setFilterMobileOpen = _b[1];
    var openFilters = react_1.useCallback(function () {
        setFilterMobileOpen(true);
    }, []);
    var closeFilters = react_1.useCallback(function () {
        setFilterMobileOpen(false);
    }, []);
    var propsChildren = __assign(__assign({}, props), { tabsRef: tabsRef,
        scrollListingRef: scrollListingRef,
        formSortRef: formSortRef,
        tagsFiltersRef: tagsFiltersRef,
        filterMobileOpen: filterMobileOpen,
        setFilterMobileOpen: setFilterMobileOpen,
        openFilters: openFilters,
        closeFilters: closeFilters,
        animateScrollY: animateScrollY,
        paddingScroll: paddingScroll,
        navActionsSize: navActionsSize,
        setTagsFilterSize: setTagsFilterSize,
        scrollPosRef: scrollPosRef,
        scrollCardHeightRef: scrollCardHeightRef,
        animatedNavActions: animatedNavActions });
    var _c = react_1.useState(0), pageListingHeight = _c[0], setPageListingHeight = _c[1];
    return (react_1.default.createElement(react_native_1.View, { style: { flex: 1 }, onLayout: function (event) {
            setPageListingHeight(event.nativeEvent.layout.height);
        } },
        react_1.default.createElement(FiltersContainer_1.FiltersContainer, __assign({}, propsChildren)),
        navActions && !!(navActions === null || navActions === void 0 ? void 0 : navActions.selected) && (react_1.default.createElement(react_native_1.Animated.View, { testID: "nav-actions-container", style: {
                position: 'absolute',
                top: 0,
                left: 0,
                width: '100%',
                zIndex: 10,
                transform: [
                    {
                        translateY: animatedNavActions2.interpolate({
                            inputRange: [0, 1],
                            outputRange: [-navActionsSize, 0],
                        }),
                    },
                ],
            } },
            react_1.default.createElement(native_1.NavActions, __assign({}, navActions)))),
        react_1.default.createElement(react_native_1.Animated.View, { onLayout: function (event) {
                ListingHeightRef.current = event.nativeEvent.layout.height;
            }, style: {
                backgroundColor: 'white',
                height: pageListingHeight + 2 * paddingScroll + navActionsSize,
                transform: [
                    {
                        translateY: react_native_1.Animated.add(animateTop, !(navActions === null || navActions === void 0 ? void 0 : navActions.selected) ? -paddingScroll : 0),
                    },
                ],
            } },
            react_1.default.createElement(PageListing_style_1.HeaderSection, { style: {
                    transform: [
                        {
                            translateY: react_native_1.Animated.add(react_native_1.Animated.add(animateHeaderTranslateY, paddingScroll), animatedNavActions.interpolate({
                                inputRange: [0, 1],
                                outputRange: [0, -paddingScroll * 2 + navActionsSize - navActionsSize / 10],
                            })),
                        },
                    ],
                } },
                react_1.default.createElement(HeaderListingContainer_1.HeaderListingContainer, __assign({}, propsChildren)),
                react_1.default.createElement(SubHeaderListingContainer_1.SubHeaderListingContainer, __assign({}, propsChildren))),
            react_1.default.createElement(react_native_1.View, { style: { flex: 1 } },
                react_1.default.createElement(CardsListingContainer_1.CardsListingContainer, __assign({}, propsChildren))))));
};
//# sourceMappingURL=PageListing.js.map