import { apiGet } from "./client";
import { TENANT_DOMAIN } from "./config";
import type { ApiStoreItem, StoreItemsResponse } from "./types";

const PAGE_SIZE = 100;

export async function getStoreItems(): Promise<ApiStoreItem[]> {
  const first = await apiGet<StoreItemsResponse>(
    `/tenants/${TENANT_DOMAIN}/store-items?page=1&pageSize=${PAGE_SIZE}`,
  );

  const items = [...first.items];
  const totalPages = Math.max(1, Math.ceil(first.total / PAGE_SIZE));

  for (let page = 2; page <= totalPages; page += 1) {
    const next = await apiGet<StoreItemsResponse>(
      `/tenants/${TENANT_DOMAIN}/store-items?page=${page}&pageSize=${PAGE_SIZE}`,
    );
    items.push(...next.items);
  }

  return items;
}
