import { React, PropTypes } from '@gravityforms/libraries';
import PaginationModule from '../../Pagination';
import { getModules } from '../utils';

const { forwardRef } = React;

/**
 * @module GridPagination
 * @description The GridPagination component for the DataGrid.
 *
 * @since 4.3.0
 *
 * @param {object}      props                   Component props.
 * @param {boolean}     props.isLoading         Whether the data grid is loading.
 * @param {Array}       props.modules           The modules for the data grid.
 * @param {object}      props.moduleAttributes  The module attributes.
 * @param {object}      props.moduleState       The module state.
 * @param {Function}    props.setIsLoading      Set the loading state of the data grid.
 * @param {Function}    props.updateModuleState Update the module state.
 * @param {boolean}     props.useAjax           Whether to use ajax.
 * @param {object|null} ref                     Ref to the component.
 *
 * @return {JSX.Element} The Pagination component.
 */
const GridPagination = forwardRef( ( {
	isLoading = false,
	modules = [],
	moduleAttributes = {},
	moduleState = {},
	setIsLoading = () => {},
	updateModuleState = () => {},
	useAjax = false,
}, ref ) => {
	const {
		onClick = () => {},
		onPageChange = () => {},
		pageCount = 0,
		...restAttributes
	} = moduleAttributes?.pagination || {};

	if ( ! pageCount ) {
		return null;
	}

	const paginationProps = {
		...restAttributes,
	};

	if ( useAjax ) {
		if ( onClick ) {
			paginationProps.onPageChange = onPageChange;
			paginationProps.onClick = ( clickData ) => {
				// Next selected page must be defined.
				if ( ! Number.isInteger( clickData.nextSelectedPage ) || isLoading ) {
					return false;
				}

				const { BulkActions } = getModules( modules, [ 'BulkActions' ] );
				const updatedState = {
					currentPage: clickData.nextSelectedPage,
					...( ( BulkActions && BulkActions?.getDefaultState() ) || {} ),
				};
				const combinedState = { ...moduleState, ...updatedState };
				setIsLoading( true );
				onClick( combinedState )
					.then( () => {
						setIsLoading( false );
						updateModuleState( updatedState );
					} )
					.catch( ( error ) => {
						// handle error.
						setIsLoading( false );
						console.info( error );
					} );

				return false;
			};
			paginationProps.forcePage = moduleState?.currentPage;
			if ( Number.isInteger( pageCount ) ) {
				paginationProps.pageCount = pageCount;
			}
		}
	} else {
		paginationProps.onPageChange = ( data ) => {
			updateModuleState( {
				currentPage: data.selected,
			} );
			onPageChange( data );
		};
		paginationProps.pageCount = pageCount;
	}

	return (
		<div className="gform-data-grid__pagination" data-testid="data-grid-pagination" ref={ ref }>
			<PaginationModule paginationProps={ paginationProps } />
		</div>
	);
} );

GridPagination.propTypes = {
	isLoading: PropTypes.bool,
	modules: PropTypes.array,
	moduleAttributes: PropTypes.object,
	moduleState: PropTypes.object,
	setIsLoading: PropTypes.func,
	updateModuleState: PropTypes.func,
	useAjax: PropTypes.bool,
};

GridPagination.displayName = 'GridPagination';

/**
 * @function getDefaultState
 * @description Get the default state for the GridPagination module.
 *
 * @since 4.3.0
 *
 * @return {object} The default state.
 */
const getDefaultState = () => ( {
	currentPage: 0,
} );

export default {
	name: 'GridPagination',
	GridPagination,
	getDefaultState,
};
