export const PIXVERSE_UI_MODEL = "pixverse-c1";
export const PIXVERSE_UI_LABEL = "PixVerse V6";
export const PIXVERSE_T2V_MODEL = "pixverse-c1-t2v";
export const PIXVERSE_I2V_MODEL = "pixverse-c1-i2v";
export const PIXVERSE_KF2V_MODEL = "pixverse-c1-kf2v";

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

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

export function toPixverseDisplayModel(model: string): string {
  return isPixverseModel(model) ? PIXVERSE_UI_MODEL : model;
}

export function normalizePixverseModelOptions<T extends PixverseModelOption>(options: T[]): T[] {
  let hasPixverse = false;

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

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

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

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

  if (imageReferenceCount >= 2) return PIXVERSE_KF2V_MODEL;
  if (imageReferenceCount <= 0) return PIXVERSE_T2V_MODEL;
  return PIXVERSE_I2V_MODEL;
}
