1import type { DetectedField } from "./detect";2import { isAnyCredentialForm } from "./detect";34export interface AutofillInteractionTargets {5 usernameEl: HTMLInputElement | null;6 passwordEl: HTMLInputElement | null;7 8 emailEl: HTMLInputElement | null;9 shouldOpen: boolean;10}11121314151617export function resolveAutofillInteractionTargets(18 anchorField: HTMLInputElement,19 fields: DetectedField[],20 explicitSummon = false21): AutofillInteractionTargets {22 const usernameField = fields.find((f) => f.type === "username");23 const emailField = fields.find((f) => f.type === "email");24 const pwField = fields.find((f) => f.type === "password");25 const anchorType = anchorField.type.toLowerCase();26 const fallbackType = anchorType === "password" ? "password" : "username";2728 29 30 31 const hasDistinctUsername = !!usernameField;32 const identityEl = usernameField?.el ?? emailField?.el ?? null;3334 return {35 usernameEl: identityEl ?? (explicitSummon && fallbackType === "username" ? anchorField : null),36 passwordEl: pwField?.el ?? (explicitSummon && fallbackType === "password" ? anchorField : null),37 emailEl: hasDistinctUsername ? (emailField?.el ?? null) : null,38 shouldOpen: explicitSummon || isAnyCredentialForm(fields),39 };40}