import { resolveClaimBeneficiary } from "./blame-claim-eligibility"; import { resolveVehicleIdentity } from "./vehicle-identity"; /** Resolve the damaged car once for panel responses and AI requests alike. */ export function resolveClaimVehicleSources(claim: any, blame: any) { const beneficiary = blame ? resolveClaimBeneficiary(blame) : null; const beneficiaryId = beneficiary?.person?.userId ?? claim?.damagedPartyUserId; const parties = blame?.parties?.length ? blame.parties : claim?.snapshot?.parties; const party = beneficiary ?? parties?.find( (p: any) => beneficiaryId && String(p.person?.userId) === String(beneficiaryId), ); const isBeneficiaryOwner = beneficiaryId && String(claim?.owner?.userId) === String(beneficiaryId); const standaloneOwner = !claim?.blameRequestId && !blame && !claim?.snapshot?.parties?.length && !beneficiaryId && !!claim?.owner?.userId; const snapshot = isBeneficiaryOwner || standaloneOwner ? (claim?.vehicle ?? {}) : {}; const role = party?.role ?? (isBeneficiaryOwner ? claim?.owner?.userRole : undefined); const bodyPolicy = role ? claim?.inquiries?.carBody?.data?.[role]?.mapped : undefined; const thirdPolicy = role ? claim?.inquiries?.thirdParty?.data?.[role]?.mapped : undefined; const records = blame?.type === "CAR_BODY" ? [bodyPolicy, thirdPolicy] : [thirdPolicy, bodyPolicy]; // Retain saved claim details only when that snapshot's owner is verified above. const vehicle = { ...snapshot, ...(party?.vehicle ?? {}) }; const inquiry = vehicle.inquiry?.mapped ?? vehicle.inquiry ?? {}; const policy = records.find(Boolean) ?? {}; const review = claim?.fanavaranVehicleReview; const identity = resolveVehicleIdentity(vehicle, records, review); return { party, vehicle, inquiry, policy, snapshot, review: review?.approved === true ? review : {}, identity, }; }