import { useEffect } from "react"; import { Label } from "../../../components/ui/label.tsx"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../components/ui/select.tsx"; import { useLabConnections, type LabConnection } from "../../../hooks/useLabConnections.ts"; import type { SendMethod } from "../../../hooks/useSendOutDiagnosticOrder.ts"; export interface TestCoding { system?: string; code?: string; } const connectedTo = (connections: LabConnection[], npi: string) => connections.find((each) => each.npi === npi.trim() && each.active !== false && each.electronic === true); // The server sends a test only with a reviewed pairing to the laboratory's order code; anything else goes on // paper. Offering electronic without one let the desk choose a send that silently became paper. export const reviewedPairing = (connection: LabConnection | undefined, test?: TestCoding) => test?.code ? (connection?.testMappings ?? []).find((mapping) => Boolean(mapping.reviewedAt) && mapping.system === test.system && mapping.code === test.code) : undefined; const pairedAt = (connection: LabConnection, test?: TestCoding) => Boolean(reviewedPairing(connection, test)); // Electronic is offered only where the server says it would send. The picker is not sent the address or the // credential state to work that out, so it reads the server's answer; paper is always offered, and is the only // choice when the answer is missing or the practice's connections cannot be read. export const sendsElectronically = (connections: LabConnection[], npi: string, test?: TestCoding) => { const connection = connectedTo(connections, npi); return Boolean(connection) && pairedAt(connection as LabConnection, test); }; interface Props { npi: string; test?: TestCoding; method: SendMethod; onChange: (method: SendMethod) => void; disabled?: boolean; } const SendMethodField = ({ npi, test, method, onChange, disabled }: Props) => { const { connections, loading, error } = useLabConnections(); const electronic = !loading && !error && sendsElectronically(connections, npi, test); const unpaired = !loading && !error && !electronic && Boolean(connectedTo(connections, npi)); useEffect(() => { onChange(electronic ? "electronic" : "paper"); }, [electronic, onChange]); return (
{unpaired ? (

This test has no reviewed pairing with this laboratory’s order codes, so it goes on paper.

) : null}
); }; export default SendMethodField;