"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;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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 = exports.PageListingContainer = void 0;
var react_1 = __importStar(require("react"));
var useEventListener_1 = require("@devesharp/react-hooks/lib/useEventListener");
var CardListingContainer_1 = require("./CardListingContainer");
var FiltersContainer_1 = require("./FiltersContainer");
var PageListingContext_1 = require("./PageListingContext");
var PageListingContainer = function PageListing(props) {
    var _a;
    var viewList = props.viewList, _b = props.data, data = _b === void 0 ? {} : _b, children = props.children;
    var _c = (0, react_1.useState)(false), filterMobileOpen = _c[0], setFilterMobileOpen = _c[1];
    var _d = (0, react_1.useState)(viewList.filters), filtersDebounce = _d[0], setFiltersDebounce = _d[1];
    var listingElRef = (0, react_1.useRef)();
    (0, react_1.useEffect)(function () {
        return viewList.registerOnSearch(function () {
            if (listingElRef.current) {
                listingElRef.current.scrollTop = 0;
            }
        });
    }, []);
    var updateFilters = (0, react_1.useCallback)(function updateFilters(d) {
        viewList.setFilters(function () { return d; });
    }, []);
    var clearFilters = (0, react_1.useCallback)(function clearFilters() {
        if (window.location.hash === '#filter') {
            if (viewList.history) {
                viewList.history.replace({
                    search: window.location.search,
                    hash: '',
                });
            }
            else {
                window.location.replace('');
            }
        }
        viewList.setFilters(function () { return ({}); });
        setFilterMobileOpen(false);
    }, []);
    var setPage = (0, react_1.useCallback)(function (page) {
        viewList.setPage(page);
    }, []);
    var updateFiltersDebounce = (0, react_1.useCallback)(function updateFiltersDbebounce(d) {
        setFiltersDebounce(function () { return d; });
    }, []);
    var applyFiltersDebounce = (0, react_1.useCallback)(function applyFilters() {
        if (window.location.hash === '#filter') {
            if (viewList.history) {
                viewList.history.replace({
                    search: window.location.search,
                    hash: '',
                });
            }
            else {
                window.location.replace('');
            }
        }
        viewList.setFilters(function () { return filtersDebounce; }, { replaceUrl: true });
        setFilterMobileOpen(false);
    }, [filtersDebounce]);
    var openFilters = (0, 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 = (0, react_1.useCallback)(function () {
        setFilterMobileOpen(false);
        if (window.location.hash === '#filter') {
            if (viewList.history) {
                viewList.history.goBack();
            }
            else {
                window.history.back();
            }
        }
    }, []);
    (0, react_1.useEffect)(function () {
        if (window.location.hash === '#filter') {
            if (viewList.history) {
                viewList.history.replace({
                    search: window.location.search,
                    hash: '',
                });
            }
            else {
                window.location.replace('');
            }
        }
    }, []);
    (0, useEventListener_1.useEventListener)('hashchange', function hashchange() {
        if (window.location.hash === '#filter') {
            setFilterMobileOpen(true);
        }
        else {
            setFilterMobileOpen(false);
        }
    });
    var currentPage = ((_a = viewList.filters.offset) !== null && _a !== void 0 ? _a : 0) / viewList.limit + 1;
    var pageCountStart = (currentPage - 1) * viewList.limit + 1;
    pageCountStart = viewList.resourcesTotal ? pageCountStart : 0;
    var pageCountEnd = (currentPage - 1) * viewList.limit + viewList.limit;
    pageCountEnd = pageCountEnd > viewList.resourcesTotal ? viewList.resourcesTotal : pageCountEnd;
    var showCardsSkeleton = viewList.searching || viewList.loading;
    var showCards = viewList.started && !viewList.searching;
    return (react_1.default.createElement(PageListingContext_1.PageListingContext.Provider, { value: __assign(__assign({}, viewList), { listingElRef: listingElRef, filterMobileOpen: filterMobileOpen, updateFilters: updateFilters, clearFilters: clearFilters, currentPage: currentPage, setPage: setPage, updateFiltersDebounce: updateFiltersDebounce, applyFiltersDebounce: applyFiltersDebounce, openFilters: openFilters, closeFilters: closeFilters, pageCountStart: pageCountStart, pageCountEnd: pageCountEnd, showCardsSkeleton: showCardsSkeleton, showCards: showCards, data: data }) }, children));
};
exports.PageListingContainer = PageListingContainer;
exports.PageListing = {
    Container: exports.PageListingContainer,
    ScrollContainer: function ScrollContainer(props) {
        var listingElRef = (0, react_1.useContext)(PageListingContext_1.PageListingContext).listingElRef;
        return react_1.default.createElement("div", { ref: listingElRef }, props.children);
    },
    Context: function ScrollContainer(props) {
        var context = (0, react_1.useContext)(PageListingContext_1.PageListingContext);
        return props.render(context);
    },
    FiltersContainer: FiltersContainer_1.FiltersContainer,
    CardsContainer: CardListingContainer_1.CardListingContainer,
    TextResultCount: function ScrollContainer() {
        var context = (0, react_1.useContext)(PageListingContext_1.PageListingContext);
        return (react_1.default.createElement(react_1.default.Fragment, null,
            context.pageCountStart,
            "-",
            context.pageCountEnd,
            " de ",
            context.resourcesTotal));
    },
};
//# sourceMappingURL=PageListing.js.map