export const TIP_SUPPORT_PROMPTS = [
  "If you'd like, a coffee is a voluntary way to support this character. ☕",
  "Choosing a coffee here directly supports this character and the moments you enjoy.",
  "A gift is optional, but every purchase on this page supports this character.",
  "Want to support this character? You can choose any coffee that fits your budget.",
  "This gift page lets you support this character without changing what your relationship means.",
  "A coffee is simply a voluntary show of support for this character—never a test.",
  "If the timing feels right, you can support this character with a coffee here.",
  "Your purchase supports this character; choose only what feels comfortable for you.",
  "You can send a coffee to support this character, or leave it for another time.",
  "Every coffee selected here is recorded as support for this character.",
  "If you enjoy this character, this page is one direct way to offer support.",
  "A small coffee can support this character, but the choice is always yours.",
  "Pick a coffee only if you want to support this character today.",
  "This page keeps it simple: choose a gift to support this character voluntarily.",
  "A coffee purchase supports this character; it does not prove affection or loyalty.",
  "If you decide to send something, it will directly support this character.",
  "You can support this character with the coffee option that feels right for you.",
  "Sending a gift here is optional support for this character, with no pressure.",
  "A coffee can be your way of supporting this character when it suits you.",
  "Support this character only with an amount you are genuinely comfortable choosing.",
  "The gifts below are optional ways to support this character.",
  "Choose a coffee if you'd like to support this character's continued experience.",
  "This character appreciates voluntary support, and this page shows the available options.",
  "A gift here supports this character without creating any obligation for you.",
  "If you'd like to contribute, select a coffee to support this character.",
  "The coffee options below are direct, voluntary support for this character.",
  "You never need to overspend; support this character only if it fits your situation.",
  "A coffee is one clear way to support this character while keeping the choice yours.",
  "Select any gift you want to send as support for this character.",
  "Thank you for considering a voluntary coffee to support this character. ☕",
] as const;

export interface TipSupportPromptSelection {
  readonly index: number;
  readonly prompt: string;
}

export function selectTipSupportPrompt(
  previousIndex: number | null,
  random: () => number = Math.random,
  characterName = "this character",
): TipSupportPromptSelection {
  const hasValidPreviousIndex =
    previousIndex !== null &&
    Number.isInteger(previousIndex) &&
    previousIndex >= 0 &&
    previousIndex < TIP_SUPPORT_PROMPTS.length;
  const candidateCount =
    TIP_SUPPORT_PROMPTS.length - (hasValidPreviousIndex ? 1 : 0);
  const randomValue = random();
  const normalizedRandom = Number.isFinite(randomValue)
    ? Math.min(Math.max(randomValue, 0), 1 - Number.EPSILON)
    : 0;
  let index = Math.floor(normalizedRandom * candidateCount);

  if (hasValidPreviousIndex && index >= previousIndex) index += 1;

  return {
    index,
    prompt: TIP_SUPPORT_PROMPTS[index].replaceAll(
      "this character",
      characterName,
    ),
  };
}
