All files / src/features/Merchants/MerchantsTable/hooks useGetMerchantsTableColumns.tsx

88.23% Statements 135/153
77.84% Branches 137/176
86.04% Functions 37/43
90.78% Lines 128/141

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777                                                                                        33x 3868x 3868x         3868x 3868x   3868x   3868x 3868x 3868x     3868x 3868x 3868x 3868x 3868x   3868x 3868x 3868x 3868x 3868x   3868x 3868x 3776x 3771x 51x 3720x 3558x     3868x   3868x 3868x   3868x 3868x 3868x   3868x         3868x                                 3868x 3868x       3868x 1159x   1159x           684x                           3868x 843x           552x 552x   552x                                                       3868x 843x             636x 636x   636x                             3868x 1159x 1159x 1105x 1105x   1159x                 908x               908x                                     908x                     3868x 1159x 1159x 1088x 1007x   1159x 48x 48x 24x   1159x             48x             48x             48x                                         3868x 843x             36x                                         3868x 843x               36x   36x   36x                                           3868x 843x           24x                 3868x 843x           24x                               3868x 843x           24x                                                         3868x 843x               24x         24x       24x   24x                               3868x 843x     843x               24x       24x           24x         3868x 843x           48x       48x                           3868x 888x                 60x               60x         3868x   3868x 1159x                 1159x                 1159x   71x                                         81x                   17x                       54x           27x                               54x           15x     17x     823x                                                                               3868x 1159x                 1040x                                                                             3868x           3868x                                        
import { useCallback, useMemo } from "react";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { MerchantData } from "@hooks/enterprise-api/account/types";
import { capitalizeEachWord, parseAmount } from "@utils/index";
import { TableColumnType } from "componentsV2/Table/Table.types";
import { QKEY_LIST_ACQUIRER_MERCHANTS } from "@constants/queryKeys";
import { selectMerchantTab } from "@redux/slices/enterprise/merchants";
import { useAppSelector } from "@redux/hooks";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { TAG_STATUSES_ENUM } from "@common/TextTag/types";
import GiveText from "@shared/Text/GiveText";
import { Box, Stack } from "@mui/material";
import MainInfo from "componentsV2/Table/MainInfo";
import StatusTag from "componentsV2/Table/StatusTag";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import RiskStatusChip, { TRiskLevel } from "componentsV2/Table/RiskStatusChip";
import GiveAssignButton from "@shared/AssignButton/GiveAssignButton";
import GiveUnderwritingScore from "@shared/UnderwritingScore/GiveUnderwritingScore";
import { checkPortals } from "@utils/routing";
import { useUnderwriterPermissions } from "features/Permissions/AccessControl/hooks";
import { setOpeningOptions } from "@redux/slices/merchantPreview";
import { useDispatch } from "react-redux";
import { AssigneeInfos } from "@shared/AssignButton/GiveAssignButton.types";
import useWatchlist from "./useWatchList";
import BadgeTooltipContent from "../components/BadgeTooltipContent";
import OnboardingScore from "../OnboardingScore";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { getSelectedTab } from "../utils";
import RiskLevelTag from "../components/RiskLevelTag";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { StatusMap } from "@components/Merchants/MerchantPreview/helpers/parsers";
import { useKYBColumns } from "./useKYBColumns";
import useUnderwritingColumns from "./useUnderwritingColumns";
import useSponsorColumns from "./useSponsorColumns";
import { usePendingColumns } from "./usePendingColumns";
import { useFormatDateInTimezone, formatInUTCMoment } from "@utils/date.helpers";
import GiveDateTooltip from "@shared/Tooltip/GiveDateTooltip";
import { useApprovedColumns } from "./useApprovedColumns";
import {
  getDefaultSort,
  getMerchantStatus,
  useGetQuery,
} from "@features/Merchants/MerchantsTable/utils";
 
