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 __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { useEffect } from 'react';
import { useStore, Provider, useSelector, } from 'react-redux';
import PropTypes from 'prop-types';
import ApiUtils from '../../Utils/ApiUtils';
import LibraryUtils from '../../Utils/LibraryUtils';
import configureListingStore from './configureListingStore';
import ListingActions from './ListingActions';
import ListingHeaderLabel from './ListingHeaderLabel';
import PaginationActions from './PaginationActions';
import ListingSelectors from './ListingSelectors';
import PaginationControls from './PaginationControls';
import LoadingRingSvg from '../Icons/LoadingRingSvg/LoadingRingSvg';
import PaginationSelectors from './PaginationSelectors';
var LoadingIndicator = function () {
    return React.createElement(LoadingRingSvg, null);
};
var LoadingError = function (_a) {
    var message = _a.message;
    return React.createElement("div", null, message);
};
LoadingError.propTypes = {
    message: PropTypes.any,
};
export var LoadingStatusIndicator = function (_a) {
    var isLoading = _a.isLoading, loadingError = _a.loadingError;
    if (loadingError) {
        return React.createElement(LoadingError, { message: loadingError });
    }
    if (isLoading) {
        return React.createElement(LoadingIndicator, null);
    }
    return null;
};
LoadingStatusIndicator.propTypes = {
    isLoading: PropTypes.bool.isRequired,
    loadingError: PropTypes.any,
};
var Listing = function (_a) {
    var className = _a.className, columnHeadings = _a.columnHeadings, endOfItemsMessage = _a.endOfItemsMessage, isLoading = _a.isLoading, ItemComponent = _a.ItemComponent, itemProps = _a.itemProps, items = _a.items, loadingError = _a.loadingError, onLoadMore = _a.onLoadMore, onSortChange = _a.onSortChange;
    return React.createElement("div", { className: [
            className,
            'listing',
        ]
            .filter(Boolean)
            .join(' ') },
        React.createElement("table", null,
            React.createElement("thead", { className: "listing__header" },
                React.createElement("tr", null, columnHeadings.map(function (heading, index) {
                    return React.createElement("th", { key: "column-heading_" + heading.key, role: "columnheader", className: "listing__column-heading listing__column-heading--" + heading.key, "aria-colindex": index },
                        React.createElement(ListingHeaderLabel, { label: heading.label, sortByType: heading.sortByType, onSortChange: onSortChange }));
                }))),
            React.createElement("tbody", { className: "listing__items" }, items.map(function (item, index) {
                return React.createElement(ItemComponent, __assign({ key: "listing-item_" + item.id }, itemProps, item));
            }))),
        React.createElement(PaginationControls, { endOfItemsMessage: endOfItemsMessage, onLoadMore: onLoadMore, isLoading: isLoading }),
        React.createElement(LoadingStatusIndicator, { isLoading: isLoading, loadingError: loadingError }));
};
Listing.propTypes = {
    className: PropTypes.string,
    columnHeadings: PropTypes.arrayOf(PropTypes.shape({
        key: PropTypes.string,
        label: PropTypes.node,
        // If specified, this label will be rendered as a sort control
        sortByType: PropTypes.string,
    })),
    endOfItemsMessage: PropTypes.string,
    isLoading: PropTypes.bool.isRequired,
    ItemComponent: PropTypes.any,
    itemProps: PropTypes.object,
    items: PropTypes.array.isRequired,
    loadingError: PropTypes.any,
    onLoadMore: PropTypes.func.isRequired,
    onSortChange: PropTypes.func.isRequired,
};
Listing.defaultProps = {
    endOfItemsMessage: 'There are no more items.',
};
var withListingStore = function (dependencies) { return function (WrappedComponent) {
    var store = configureListingStore(dependencies);
    var WithListingStore = function (props) {
        return React.createElement(Provider, { store: store },
            React.createElement(WrappedComponent, __assign({}, props)));
    };
    WithListingStore.displayName = "WithListingStore(" + LibraryUtils.getDisplayName(WrappedComponent) + ")";
    return WithListingStore;
}; };
var withListing = function (dependencies) { return function (WrappedComponent) {
    var WithListing = function (_a) {
        var itemProps = _a.itemProps, baseQueries = _a.baseQueries, props = __rest(_a, ["itemProps", "baseQueries"]);
        var store = useStore();
        var handleFilterMapChange = function (filterMap) {
            return store.dispatch(PaginationActions.setFilterMap(filterMap));
        };
        var handleSortChange = function (sort) {
            return store.dispatch(PaginationActions.setSort(sort));
        };
        var handleLoadMore = function () {
            return store.dispatch(ListingActions.loadMore());
        };
        var handleReload = function () {
            return store.dispatch(ListingActions.reload());
        };
        useEffect(function () {
            store.dispatch(ListingActions.setBaseQueries(baseQueries));
        }, [
            baseQueries,
        ]);
        // onMount
        // !!! mount should always be the last effect !!!
        useEffect(function () {
            store.dispatch(ListingActions.mount());
        }, [
            store,
        ]);
        return (React.createElement(WrappedComponent, __assign({ isLoading: useSelector(ListingSelectors.getIsLoading), itemProps: itemProps, items: useSelector(ListingSelectors.getVisibleItems), filterMap: useSelector(PaginationSelectors.getFilterMap), onFilterMapChange: handleFilterMapChange, sortBy: useSelector(PaginationSelectors.getSortBy), sortOrder: useSelector(PaginationSelectors.getSortOrder), onSortChange: handleSortChange, onLoadMore: handleLoadMore, reload: handleReload }, props)));
    };
    WithListing.propTypes = {
        itemProps: PropTypes.object,
        baseQueries: PropTypes.arrayOf(PropTypes.shape({
            key: PropTypes.string.isRequired,
            value: PropTypes.any,
        })),
    };
    WithListing.defaultProps = {
        baseQueries: [],
    };
    WithListing.displayName = "WithListing(" + LibraryUtils.getDisplayName(WrappedComponent) + ")";
    return withListingStore(dependencies)(WithListing);
}; };
export var createListing = function (_a) {
    var requestItems = _a.requestItems, _b = _a.fetchThrottleDelay, fetchThrottleDelay = _b === void 0 ? 1000 : _b, _c = _a.ListingComponent, ListingComponent = _c === void 0 ? Listing : _c, _d = _a.getDataFromJson, getDataFromJson = _d === void 0 ? ApiUtils.getDataFromJson : _d, _e = _a.getPaginationFromJson, getPaginationFromJson = _e === void 0 ? ApiUtils.getPaginationFromJson : _e;
    return withListing({
        requestItems: requestItems,
        fetchThrottleDelay: fetchThrottleDelay,
        getDataFromJson: getDataFromJson,
        getPaginationFromJson: getPaginationFromJson,
    })(ListingComponent);
};
export default createListing;
//# sourceMappingURL=Listing.js.map