"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.CardsListingContainer = void 0;
var react_1 = __importStar(require("react"));
var react_native_1 = require("react-native");
var native_1 = require("../../../LoaderIcon/native");
var native_2 = require("../../../PageListingError/native");
var native_3 = require("../../../PageListingErrorLoadMore/native");
var native_4 = require("../../../PageListingNotFound/native");
var RFValue_1 = require("../../../RFValue");
function CardsListingContainer(props) {
    var renderResourceSkeleton = props.renderResourceSkeleton, viewList = props.viewList, paddingScroll = props.paddingScroll, navActions = props.navActions, navActionsSize = props.navActionsSize;
    var skeletonResources = viewList.skeletonResources, setPage = viewList.setPage, starting = viewList.starting, searching = viewList.searching, resources = viewList.resources, reloadPage = viewList.reloadPage, errorLoadData = viewList.errorLoadData;
    var contentContainerStyle = react_1.useMemo(function () { return ({
        paddingTop: (navActions === null || navActions === void 0 ? void 0 : navActions.selected) ? navActionsSize : 2 * paddingScroll,
        paddingBottom: (navActions === null || navActions === void 0 ? void 0 : navActions.selected) ? 2 * paddingScroll + navActionsSize : paddingScroll + navActionsSize,
    }); }, [paddingScroll, navActions === null || navActions === void 0 ? void 0 : navActions.selected]);
    var showCardsSkeleton = starting || searching;
    var showPageError = errorLoadData;
    var showCards = !starting && !searching && resources && !showPageError;
    var keyExtractorSkeleton = react_1.useCallback(function (item, index) { return "card-skeleton-" + index; }, []);
    var renderItemFlatListSkeleton = react_1.useCallback(function (item) { return react_1.default.createElement(react_native_1.View, { testID: "skeleton-card" }, renderResourceSkeleton(item.item, item.index)); }, [renderResourceSkeleton]);
    return (react_1.default.createElement(react_native_1.View, { style: { flex: 1 } },
        showPageError && (react_1.default.createElement(react_native_1.View, { style: {
                flex: 1,
                justifyContent: 'center',
                alignItems: 'center',
                flexGrow: 1,
                paddingTop: paddingScroll,
            }, testID: "error-page-listing" },
            react_1.default.createElement(react_native_1.View, { style: { flexDirection: 'row' } },
                react_1.default.createElement(react_native_1.ScrollView, null,
                    react_1.default.createElement(native_2.PageErrorLoad, { retry: reloadPage }))))),
        showCards && resources.length === 0 && (react_1.default.createElement(react_native_1.View, { style: {
                flex: 1,
                justifyContent: 'center',
                alignItems: 'center',
                flexGrow: 1,
                paddingTop: paddingScroll,
            }, testID: "not-found-page" },
            react_1.default.createElement(react_native_1.View, { style: { flexDirection: 'row' } },
                react_1.default.createElement(react_native_1.ScrollView, null,
                    react_1.default.createElement(native_4.PageListingNotFound, null))))),
        react_1.default.createElement(CardListing, __assign({}, props)),
        showCardsSkeleton && (react_1.default.createElement(react_native_1.Animated.FlatList, { contentContainerStyle: contentContainerStyle, renderItem: renderItemFlatListSkeleton, keyExtractor: keyExtractorSkeleton, data: skeletonResources, scrollEnabled: false }))));
}
exports.CardsListingContainer = CardsListingContainer;
function CardListing(props) {
    var renderResource = props.renderResource, renderResourceSkeleton = props.renderResourceSkeleton, viewList = props.viewList, animateScrollY = props.animateScrollY, paddingScroll = props.paddingScroll, scrollListingRef = props.scrollListingRef, scrollPosRef = props.scrollPosRef, scrollCardHeightRef = props.scrollCardHeightRef, animatedNavActions = props.animatedNavActions, navActions = props.navActions, navActionsSize = props.navActionsSize;
    var resourcesTotal = viewList.resourcesTotal, registerOnSearch = viewList.registerOnSearch, skeletonResources = viewList.skeletonResources, setPage = viewList.setPage, filters = viewList.filters, starting = viewList.starting, searching = viewList.searching, resources = viewList.resources, reloadPage = viewList.reloadPage, errorLoadData = viewList.errorLoadData, errorLoadMore = viewList.errorLoadMore, limit = viewList.limit, searchingMore = viewList.searchingMore, lastPage = viewList.lastPage, loadNextPage = viewList.loadNextPage;
    var onEndReachedEnabled = react_1.useRef(false);
    onEndReachedEnabled.current = !lastPage && !starting && !errorLoadMore && !errorLoadMore && !!resources.length;
    var contentContainerStyle = react_1.useMemo(function () { return ({
        paddingTop: (navActions === null || navActions === void 0 ? void 0 : navActions.selected) ? navActionsSize : 2 * paddingScroll,
        paddingBottom: (navActions === null || navActions === void 0 ? void 0 : navActions.selected) ? 2 * paddingScroll + navActionsSize : paddingScroll + navActionsSize,
    }); }, [paddingScroll, navActions === null || navActions === void 0 ? void 0 : navActions.selected]);
    var keyExtractor = react_1.useCallback(function (item, index) { return "card-" + item.id; }, []);
    var renderItemFlatList = react_1.useCallback(function (item) { return react_1.default.createElement(react_native_1.View, { testID: "card-" + item.index }, renderResource(item.item, item.index)); }, [renderResource]);
    var onEndReached = react_1.useCallback(function () {
        if (onEndReachedEnabled.current) {
            loadNextPage();
        }
    }, []);
    var showPageError = errorLoadData;
    var showCards = !starting && !searching && resources && !showPageError;
    return (react_1.default.createElement(react_1.default.Fragment, null, showCards && resources.length !== 0 && (react_1.default.createElement(react_native_1.Animated.FlatList, { testID: "card-listing", ref: scrollListingRef, contentContainerStyle: contentContainerStyle, onContentSizeChange: function (width, height) {
            scrollCardHeightRef.current = height;
        }, ListFooterComponent: react_1.default.createElement(ListFooterComponent, { endPage: lastPage, searchingMoreItems: searchingMore, errorLoadMore: errorLoadMore, retryMoreItems: loadNextPage }), showsVerticalScrollIndicator: false, onScroll: react_native_1.Animated.event([{ nativeEvent: { contentOffset: { y: animateScrollY } } }], {
            useNativeDriver: true,
            listener: function (event) {
                scrollPosRef.current = event.nativeEvent.contentOffset.y;
            },
        }), onEndReachedThreshold: 0.4, onEndReached: onEndReached, renderItem: renderItemFlatList, keyExtractor: keyExtractor, data: resources }))));
}
var ListFooterComponent = react_1.default.memo(function ListFooterComponent(_a) {
    var endPage = _a.endPage, searchingMoreItems = _a.searchingMoreItems, errorLoadMore = _a.errorLoadMore, retryMoreItems = _a.retryMoreItems;
    return (react_1.default.createElement(react_1.default.Fragment, null,
        !endPage && !errorLoadMore && (react_1.default.createElement(react_native_1.View, { style: {
                height: RFValue_1.RSValue(150),
                flexDirection: 'column',
                alignItems: 'center',
                alignContent: 'center',
                justifyContent: 'center',
            } },
            react_1.default.createElement(native_1.LoaderIcon, { size: 48, color: "#222" }))),
        errorLoadMore && (react_1.default.createElement(react_native_1.View, { style: {
                height: RFValue_1.RSValue(150),
                flexDirection: 'column',
                alignItems: 'center',
                alignContent: 'center',
            } },
            react_1.default.createElement(native_3.PageListingErrorLoadMore, { retry: retryMoreItems })))));
});
//# sourceMappingURL=CardsListingContainer.js.map