import { apiFetch } from "@/lib/auth/api-client"; import { CreateOptions, idempotencyHeaders } from "@/lib/offline/idempotency"; import { CreateTreatmentInput, Treatment, UpdateTreatmentInput, WithdrawalStatus, } from "./types"; export function fetchColonyTreatments(colonyId: string): Promise { return apiFetch(`/colonies/${encodeURIComponent(colonyId)}/treatments`); } /** * Active withdrawal windows of the given colonies at `at` (a harvest date, * "YYYY-MM-DD"); without `at` the server checks "now". Built with * URLSearchParams so an id can never add or override a query parameter. */ export function fetchWithdrawalStatus( colonyIds: string[], at?: string, ): Promise { if (colonyIds.length === 0) { return Promise.resolve([]); } const params = new URLSearchParams({ colonyIds: colonyIds.join(",") }); if (at) params.set("at", at); return apiFetch(`/treatments/withdrawal-status?${params.toString()}`); } export function createTreatment( input: CreateTreatmentInput, options: CreateOptions = {}, ): Promise { return apiFetch("/treatments", { method: "POST", headers: idempotencyHeaders(options), body: JSON.stringify(input), }); } export function updateTreatment( id: string, input: UpdateTreatmentInput, ): Promise { return apiFetch(`/treatments/${encodeURIComponent(id)}`, { method: "PATCH", body: JSON.stringify(input), }); }