import type {Page, PageParams} from '@/types/base';

// 默认分页参数
export const DEFAULT_PAGE_SIZE = 10;
export const DEFAULT_CURRENT = 1;

/**
 * 转换前端分页参数为后端分页参数
 */
export const convertToPageParams = (params?: {
    current?: number;
    pageSize?: number;
    sortField?: string;
    sortOrder?: string;
}): { sortOrder: any; sortField: string | undefined; pageSize: number; pageNum: number } => ({
    pageNum: Math.max(1, params?.current || DEFAULT_CURRENT) - 1,  // 转换为从0开始的页码
    pageSize: params?.pageSize || DEFAULT_PAGE_SIZE,
    sortField: params?.sortField,
    sortOrder: params?.sortOrder?.replace('end', '')
});

/**
 * 转换后端分页数据为前端分页数据
 */
export const convertToPageInfo = (page?: Page<any>) => ({
    current: (page?.number || 0) + 1,
    pageSize: page?.size || DEFAULT_PAGE_SIZE,
    total: page?.totalElements || 0
});