import type { AdapterResult, CommercialUsageCounter } from "../types";
import { commercialGet, extractItems, failResult, listResult } from "./http";

export async function loadCommercialUsage(
  tenantId: string
): Promise<AdapterResult<CommercialUsageCounter[]>> {
  const res = await commercialGet<unknown>(
    `/api/v1/commercial/usage?tenant_id=${encodeURIComponent(tenantId)}`
  );
  if (!res.ok) {
    return failResult(res.availability, res.message || "مصرف/سهمیه در دسترس نیست", []);
  }
  const items = extractItems<CommercialUsageCounter>(res.data, ["items", "usage", "quotas"]);
  return listResult(items, "commercial.usage", "شمارنده‌ای ثبت نشده است");
}
