export interface PageResult<T> {
  items: T[]
  total: number
  limit?: number
  offset?: number
}

export function pageResult<T>(value: unknown): PageResult<T> {
  if (Array.isArray(value)) {
    return { items: value as T[], total: value.length }
  }
  const data = value as { items?: unknown; total?: unknown; limit?: unknown; offset?: unknown } | null | undefined
  const items = Array.isArray(data?.items) ? data.items as T[] : []
  return {
    items,
    total: Number(data?.total ?? items.length) || 0,
    limit: Number(data?.limit ?? items.length) || undefined,
    offset: Number(data?.offset ?? 0) || 0,
  }
}

export function listResult<T>(value: unknown): T[] {
  return pageResult<T>(value).items
}
