// --- ASA GENERATED START ---
// Database utilities — pagination, filtering, error handling.
// --- ASA GENERATED END ---

// --- USER CODE START ---

export interface PaginationParams {
  page?: number;
  pageSize?: number;
}

export interface PaginatedResult<T> {
  data: T[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

export function paginate<T>(
  data: T[],
  total: number,
  params: PaginationParams = {},
): PaginatedResult<T> {
  const page = params.page ?? 1;
  const pageSize = params.pageSize ?? 20;
  return {
    data,
    total,
    page,
    pageSize,
    totalPages: Math.ceil(total / pageSize),
  };
}

export function handleDbError(error: unknown): never {
  if (error instanceof Error) {
    throw new Error(`Database error: ${error.message}`);
  }
  throw new Error('Unknown database error');
}

// --- USER CODE END ---
