import type {
  CaptchaChallengeGenerator,
  CaptchaGeneratorContext,
  GeneratedCaptchaChallenge,
} from "../types.ts";

type InvisibleCaptchaType = "honeypot" | "timing" | "not-robot";

export class InvisibleCaptchaGenerator implements CaptchaChallengeGenerator {
  readonly type: InvisibleCaptchaType;

  constructor(type: InvisibleCaptchaType) {
    this.type = type;
  }

  generate(context: CaptchaGeneratorContext): GeneratedCaptchaChallenge {
    const honeypotField = `website_${context.randomId(5)}`;
    const timingToken = context.randomId(18);
    return {
      type: this.type,
      presentation: "invisible",
      prompt:
        this.type === "not-robot" ? "Confirm that you are not a robot" : "Automated abuse check",
      answer: JSON.stringify({ honeypot: "", timingToken }),
      answerKind: "invisible",
      inputMode: "none",
      metadata: {
        honeypotField,
        timingToken,
        minCompletionMs: context.minCompletionMs,
        interaction: this.type === "not-robot" ? "checkbox" : "automatic",
      },
    };
  }
}

export const honeypotCaptchaGenerator = new InvisibleCaptchaGenerator("honeypot");
export const timingCaptchaGenerator = new InvisibleCaptchaGenerator("timing");
export const notRobotCaptchaGenerator = new InvisibleCaptchaGenerator("not-robot");
