"use client";

import { usePaginatedQuery } from "convex/react";
import { api } from "../../../../../convex/_generated/api";
import { ORDERS_PAGE_SIZE } from "./constants";
import type { OrderSummary } from "./types";

export function useOrders(statusFilter?: string[]): {
  orders: OrderSummary[];
  isLoading: boolean;
  hasMore: boolean;
  loadMore: () => void;
  status: "LoadingFirstPage" | "LoadingMore" | "Exhausted" | "CanLoadMore";
} {
  const activeFilter =
    statusFilter && statusFilter.length > 0 ? statusFilter : undefined;

  const { results, status, loadMore } = usePaginatedQuery(
    api.orders.listMine,
    { statusFilter: activeFilter },
    { initialNumItems: ORDERS_PAGE_SIZE },
  );

  return {
    orders: results as OrderSummary[],
    isLoading: status === "LoadingFirstPage",
    hasMore: status === "CanLoadMore",
    loadMore: () => loadMore(ORDERS_PAGE_SIZE),
    status,
  };
}