export const useGetMerchantsTableColumns = () => {
  const { formatInTimezone } = useFormatDateInTimezone();
  const dispatch = useDispatch();
  const {
    isMerchantStatusRefactorEnabled,
    isNewApprovalFlowEnabled,
    isMerchantOnboardingModalEnabled,
  } = useGetFeatureFlagValues();
  const { isAcquirerPortal, isEnterprisePortal } = checkPortals();
  const { isWideView, isTabletView, isMobileView, isLargeView } =
    useCustomThemeV2();
  const { isViewUnderwritingAllowed, isUpdateAssigneeAllowed } =
    useUnderwriterPermissions();
  const { risk_monitoring, merchant_underwriting } = useEnterprisePermissions();
  const { addMerchantToWatchlist, removeMerchantFromWatchlist } = useWatchlist(
    QKEY_LIST_ACQUIRER_MERCHANTS,
  );
  const tab = useAppSelector(selectMerchantTab);
  const selectedTab = getSelectedTab(tab);
  const isUnderwritingSelected = selectedTab === "underwriting";
  const isInvitedTabSelected = selectedTab === "invited";
  const isRiskTabSelected = selectedTab === "risk";
  const isOnboardingSelected =
    selectedTab === "onboarding" && !isNewApprovalFlowEnabled; //TODO clean up when KYB is integrated
  const isKYBSelected = selectedTab === "kyb" && isNewApprovalFlowEnabled;
  const isSponsorTabSelected = selectedTab === "sponsor";
  const isApprovedTabSelected = selectedTab === "approved";
  const isPendingTabSelected = selectedTab === "pending";
 
  const merchantColWidth = (() => {
    if (isInvitedTabSelected) return 1.8;
    if (isKYBSelected && !isLargeView) return 3;
    if ((isUnderwritingSelected && isNewApprovalFlowEnabled) || isKYBSelected)
      return 4.8;
    if (isTabletView || (isOnboardingSelected && isWideView)) return 2.5;
    return 4;
  })();
 
  const getQuery = useGetQuery();
 
  const { kybProgressCol } = useKYBColumns();
  const { approvalDateCol } = useApprovedColumns();
  const { taxIdCol, KYBCol, assignedCol, underwritingProgressCol } =
    useUnderwritingColumns({});
  const { pendingProgressCol } = usePendingColumns();
  const { readyCol } = useSponsorColumns();
 
  const addToWatchlistHandler = (row?: MerchantData) => {
    if (!row) return;
    addMerchantToWatchlist(row?.accID, row?.name);
  };
 
  const isWarnedMerchant = (row: MerchantData) => {
    if (isInvitedTabSelected) return false;
    if (!row?.isProfileCompleted) return true;
    const isOfacMatch = ["possible_match", "confirmed_match"].includes(
      row?.entityOfac?.lastCheckStatusName,
    );
 
    const hasMIDIssue = isMerchantStatusRefactorEnabled
      ? row?.stateName === "has_mid_issue"
      : row?.merchantStatus === "preapproved_mid_issue";
 
    const isRiskLevel =
      row?.riskLevelLabel && row?.riskLevelLabel !== "low" && risk_monitoring;
 
    return isRiskLevel || isOfacMatch || hasMIDIssue;
  };
 
  const hideApprovedRiskColumn = !risk_monitoring && isEnterprisePortal;
  const handleClickScore = useCallback(() => {
    dispatch(setOpeningOptions({ underwriter: true }));
  }, []);
 
  const signupDateCol = useMemo(() => {
    const label = "Created";
 
    return {
      key: "createdAt,-accID",
      sortable: true,
      title: label,
      columnWidth: isKYBSelected ? 1.2 : isOnboardingSelected ? 1 : 1.4,
      renderColumn: (row: MerchantData) => {
        return (
          <GiveText
            data-testid={`give-row-signup-date-${row.createdAt}`}
            variant="bodyS"
          >
            {row?.createdAt
              ? formatInUTCMoment(row?.createdAt, "MMM DD, YYYY")
              : ""}
          </GiveText>
        );
      },
    };
  }, [tab]);
 
  const categoryRiskCol = useMemo(() => {
    return {
      key: "merchantRiskStatusSortOrder,-accID",
      sortable: true,
      title: "Risk Category",
      columnWidth: isSponsorTabSelected ? 1 : 1.3,
      renderColumn: (row: MerchantData) => {
        const isRiskLevel = (value: any): value is TRiskLevel => {
          return ["normal", "high", "restricted"].includes(value);
        };
        return (
          <GiveTooltip
            color="default"
            disableHoverListener={
              !row?.categoryCode ||
              !row?.categoryCodeTitle ||
              !row?.merchantRiskStatus
            }
            title={
              <GiveText color="default" variant="bodyS">
                {row?.categoryCode}- {row?.categoryCodeTitle}
              </GiveText>
            }
            alignment="start"
          >
            <RiskStatusChip
              riskLevelName={
                isRiskLevel(row?.merchantRiskStatus)
                  ? row?.merchantRiskStatus
                  : null
              }
            />
          </GiveTooltip>
        );
      },
    };
  }, []);
 
  const processedCol = useMemo(() => {
    return {
      key: "totalProcessed,-accID",
      sortable: true,
      title: "Processed (USD)",
      headerPosition: "right",
      columnWidth: "140px",
      renderColumn: (row: MerchantData) => {
        const totalProcessed = row?.totalProcessed / 100;
        const revenue = parseAmount(totalProcessed);
 
        return (
          <Box display="flex" width="100%" justifyContent="flex-end">
            <GiveTooltip
              title={Math.abs(totalProcessed)?.toLocaleString()}
              placement="bottom"
              disableHoverListener={Math.abs(totalProcessed || 0) < 1_000_000}
            >
              <GiveText variant="bodyS">{revenue}</GiveText>
            </GiveTooltip>
          </Box>
        );
      },
    } as TableColumnType;
  }, []);
 
  const statusCol = useMemo(() => {
    const sortKey = (() => {
      if (isInvitedTabSelected) return "ownerInviteStatusDisplayName";
      Iif (isMerchantStatusRefactorEnabled) return "stateSortOrder";
      else return "statusSortOrder";
    })();
    return {
      key: `${
        isInvitedTabSelected ? "ownerInviteStatusDisplayName" : sortKey
      },-accID`,
      sortable: true,
      title: "Status",
      columnWidth: isMobileView ? "114px" : "170px",
      columnType: "status-round" as const,
      renderColumn: (row: MerchantData) => {
        const status = getMerchantStatus(
          row,
          isInvitedTabSelected,
          isMerchantStatusRefactorEnabled,
          isNewApprovalFlowEnabled,
          isMerchantOnboardingModalEnabled,
        ) as TAG_STATUSES_ENUM;
 
        const statusData = {
          [TAG_STATUSES_ENUM.APPROVED]: {
            name: `${row?.approvedByFirstName} ${row?.approvedByLastName}`,
            updatedAt: Number(row?.approvedAt || 0),
          },
          [TAG_STATUSES_ENUM.SUSPENDED]: {
            name: `${row?.suspendedByFirstName} ${row?.suspendedByLastName}`,
            updatedAt: Number(row?.suspendedAt || 0),
          },
          [TAG_STATUSES_ENUM.DECLINED]: {
            name: `${row?.declinedByFirstName} ${row?.declinedByLastName}`,
            updatedAt: Number(row?.declinedAt || 0),
          },
          [TAG_STATUSES_ENUM.INVITE]: {
            updatedAt: Number(row?.OwnerInviteLastSentAt || 0),
          },
          [TAG_STATUSES_ENUM.ONBOARDING]: {},
        } as Record<TAG_STATUSES_ENUM, { name?: string; updatedAt: number }>;
 
        return (
          <StatusTag
            statusDisplayName={status}
            updatedBy={statusData[status]?.name}
            updatedAt={statusData[status]?.updatedAt}
          />
        );
      },
    };
  }, [tab, isMobileView]);
 
  const assignmentCol = useMemo(() => {
    const getWidth = () => {
      if (isUnderwritingSelected) return 1.5;
      if (isRiskTabSelected) return 1.2;
      else return 1;
    };
    const getAssigneeType = () => {
      Iif (isRiskTabSelected) return "risk";
      if (isOnboardingSelected) return "sales";
      else return undefined;
    };
    return {
      key: "assignment",
      sortable: false,
      title: "Assignee",
      columnWidth: getWidth(),
      columnType: "assignment" as const,
      renderColumn: (row: MerchantData) => {
        const status = getMerchantStatus(
          row,
          isInvitedTabSelected,
          isMerchantStatusRefactorEnabled,
          isNewApprovalFlowEnabled,
        ) as TAG_STATUSES_ENUM;
 
        Iif (
          status === TAG_STATUSES_ENUM.PENDING &&
          isMerchantOnboardingModalEnabled
        ) {
          return null;
        }
 
        return (
          <GiveAssignButton
            data={row as AssigneeInfos}
            disabled={false}
            showCaretIcon={false}
            handleClick={() => {
              // this is useless we are already calling inside the GiveAssignButton
              // => queryClient.invalidateQueries(QKEY_LIST_ENTERPRISE_STATS);
            }}
            type={getAssigneeType()}
            query={getQuery(status)}
            transformOrigin={{
              vertical: -45,
              horizontal: "left",
            }}
          />
        );
      },
    };
  }, [tab]);
 
  const underwritingScoreCol = useMemo(() => {
    return {
      key: "underwritingChallengeScorePercentage,-accID",
      sortable: true,
      title: "Underwriting Score",
      columnWidth: !isWideView ? 1 : 1.6,
      columnType: isWideView ? "text" : "empty",
      renderColumn: (row: MerchantData) => {
        return (
          <Box width={isWideView ? "140px" : "auto"}>
            <GiveUnderwritingScore
              statusDisplayName={row?.statusDisplayName}
              score={row?.underwritingChallengeScorePercentage}
              canProcessMoney={row?.canProcessMoney}
              canTransferMoney={row?.canTransferMoney}
              pendingScore={row?.underwritingPendingChallengesPercentage}
              pendingChallenges={row?.underwritingPendingChallengesCount}
              merchant_underwriting={merchant_underwriting}
              isViewUnderwritingAllowed={isViewUnderwritingAllowed}
              handleClick={handleClickScore}
              hideIcons
              alwaysDisplayProgressBars
            />
          </Box>
        );
      },
    };
  }, [isWideView]);
 
  const onboardingScoreCol = useMemo(() => {
    return {
      key: "completedChallengesCount,-accID",
      sortable: false,
      title: "Onboarding Progress",
      columnWidth: 1,
      columnType: isWideView ? "text" : "empty",
      renderColumn: (row: MerchantData) => {
        const { onboardingChallengesCount, completedChallengesCount } =
          row || {};
        const percentage =
          ((completedChallengesCount || 0) * 100) /
          (onboardingChallengesCount || 0);
        return (
          <Stack
            direction="row"
            alignItems="center"
            justifyContent="space-between"
            gap="12px"
          >
            <GiveText
              variant={isWideView ? "bodyS" : "bodyXS"}
              color={!completedChallengesCount ? "tertiary" : "primary"}
            >
              {completedChallengesCount}/{onboardingChallengesCount}
            </GiveText>
            <Box width={isWideView ? "115px" : "auto"}>
              <OnboardingScore score={percentage} />
            </Box>
          </Stack>
        );
      },
    };
  }, [isWideView]);
 
  const emailCol = useMemo(() => {
    return {
      key: "email",
      sortable: false,
      title: "Email",
      columnWidth: 2.1,
      renderColumn: (row: MerchantData) => {
        return (
          <GiveTruncateText lineClamp={1} variant="bodyS">
            {row?.ownerEmail}
          </GiveTruncateText>
        );
      },
    };
  }, []);
 
  const inviteCol = useMemo(() => {
    return {
      key: "ownerInviteLastSentAt,-accID",
      sortable: true,
      title: "Invited",
      columnWidth: 0.7,
      renderColumn: (row: MerchantData) => {
        return (
          <GiveDateTooltip>
            <GiveText
              variant="bodyS"
              data-testid={`invitedate-${row?.ownerInviteLastSentAt}`}
            >
              {row?.ownerInviteLastSentAt
                ? formatInTimezone(row?.ownerInviteLastSentAt, "MMM dd, yyyy")
                : ""}
            </GiveText>
          </GiveDateTooltip>
        );
      },
    };
  }, []);
 
  const mccCol = useMemo(() => {
    return {
      key: "categoryCode,-accID",
      sortable: false,
      title: "MCC",
      columnWidth: 3,
      renderColumn: (row: MerchantData) => {
        return (
          <GiveTooltip
            disableHoverListener={!row?.categoryCode || !row?.categoryCodeTitle}
            title={
              <GiveText color="default" variant="bodyS">
                {row?.categoryCodeTitle}
                <br />
                {row?.categoryCode}
              </GiveText>
            }
            placement={isMobileView ? "bottom" : "top-start"}
            alignment="start"
          >
            <Stack gap="4px">
              <>
                <GiveTruncateText lineClamp={1} variant="bodyS">
                  {row?.categoryCodeTitle}
                </GiveTruncateText>
                <GiveText variant="bodyXS" color="secondary">
                  {row?.categoryCode}
                </GiveText>
              </>
            </Stack>
          </GiveTooltip>
        );
      },
    };
  }, []);
 
  const actionRequiredCol = useMemo(() => {
    return {
      key: isMerchantStatusRefactorEnabled
        ? "approvalAt"
        : "lastInternalApprovalAt",
      sortable: true,
      title: "Action Required",
      columnWidth: 1.2,
      renderColumn: (row: MerchantData) => {
        const statusName = isMerchantStatusRefactorEnabled
          ? row?.approvalStateName
          : row?.lastInternalApprovalTypeDisplayName ?? "";
 
        const date =
          (isMerchantStatusRefactorEnabled
            ? row?.approvalAt
            : row?.lastInternalApprovalAt) ?? 0;
 
        const parsedDate = date ? formatInTimezone(date, "MMM dd, yyyy") : "-";
 
        return (
          <Stack gap="4px">
            <GiveText variant="bodyS">
              {capitalizeEachWord(statusName as string, "-")}
            </GiveText>
            <GiveDateTooltip>
              <GiveText variant="bodyXS" color="secondary">
                {parsedDate}
              </GiveText>
            </GiveDateTooltip>
          </Stack>
        );
      },
    };
  }, []);
 
  const sponsorStatusCol = useMemo(() => {
    const sortKey = isMerchantStatusRefactorEnabled
      ? "stateSortOrder"
      : "sponsorStatusSortOrder";
    return {
      key: `${sortKey},-accID`,
      sortable: isWideView,
      title: "Sponsor Status",
      columnType: "status-round" as const,
      columnWidth: "170px",
      renderColumn: (row: MerchantData) => {
        const sponsorStatusDisplayName =
          isMerchantStatusRefactorEnabled && row?.stateName
            ? StatusMap[row.stateName]
            : row?.sponsorStatusDisplayName || "";
        const status =
          !!sponsorStatusDisplayName &&
          !["Approved", "Declined", "Pending"].includes(
            sponsorStatusDisplayName,
          )
            ? "Ready for Review"
            : sponsorStatusDisplayName;
        return <StatusTag statusDisplayName={status} disableTooltip />;
      },
    };
  }, [isWideView]);
 
  const ageEscalationCol = useMemo(() => {
    return {
      key: "lastEscalationAt,-accID",
      sortable: true,
      title: "Age of Escalation",
      columnWidth: 1.1,
      renderColumn: (row: MerchantData) => {
        const parsedDate = row?.lastEscalationAt
          ? formatInTimezone(row?.lastEscalationAt, "MMM dd, yyyy")
          : "-";
 
        return (
          <GiveDateTooltip>
            <GiveText
              data-testid={`give-risk-${row?.lastEscalationAt}`}
              variant="bodyS"
            >
              {parsedDate}
            </GiveText>
          </GiveDateTooltip>
        );
      },
    };
  }, []);
 
  const riskLevelCol = useMemo(() => {
    return {
      key: "riskSortOrder,-accID",
      sortable: true,
      title: "Risk Level",
      columnWidth: "144px",
      columnType: isRiskTabSelected
        ? ("risk-level" as const)
        : ("status-round" as const),
      renderColumn: (row: MerchantData) => {
        Iif (row?.statusDisplayName === "Suspended" || row?.riskSortOrder === 5)
          return (
            <StatusTag
              statusDisplayName={TAG_STATUSES_ENUM.SUSPENDED}
              updatedBy={`${row?.suspendedByFirstName} ${row?.suspendedByLastName}`}
              updatedAt={Number(row?.suspendedAt || 0)}
            />
          );
        return <RiskLevelTag row={row} isRiskTabSelected={isRiskTabSelected} />;
      },
    };
  }, [isRiskTabSelected]);
 
  const assignedColumnBasedOnPlatform = isEnterprisePortal ? [] : [assignedCol];
 
  const extraCols = useMemo(() => {
    const kybColumns = {
      desktop: [
        ...(isWideView ? [taxIdCol] : []),
        signupDateCol,
        ...assignedColumnBasedOnPlatform,
        kybProgressCol,
      ],
      mobile: [kybProgressCol],
    };
    const pendingColumns = {
      desktop: [
        ...(isMobileView ? [] : [taxIdCol]),
        signupDateCol,
        pendingProgressCol,
      ],
      mobile: [pendingProgressCol],
    };
 
    switch (selectedTab) {
      case "underwriting":
        return isNewApprovalFlowEnabled
          ? {
              desktop: [
                taxIdCol,
                KYBCol,
                ...assignedColumnBasedOnPlatform,
                underwritingProgressCol,
              ],
              mobile: [underwritingProgressCol],
            }
          : {
              desktop: [
                signupDateCol,
                assignmentCol,
                categoryRiskCol,
                underwritingScoreCol,
                statusCol,
              ],
              mobile: [underwritingScoreCol],
            };
      case "risk":
        return {
          desktop: [
            ...assignedColumnBasedOnPlatform,
            ageEscalationCol,
            riskLevelCol,
          ],
          mobile: [riskLevelCol],
        };
 
      case "approved":
        return {
          desktop: [
            ...(isLargeView ? [mccCol] : []),
            taxIdCol,
            approvalDateCol,
            processedCol,
            ...(!hideApprovedRiskColumn ? [riskLevelCol] : []),
          ],
          mobile: [...(!hideApprovedRiskColumn ? [riskLevelCol] : [])],
        };
 
      case "invited":
        return {
          desktop: [emailCol, inviteCol, statusCol],
          mobile: [statusCol],
        };
 
      case "sponsor":
        return isNewApprovalFlowEnabled
          ? {
              desktop: [assignedCol, mccCol, readyCol],
              mobile: [],
            }
          : {
              desktop: [
                mccCol,
                categoryRiskCol,
                actionRequiredCol,
                sponsorStatusCol,
              ],
              mobile: [sponsorStatusCol],
            };
 
      case "onboarding":
        return {
          desktop: [assignmentCol, signupDateCol, onboardingScoreCol],
          mobile: [onboardingScoreCol],
        };
 
      case "pending":
        return pendingColumns;
 
      case "kyb":
        return kybColumns;
 
      default:
        return isNewApprovalFlowEnabled
          ? {
              desktop: [
                taxIdCol,
                signupDateCol,
                ...assignedColumnBasedOnPlatform,
                processedCol,
                statusCol,
              ],
              mobile: [statusCol],
            }
          : {
              desktop: [
                signupDateCol,
                categoryRiskCol,
                processedCol,
                statusCol,
              ],
              mobile: [statusCol],
            };
    }
  }, [
    selectedTab,
    signupDateCol,
    assignmentCol,
    categoryRiskCol,
    underwritingScoreCol,
    processedCol,
    statusCol,
    taxIdCol,
    KYBCol,
    assignedCol,
    underwritingProgressCol,
    kybProgressCol,
    readyCol,
    pendingProgressCol,
    isWideView,
    isLargeView,
  ]);
 
  const columns: TableColumnType[] = useMemo(
    () => [
      {
        key: "name",
        sortable: isWideView,
        columnType: "merchant-first",
        title: "Merchant",
        columnWidth: !isWideView ? 3 : merchantColWidth,
        hasCheckbox: isSponsorTabSelected,
        renderColumn: (row: MerchantData) => {
          return (
            <MainInfo
              row={row}
              disabled={!isUpdateAssigneeAllowed}
              isSponsorTable={isSponsorTabSelected}
              removeFromWatchlistHandler={() =>
                removeMerchantFromWatchlist(row?.accID, row?.name)
              }
              addToWatchlistHandler={() => addToWatchlistHandler(row)}
              isAcquirerPortal={isAcquirerPortal}
              badgeTooltipProps={
                row?.hasUnrepliedActivityMessages && row?.lastMessageActivity
                  ? {
                      color: "light",
                      title: (
                        <BadgeTooltipContent data={row?.lastMessageActivity} />
                      ),
                    }
                  : undefined
              }
              // warning hidden on April 29, 2026, to avoid production issues and misleading information
              isWarnedMerchant={false}
              thumbnailType="merchant"
              showCanManageMoney={selectedTab === "risk" && isWideView}
            />
          );
        },
      },
      ...(isWideView ? extraCols.desktop : extraCols.mobile),
    ],
    [
      isWideView,
      isAcquirerPortal,
      extraCols,
      selectedTab,
      isUpdateAssigneeAllowed,
    ],
  );
 
  const { defaultSort, defaultDescending } = getDefaultSort(
    selectedTab,
    isMerchantStatusRefactorEnabled,
    isNewApprovalFlowEnabled,
  );
 
  return {
    removeMerchantFromWatchlist,
    addToWatchlistHandler,
    isWarnedMerchant,
    columns,
    defaultSort,
    defaultDescending,
    selectedTabStatus: {
      isAllTabSelected: selectedTab === "all",
      isInvitedTabSelected,
      isRiskTabSelected,
      isOnboardingSelected,
      isSponsorTabSelected,
      isKYBSelected,
      isUnderwritingTabSelected: selectedTab === "underwriting",
      isApprovedTabSelected,
      isPendingTabSelected,
    },
  };
};