"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.prototype.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_hooks_1 = require("@devesharp/react-hooks");
var use_immer_1 = require("use-immer");
var CardListingContainer_1 = require("./CardListingContainer");
var FiltersContainer_1 = require("./FiltersContainer");
var HeaderContainer_1 = require("./HeaderContainer");
var S = __importStar(require("./PageListing.style"));
var PageListingContext_1 = require("./PageListingContext");
var SubHeaderContainer_1 = require("./SubHeaderContainer");
var PageListing = function (props) {
    var _a, _b;
    var viewList = props.viewList, title = props.title, tabs = props.tabs, sortOptions = props.sortOptions, tagsFilters = props.tagsFilters, renderResource = props.renderResource, renderResourceSkeleton = props.renderResourceSkeleton, _c = props.limit, limit = _c === void 0 ? 20 : _c;
    var listingElRef = react_1.useRef();
    react_1.useEffect(function () {
        return viewList.registerOnSearch(function () {
            listingElRef.current.scrollTop = 0;
        });
    }, []);
    var handleFilters = react_1.useCallback(function handleFilters(data) {
        viewList.setFilters(function () { return data; });
    }, []);
    var clearFilters = react_1.useCallback(function clearFilters() {
        viewList.setFilters(function () { return ({}); });
    }, []);
    var changePage = react_1.useCallback(function (page) {
        viewList.setPage(page);
    }, []);
    var _d = react_1.useState(false), filterMobileOpen = _d[0], setFilterMobileOpen = _d[1];
    var openFilters = react_1.useCallback(function () {
        setFilterMobileOpen(true);
        if (viewList.history) {
            viewList.history.push({
                search: window.location.search,
                hash: '#filter',
            });
        }
        else {
            window.location.hash = 'filter';
        }
    }, []);
    var closeFilters = react_1.useCallback(function () {
        setFilterMobileOpen(false);
        if (window.location.hash === '#filter') {
            if (viewList.history) {
                viewList.history.back();
            }
            else {
                window.history.back();
            }
        }
    }, []);
    react_1.useEffect(function () {
        if (window.location.hash === '#filter') {
            window.location.replace('');
        }
    }, []);
    react_hooks_1.useEventListener('hashchange', function hashchange() {
        if (window.location.hash === '#filter') {
            setFilterMobileOpen(true);
        }
        else {
            setFilterMobileOpen(false);
        }
    });
    var _e = use_immer_1.useImmer(viewList.filters), filtersTemp = _e[0], setFiltersTemp = _e[1];
    var handleFiltersMobile = react_1.useCallback(function handleFiltersMobile(data) {
        setFiltersTemp(function () { return data; });
    }, []);
    var clearFiltersMobile = react_1.useCallback(function clearFiltersMobile() {
        closeFilters();
        viewList.setFilters(function () { return ({}); });
    }, []);
    var applyFilters = react_1.useCallback(function applyFilters() {
        viewList.setFilters(function () { return filtersTemp; }, { replaceUrl: true });
        setFilterMobileOpen(false);
    }, [filtersTemp]);
    var page = (_a = viewList.filters.page) !== null && _a !== void 0 ? _a : 1;
    var pageCountStart = (page - 1) * 20 + 1;
    pageCountStart = viewList.resourcesTotal ? pageCountStart : 0;
    var pageCountEnd = (page - 1) * 20 + 20;
    pageCountEnd = pageCountEnd > viewList.resourcesTotal ? viewList.resourcesTotal : pageCountEnd;
    var currentPage = ((_b = viewList.filters.offset) !== null && _b !== void 0 ? _b : 0) / limit + 1;
    var showCardsSkeleton = viewList.searching || viewList.loading;
    var showCards = viewList.started && !viewList.searching;
    var formSortRef = react_1.useRef();
    var tagsFiltersRef = react_1.useRef();
    var providerValue = __assign(__assign({}, viewList), { title: title,
        tabs: tabs,
        sortOptions: sortOptions,
        handleFilters: handleFilters,
        clearFilters: clearFilters,
        pageCountStart: pageCountStart,
        pageCountEnd: pageCountEnd,
        currentPage: currentPage,
        applyFilters: applyFilters,
        changePage: changePage,
        formSortRef: formSortRef,
        showCardsSkeleton: showCardsSkeleton,
        showCards: showCards,
        tagsFilters: tagsFilters,
        tagsFiltersRef: tagsFiltersRef,
        renderResource: renderResource,
        openFilters: openFilters,
        closeFilters: closeFilters,
        handleFiltersMobile: handleFiltersMobile,
        clearFiltersMobile: clearFiltersMobile,
        filterMobileOpen: filterMobileOpen,
        renderResourceSkeleton: renderResourceSkeleton, FiltersComponent: props.FiltersComponent });
    return (react_1.default.createElement(PageListingContext_1.PageListingContext.Provider, { value: providerValue },
        react_1.default.createElement(S.Container, null,
            react_1.default.createElement("div", { className: "col-filters" },
                react_1.default.createElement(FiltersContainer_1.FiltersContainer, null)),
            react_1.default.createElement("div", { className: "col-listing", ref: listingElRef },
                react_1.default.createElement(HeaderContainer_1.HeaderContainer, null),
                react_1.default.createElement(SubHeaderContainer_1.SubHeaderContainer, null),
                react_1.default.createElement(CardListingContainer_1.CardListingContainer, null)))));
};
exports.PageListing = PageListing;
//# sourceMappingURL=PageListing.backup.js.map