import { IndicatorCircle } from '@/common/components/atoms/IndicatorCircle/IndicatorCircle'; import { ReadOnlyDetail } from '@/common/components/atoms/ReadOnlyDetail/ReadOnlyDetail'; import { createBlocksTyped } from '@/lib/blocks/create-blocks-typed/create-blocks-typed'; import { useMemo } from 'react'; import { ctw } from '@/common/utils/ctw/ctw'; import { inquiredMatchedMerchantsColumns, terminatedMatchedMerchantsColumns, terminatedMatchedMerchantsSummaryColumns, } from '@/lib/blocks/hooks/useMerchantScreeningBlock/columns'; import { formatValue } from '@/lib/blocks/hooks/useMerchantScreeningBlock/format-value'; import { IMerchantScreening } from '@/lib/blocks/hooks/useMerchantScreeningBlock/interfaces'; import { isObject, safeEvery } from '@ballerine/common'; import { JsonDialog } from '@ballerine/ui'; import { toTitleCase } from 'string-ts'; export const useMerchantScreeningBlock = ({ terminatedMatchedMerchants, inquiredMatchedMerchants, merchantScreeningInput, logoUrl = 'https://cdn.ballerine.io/logos/Mastercard%20logo.svg', rawData, checkDate, }: { terminatedMatchedMerchants: IMerchantScreening[]; inquiredMatchedMerchants: IMerchantScreening[]; merchantScreeningInput: Record; logoUrl: string; rawData: Record; checkDate: string; }) => { return useMemo(() => { return createBlocksTyped() .addBlock() .addCell({ type: 'block', value: createBlocksTyped() .addBlock() .addCell({ type: 'container', value: createBlocksTyped() .addBlock() .addCell({ type: 'image', value: logoUrl, props: { width: '40px', height: '40px', className: '[&>figcaption]:sr-only', }, }) .addCell({ type: 'heading', value: 'MATCH Results', props: { className: 'mt-0 p-0', }, }) .buildFlat(), props: { className: 'flex space-x-4 items-center my-8', }, }) .addCell({ type: 'container', value: createBlocksTyped() .addBlock() .addCell({ type: 'table', value: { columns: terminatedMatchedMerchantsSummaryColumns, data: [ { terminatedMatches: terminatedMatchedMerchants?.length ?? 0, numberOfInquiries: inquiredMatchedMerchants?.length ?? 0, checkDate, merchantScreeningInput: merchantScreeningInput ?? {}, fullJsonData: rawData ?? {}, }, ], props: { head: { className: '!ps-0', }, cell: { className: '!ps-0', }, }, }, }) .buildFlat(), props: { className: 'mb-16', }, }) .addCell({ type: 'container', value: createBlocksTyped() .addBlock() .addCell({ type: 'container', value: createBlocksTyped() .addBlock() .addCell({ type: 'subheading', value: 'Terminated Merchants Matches', props: { className: 'ps-0 mb-4 ms-0', }, }) .addCell({ type: 'dataTable', value: { props: { table: { className: 'my-8', }, scroll: { className: ctw('h-[26rem]', { 'h-34': !terminatedMatchedMerchants?.length, }), }, }, options: { enableSorting: false, }, columns: terminatedMatchedMerchantsColumns, data: terminatedMatchedMerchants, CollapsibleContent: ({ row: terminatedMatchedMerchant, }: { row: IMerchantScreening; }) => { const isEmptyPrincipalMatches = safeEvery( terminatedMatchedMerchant?.principals, principal => Object.keys(principal?.exactMatches ?? {}).length === 0 && Object.keys(principal?.partialMatches ?? {}).length === 0, ); const isEmptyUrlMatches = safeEvery( terminatedMatchedMerchant?.urls, url => Object.keys(url?.exactMatches ?? {}).length === 0 && Object.keys(url?.partialMatches ?? {}).length === 0, ); return (

Matching Properties

{!Object.keys(terminatedMatchedMerchant?.exactMatches ?? {}) .length && (

No matching properties found.

)} {!!Object.keys(terminatedMatchedMerchant?.exactMatches ?? {}) .length && (
  • Merchant Information
    • {Object.entries(terminatedMatchedMerchant?.exactMatches).map( ([key, value]) => (
    • {toTitleCase(key)}
      {formatValue({ key, value })}
    • ), )} {Object.entries(terminatedMatchedMerchant?.partialMatches).map( ([key, value]) => (
    • {toTitleCase(key)}
      {formatValue({ key, value })}
    • ), )}
)} {!isEmptyPrincipalMatches && terminatedMatchedMerchant?.principals.map( (principalMatch, index) => { if ( !Object.keys(principalMatch?.exactMatches ?? {}).length && !Object.keys(principalMatch?.partialMatches ?? {}).length ) { return; } return (
  • Principal {index + 1}
    • {isObject(principalMatch?.exactMatches) && !!Object.keys(principalMatch?.exactMatches).length && Object.entries(principalMatch?.exactMatches).map( ([key, value]) => (
    • {toTitleCase(key)}
      {formatValue({ key, value })}
    • ), )} {isObject(principalMatch?.partialMatches) && !!Object.keys(principalMatch?.partialMatches).length && Object.entries(principalMatch?.partialMatches).map( ([key, value]) => (
    • {toTitleCase(key)}
      {formatValue({ key, value })}
    • ), )}
); }, )} {!isEmptyUrlMatches && terminatedMatchedMerchant?.urls.map((urlMatch, index) => { if ( !Object.keys(urlMatch?.exactMatches ?? {}).length && !Object.keys(urlMatch?.partialMatches ?? {}).length ) { return; } return (
  • Url {index + 1}
    • {isObject(urlMatch?.exactMatches) && !!Object.keys(urlMatch?.exactMatches).length && Object.entries(urlMatch?.exactMatches).map( ([key, value]) => (
    • {formatValue({ key, value })}
    • ), )} {isObject(urlMatch?.partialMatches) && !!Object.keys(urlMatch?.partialMatches).length && Object.entries(urlMatch?.partialMatches).map( ([key, value]) => (
    • {formatValue({ key, value })}
    • ), )}
); })}
); }, }, }) .buildFlat(), }) .addCell({ type: 'container', value: createBlocksTyped() .addBlock() .addCell({ type: 'subheading', value: 'Inquired Merchants Matches', props: { className: 'ps-0 mb-4 ms-0', }, }) .addCell({ type: 'dataTable', value: { props: { table: { className: 'my-8', }, scroll: { className: ctw('h-[26rem]', { 'h-34': !inquiredMatchedMerchants.length, }), }, }, options: { enableSorting: false, }, columns: inquiredMatchedMerchantsColumns, data: inquiredMatchedMerchants, CollapsibleContent: ({ row: inquiredMatchedMerchant, }: { row: IMerchantScreening; }) => { const isEmptyPrincipalMatches = safeEvery( inquiredMatchedMerchant?.principals, principal => Object.keys(principal?.exactMatches ?? {}).length === 0 && Object.keys(principal?.partialMatches ?? {}).length === 0, ); const isEmptyUrlMatches = safeEvery( inquiredMatchedMerchant?.urls, url => Object.keys(url?.exactMatches ?? {}).length === 0 && Object.keys(url?.partialMatches ?? {}).length === 0, ); return (

Matching Properties

{!Object.keys(inquiredMatchedMerchant?.exactMatches ?? {}).length && (

No matching properties found.

)} {!!Object.keys(inquiredMatchedMerchant?.exactMatches ?? {}) .length && (
  • Merchant Information
    • {Object.entries( inquiredMatchedMerchant?.exactMatches ?? {}, ).map(([key, value]) => (
    • {toTitleCase(key)}
      {formatValue({ key, value })}
    • ))} {Object.entries( inquiredMatchedMerchant?.partialMatches ?? {}, ).map(([key, value]) => (
    • {toTitleCase(key)}
      {formatValue({ key, value })}
    • ))}
)} {!isEmptyPrincipalMatches && inquiredMatchedMerchant?.principals.map((principalMatch, index) => { if ( !Object.keys(principalMatch?.exactMatches ?? {}).length && !Object.keys(principalMatch?.partialMatches ?? {}).length ) { return; } return (
  • Principal {index + 1}
    • {isObject(principalMatch?.exactMatches) && !!Object.keys(principalMatch?.exactMatches).length && Object.entries(principalMatch?.exactMatches).map( ([key, value]) => (
    • {toTitleCase(key)}
      {formatValue({ key, value })}
    • ), )} {isObject(principalMatch?.partialMatches) && !!Object.keys(principalMatch?.partialMatches).length && Object.entries(principalMatch?.partialMatches).map( ([key, value]) => (
    • {toTitleCase(key)}
      {formatValue({ key, value })}
    • ), )}
); })} {!isEmptyUrlMatches && inquiredMatchedMerchant?.urls?.map((urlMatch, index) => { if ( !Object.keys(urlMatch?.exactMatches ?? {}).length && !Object.keys(urlMatch?.partialMatches ?? {}).length ) { return; } return (
  • Url {index + 1}
    • {isObject(urlMatch?.exactMatches) && !!Object.keys(urlMatch?.exactMatches).length && Object.entries(urlMatch?.exactMatches).map( ([key, value]) => (
    • {formatValue({ key, value })}
    • ), )} {isObject(urlMatch?.partialMatches) && !!Object.keys(urlMatch?.partialMatches).length && Object.entries(urlMatch?.partialMatches).map( ([key, value]) => (
    • {formatValue({ key, value })}
    • ), )}
); })}
); }, }, }) .buildFlat(), }) .buildFlat(), props: { className: 'flex flex-col space-y-6', }, }) .buildFlat(), }) .build(); }, [inquiredMatchedMerchants, logoUrl, terminatedMatchedMerchants]); };