export const TYPE_PREVIOUS = 'previous';
export const TYPE_NEXT = 'next';

export const TYPE_PAGE = 'page';

const getSelectedPage = (listingPage, lastPage, pageSelection) => {
    switch (pageSelection) {
        case TYPE_PREVIOUS: {
            if (listingPage === null || listingPage - 1 < 1) {
                return 1;
            }
            return listingPage - 1;
        }
        case TYPE_NEXT: {
            if (listingPage === null || listingPage === undefined) {
                return 2;
            }
            if (listingPage + 1 > lastPage) {
                return lastPage;
            }
            return listingPage + 1;
        }
        default:
            return pageSelection;
    }
};

export const handlePageSelect = (e, pageSelection, listingPage, setListingPage, lastPage) => {
    e.preventDefault();
    const newSelectedPage = getSelectedPage(listingPage, lastPage, pageSelection);
    setListingPage(newSelectedPage);
};
