import React, { useEffect } from 'react';
import { classNames } from '../../types';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faAngleLeft, faAngleRight } from '@fortawesome/free-solid-svg-icons';
import PaginationSelected from './PaginaitonSelected';
import { applyUrlQuery } from '../utils';
import { PHXUseDirectLink } from '../../../hooks/direct-link';
function Pagination({ pagination, currentPage, setCurrentPage, offset, setOffset, tableKey, configUrlApply, }) {
    const directLink = PHXUseDirectLink();
    useEffect(() => {
        if (configUrlApply) {
            const { searchParams } = configUrlApply;
            const pageQuery = applyUrlQuery.get(searchParams, `pagi-${tableKey}`);
            if (pageQuery && pagination.onChange) {
                setCurrentPage(Number(pageQuery));
                setOffset(Number(pageQuery) - 1);
                pagination.onChange(Number(pageQuery));
            }
        }
    }, []);
    const handleClickPage = (page) => {
        var _a;
        setCurrentPage(page);
        setOffset(page - 1);
        if (pagination.onChange) {
            pagination.onChange(page);
        }
        if (configUrlApply) {
            applyUrlQuery.set(((_a = configUrlApply === null || configUrlApply === void 0 ? void 0 : configUrlApply.router) === null || _a === void 0 ? void 0 : _a.push) || directLink, configUrlApply.searchParams, [
                { key: `pagi-${tableKey}`, value: page + '' },
            ]);
        }
    };
    const totalPages = Math.ceil(pagination.dataAggregate / pagination.rowNumberInPage);
    const disableNextPage = !totalPages || currentPage === totalPages;
    const disablePrevPage = currentPage === 1;
    const PreviousPage = () => (React.createElement("a", { "aria-hidden": true, className: classNames('relative inline-flex min-h-[1.75rem] items-center rounded-bl-lg rounded-tl-lg bg-gray-100 px-3 pb-1 pt-0.5 text-gray-900 focus:z-20 focus:outline-offset-0', !disablePrevPage ? 'hover:bg-gray-200' : '', !disablePrevPage
            ? 'active:bg-gray-300 active:pb-[0.2rem] active:pt-[0.2rem] active:shadow-[0rem_0.125rem_0.1rem_0rem_#0004_inset]'
            : '', !disablePrevPage ? 'cursor-pointer' : ''), onClick: () => {
            if (!disablePrevPage) {
                handleClickPage(currentPage - 1);
            }
        } },
        React.createElement("p", null,
            React.createElement("span", { className: 'sr-only' }, "Previous"),
            React.createElement(FontAwesomeIcon, { className: classNames('h-3 w-2.5', disablePrevPage ? 'text-gray-300' : 'text-gray-900'), icon: faAngleLeft }))));
    const NextPage = () => (React.createElement("a", { "aria-hidden": true, className: classNames('relative inline-flex min-h-[1.75rem] items-center rounded-r-md rounded-br-lg rounded-tr-lg bg-gray-100 px-3 pb-1 pt-0.5 text-gray-900 focus:z-20 focus:outline-offset-0', !disableNextPage ? 'hover:bg-gray-200' : '', !disableNextPage
            ? 'active:bg-gray-300 active:pb-[0.03rem] active:pt-[0.01rem] active:shadow-[0rem_0.125rem_0.1rem_0rem_#0004_inset]'
            : '', !disableNextPage ? 'cursor-pointer' : ''), onClick: () => {
            if (!disableNextPage) {
                handleClickPage(currentPage + 1);
            }
        } },
        React.createElement("p", null,
            React.createElement("span", { className: 'sr-only' }, "Next"),
            React.createElement(FontAwesomeIcon, { className: classNames('h-3 w-2.5', disableNextPage ? 'text-gray-300' : 'text-gray-900'), icon: faAngleRight }))));
    const startIndex = pagination.dataAggregate > 0 ? offset * pagination.rowNumberInPage + 1 : pagination.dataAggregate;
    const endIndex = offset * pagination.rowNumberInPage + pagination.rowNumberInPage;
    return (React.createElement("div", { className: 'flex items-center justify-between rounded-lg rounded-tl-none rounded-tr-none border-t border-gray-200 bg-[#F9FAFB] py-2 pl-7 pr-2' },
        React.createElement(PaginationSelected, { paginationChange: pagination === null || pagination === void 0 ? void 0 : pagination.onChange, rowQuantity: pagination.rowNumberInPage, setCurrentPage: setCurrentPage, setRowQuantity: pagination === null || pagination === void 0 ? void 0 : pagination.setRowNumberInPage, setOffset: setOffset, tableKey: tableKey, configUrlApply: configUrlApply }),
        React.createElement("div", { className: 'flex items-center gap-x-3' },
            React.createElement("p", { className: 'hidden text-xs sm:block' },
                "Hi\u1EC3n th\u1ECB ",
                startIndex,
                " - ",
                endIndex >= pagination.dataAggregate ? pagination.dataAggregate : endIndex,
                " tr\u00EAn",
                ' ',
                pagination.dataAggregate,
                " b\u1EA3n ghi"),
            React.createElement("div", { className: 'isolate inline-flex -space-x-px rounded-md bg-white' },
                React.createElement("div", { className: 'mr-1' },
                    React.createElement(PreviousPage, null)),
                React.createElement(NextPage, null)))));
}
export default Pagination;
//# sourceMappingURL=Pagination.js.map