import type { SortOrder } from 'antd/lib/table/interface';

export interface PageQuery {
	pageIndex?: number;
	pageSize?: number;
	orderBy?: string;
	orderDirection?: string;
}

export function formatParam<T extends PageQuery>(
	params: T & {
		pageSize?: number;
		current?: number;
		keyword?: string;
	},
	sorter: Record<string, SortOrder>,
	filter: Record<string, (string | number)[] | null>,
): T & {
	pageSize?: number;
	current?: number;
	keyword?: string;
} {
	params.pageIndex = params.current;

	// 获取 sorter 的key 和value
	if (sorter) {
		const sorterKey = Object.keys(sorter)[0];
		const sorterValue = sorter[sorterKey];

		if (sorterKey) {
			params.orderBy = sorterKey;
		}

		if (sorterValue) {
			params.orderDirection = sorterValue === 'ascend' ? 'asc' : 'desc';
		}
	}

	if (filter) {
		// 循环filter
		for (const key in filter) {
			if (filter[key]) {
				// @ts-ignore
				params[key] = filter[key];
			}
		}
	}

	return params;
}
