import type { DetectedField } from "./detect"; import { isAnyCredentialForm } from "./detect"; export interface AutofillInteractionTargets { usernameEl: HTMLInputElement | null; passwordEl: HTMLInputElement | null; shouldOpen: boolean; } /** * Explicit icon clicks should still work when passive heuristics are too * cautious. Prefer the real detected fields when present, but allow the click * path to open Peach even if the current page only exposes a partial form. */ export function resolveAutofillInteractionTargets( anchorField: HTMLInputElement, fields: DetectedField[], explicitSummon = false ): AutofillInteractionTargets { const userField = fields.find((f) => f.type === "username" || f.type === "email"); const pwField = fields.find((f) => f.type === "password"); const anchorType = anchorField.type.toLowerCase(); const fallbackType = anchorType === "password" ? "password" : "username"; return { usernameEl: userField?.el ?? (explicitSummon && fallbackType === "username" ? anchorField : null), passwordEl: pwField?.el ?? (explicitSummon && fallbackType === "password" ? anchorField : null), shouldOpen: explicitSummon || isAnyCredentialForm(fields), }; }