import type { QueryParams } from "./types";

/**
 * Arrays use one comma-separated query value. URLSearchParams performs the
 * percent encoding, so ["MAIN", "SUB_PLAN"] becomes MAIN%2CSUB_PLAN.
 */
export function serializeQuery(params?: QueryParams): string {
  if (!params) return "";

  const search = new URLSearchParams();
  for (const [key, value] of Object.entries(params)) {
    if (value === undefined || value === null) continue;
    const serialized = Array.isArray(value)
      ? value.map(String).join(",")
      : String(value);
    search.append(key, serialized);
  }
  return search.toString();
}
