import { apiPost } from "./api";

type NextNumberResponse = {
  code: string;
  number: string;
};

export async function reserveNextNumber(code: string): Promise<string | null> {
  const result = await apiPost<NextNumberResponse>(`/api/v1/number-ranges/${encodeURIComponent(code)}/next`, {});
  return result.ok ? result.data.number : null;
}

export function matchesObjectSearch(number: string | null | undefined, title: string | null | undefined, query: string) {
  const needle = query.trim().toLowerCase();
  if (!needle) return true;
  return `${number ?? ""} ${title ?? ""}`.toLowerCase().includes(needle);
}
