import { ClaimCaseStatus } from "src/Types&Enums/claim-request-management/claim-case-status.enum"; import { ClaimWorkflowStep } from "src/Types&Enums/claim-request-management/claim-workflow-steps.enum"; import { getDamagedPartCaptureBlob } from "./claim-car-angle-media"; import { normalizeDamageSelectedParts } from "./outer-damage-parts"; import { ClaimVehicleTypeV2 } from "src/static/outer-car-parts-catalog"; import { canonicalizeResendDocumentKey } from "./claim-resend-document-keys"; export function resendRequestHasPayload(r: { resendDescription?: string; resendDocuments?: unknown[]; resendCarParts?: unknown[]; }): boolean { if (!r) return false; if (String(r.resendDescription || "").trim() !== "") return true; if (Array.isArray(r.resendDocuments) && r.resendDocuments.length > 0) return true; if (Array.isArray(r.resendCarParts) && r.resendCarParts.length > 0) return true; return false; } /** Normalize expert-requested document keys (strings or `{ key }`) to canonical enum strings. */ export function normalizeResendDocumentKeys(docs: unknown[] | undefined): string[] { if (!Array.isArray(docs)) return []; const keys: string[] = []; for (const d of docs) { if (typeof d === "string" && d.trim()) { const c = canonicalizeResendDocumentKey(d.trim()); if (c) keys.push(c); } else if (d && typeof d === "object" && "key" in (d as object)) { const k = String((d as { key?: string }).key || "").trim(); const c = canonicalizeResendDocumentKey(k); if (c) keys.push(c); } } return [...new Set(keys)]; } /** Normalize expert-requested damaged part keys (`DamagedPartItem.key`). */ export function normalizeResendPartKeys(parts: unknown[] | undefined): string[] { if (!Array.isArray(parts)) return []; const keys: string[] = []; for (const p of parts) { if (typeof p === "string" && p.trim()) keys.push(p.trim()); else if (p && typeof p === "object" && "key" in (p as object)) { const k = String((p as { key?: string }).key || "").trim(); if (k) keys.push(k); } } return keys; } function getRequiredDocEntry(claim: any, documentKey: string): unknown { const rd = claim?.requiredDocuments; if (!rd) return undefined; return rd instanceof Map ? rd.get(documentKey) : rd[documentKey]; } export function isRequiredDocumentUploaded(claim: any, documentKey: string): boolean { const doc = getRequiredDocEntry(claim, documentKey) as { uploaded?: boolean } | undefined; return !!doc?.uploaded; } export function hasDamagedPartCapture(claim: any, partKey: string): boolean { const data = claim?.media?.damagedParts; if (!data) return false; const norm = normalizeDamageSelectedParts( claim?.damage?.selectedParts, claim?.vehicle?.carType as ClaimVehicleTypeV2 | undefined, claim?.damage?.selectedOuterParts, ); return getDamagedPartCaptureBlob(data, partKey, norm) != null; } /** * True when the user has satisfied every document and part the damage expert asked for, * or when the expert only left instructions (no doc/part list) and there is a description. */ export function canFinalizeExpertResend(claim: any): boolean { if (!claim) return false; if (claim.status !== ClaimCaseStatus.WAITING_FOR_USER_RESEND) return false; if (claim.workflow?.currentStep !== ClaimWorkflowStep.USER_EXPERT_RESEND) return false; const r = claim.evaluation?.damageExpertResend; if (!r || r.fulfilledAt) return false; const docKeys = normalizeResendDocumentKeys(r.resendDocuments); const partKeys = normalizeResendPartKeys(r.resendCarParts); if (docKeys.length === 0 && partKeys.length === 0) { return String(r.resendDescription || "").trim() !== ""; } for (const k of docKeys) { if (!isRequiredDocumentUploaded(claim, k)) return false; } for (const k of partKeys) { if (!hasDamagedPartCapture(claim, k)) return false; } return true; } export function documentKeyAllowedForExpertResend( claim: any, documentKey: string, ): boolean { const r = claim?.evaluation?.damageExpertResend; if (!r || r.fulfilledAt) return false; const allowed = new Set(normalizeResendDocumentKeys(r.resendDocuments)); const normalized = canonicalizeResendDocumentKey(documentKey) ?? documentKey.trim(); return allowed.has(normalized); } export function partKeyAllowedForExpertResend(claim: any, partKey: string): boolean { const r = claim?.evaluation?.damageExpertResend; if (!r || r.fulfilledAt) return false; const allowed = new Set(normalizeResendPartKeys(r.resendCarParts)); return allowed.has(partKey); }