import { DropdownOption } from "@/types/ui";

type GenerateOptionsParams<T> = {
  values: T[];
  valueKey?: keyof T;
  labelKey?: keyof T;
  includeAll?: boolean;
  allLabel?: string;
};

/**
 * Generic dropdown generator for both string[] and object[] values
 */
export function generateDropDownOptions<T extends string | Record<string, any>>(
  params: GenerateOptionsParams<T>
): DropdownOption[] {
  const {
    values,
    valueKey,
    labelKey,
    includeAll = false,
    allLabel = "All",
  } = params;

  const options: DropdownOption[] = values.map((item) => {
    if (typeof item === "string") {
      return { value: item, label: item };
    }

    return {
      value: String(item[valueKey as keyof T]),
      label: String(item[labelKey as keyof T]),
    };
  });

  if (includeAll) {
    return [{ value: "all", label: allLabel }, ...options];
  }

  return options;
}
