import { Request, NextFunction, RequestHandler } from 'express';
import { HumandResponse } from '../types/response';
import { FilterableMandatoryCursorOrPagePaginableVC } from '../validationClasses/filterableMandatoryCursorOrPagePaginableVC';
import { validateMandatoryCursorPagination } from './cursorPaginationValidator';
import { validatePageLimitPagination } from './pageLimitPaginationValidator';

export const validateMandatoryCursorOrPagePagination =
    (paginationMaxLimit: number) =>
    (req: Request, res: HumandResponse, next: NextFunction): RequestHandler => {
        const { page } = res.locals.queryParams as FilterableMandatoryCursorOrPagePaginableVC;
        if (!page) {
            return validateMandatoryCursorPagination(paginationMaxLimit)(req, res, next);
        } else {
            return validatePageLimitPagination({ maxLimit: paginationMaxLimit, required: true })(req, res, next)!;
        }
    };
