import type { ApiListResponse } from '@/shared/types/api';

export async function getApiListRows<TItem>(
  request: Promise<ApiListResponse<TItem>>,
): Promise<readonly TItem[]> {
  const response = await request;
  return response.rows;
}

export async function mapApiListRows<TItem, TOutput>(
  request: Promise<ApiListResponse<TItem>>,
  mapItem: (item: TItem) => TOutput,
): Promise<readonly TOutput[]> {
  const response = await request;
  return response.rows.map(mapItem);
}

export async function selectApiListRows<TItem, TOutput>(
  request: Promise<ApiListResponse<TItem>>,
  selectRows: (rows: readonly TItem[]) => TOutput,
): Promise<TOutput> {
  const response = await request;
  return selectRows(response.rows);
}
