export const VIDU_UI_MODEL = "vidu-q3-turbo";
export const VIDU_UI_LABEL = "Vidu Q3 Turbo";
export const VIDU_T2V_MODEL = "vidu-q3-turbo-t2v";
export const VIDU_I2V_MODEL = "vidu-q3-turbo-i2v";

export interface ViduModelOption {
  value: string;
  label: string;
  description?: string;
  badge?: string;
}

export function isViduModel(model: string | undefined | null): boolean {
  return String(model || "").toLowerCase().includes("vidu");
}

export function toViduDisplayModel(model: string): string {
  return isViduModel(model) ? VIDU_UI_MODEL : model;
}

export function normalizeViduModelOptions<T extends ViduModelOption>(options: T[]): T[] {
  let hasVidu = false;

  return options.reduce<T[]>((result, option) => {
    if (!isViduModel(option.value)) {
      result.push(option);
      return result;
    }

    if (hasVidu) return result;
    hasVidu = true;
    result.push({
      ...option,
      value: VIDU_UI_MODEL,
      label: VIDU_UI_LABEL,
      description: "自动匹配文生视频或图生视频",
    });
    return result;
  }, []);
}

export function resolveViduRequestModel(input: {
  model: string;
  referenceUrls?: string[];
  imageReferenceCount?: number;
}): string {
  if (!isViduModel(input.model)) return input.model;

  const imageReferenceCount =
    typeof input.imageReferenceCount === "number"
      ? input.imageReferenceCount
      : (input.referenceUrls || []).filter((url) => String(url || "").trim()).length;

  if (imageReferenceCount <= 0) return VIDU_T2V_MODEL;
  return VIDU_I2V_MODEL;
}
