/** PDF page 39 permits capacity that has not been allocated to a predefined role. */
export function isRoleCapacityWithinTotal(total: number, allocated: number): boolean {
  return Number.isFinite(total) && Number.isFinite(allocated) && total >= 0 && allocated >= 0 && allocated <= total;
}

/**
 * PDF p.39: «إضافة دور» stays available while the roles leave seats free. A
 * valid total is not a reason to refuse a new role: that inverted check kept
 * the button disabled even with no roles defined.
 */
export function canAddVolunteerRole(total: number, allocated: number): boolean {
  return isRoleCapacityWithinTotal(total, allocated) && allocated < total;
}

export const UNASSIGNED_ROLE_VALUE = "__unassigned__";
export function unassignedRoleOption(remaining: unknown, language: string) {
  if (typeof remaining !== "number" || !Number.isInteger(remaining) || remaining <= 0) return null;
  return { value: UNASSIGNED_ROLE_VALUE, label: language === "ar" ? "دون دور محدد" : "No assigned role", remaining_slots: remaining, instructions: "" };
}
