import { dropdownConfigs } from "@/config/DropDown.config";
import { DropdownOption } from "@/types/ui";
import { generateDropDownOptions } from "@/utils/generateDropDownOptions";

type ConfigObjectArray = {
  values: { [key: string]: any }[];
  allLabel: string;
  valueKey: string;
  labelKey: string;
};

function isObjectArrayConfig(config: any): config is ConfigObjectArray {
  return (
    Array.isArray(config.values) &&
    typeof config.values[0] === "object" &&
    "valueKey" in config &&
    "labelKey" in config
  );
}

/**
 * Generate dropdown from predefined config
 */
export function getDropdownOptions<T extends keyof typeof dropdownConfigs>(
  key: T,
  includeAll = false
): DropdownOption[] {
  const config = dropdownConfigs[key];

  if (isObjectArrayConfig(config)) {
    return generateDropDownOptions({
      values: config.values as Record<string, any>[], // TODO: need to fix ts any
      valueKey: config.valueKey,
      labelKey: config.labelKey,
      includeAll,
      allLabel: config.allLabel,
    });
  }

  return generateDropDownOptions({
    values: [...config.values] as string[],
    includeAll,
    allLabel: config.allLabel,
  });
}
