/**
 * Parse a comma-separated string of positive integer IDs.
 * Accepts either a plain string or a string[] (from searchParams).
 */
export function parseIdList(value: string | string[] | undefined): number[] | undefined {
  if (!value) return undefined;
  const raw = Array.isArray(value) ? value.join(",") : value;
  const ids = raw
    .split(",")
    .map((id) => Number(id.trim()))
    .filter((id) => Number.isFinite(id) && id > 0);
  return ids.length ? ids : undefined;
}

/** Default machine type IDs for entry/release searches */
export const preferredMachineIds = [27, 26, 8, 9];

/**
 * Parse machine type IDs from a comma-separated string,
 * falling back to preferredMachineIds when empty.
 */
export function parseMachineIds(value?: string): number[] {
  if (!value) return preferredMachineIds.slice();
  const ids = value
    .split(",")
    .map((id) => Number(id.trim()))
    .filter((id) => Number.isFinite(id) && id > 0);
  return ids.length ? ids : preferredMachineIds.slice();
}
