"use client";

import type { ColumnDef, FilterFn } from "@tanstack/react-table";
import type { SettingsSelectOption } from "@/components/settings/types";

import { AssetTypeCell } from "./asset-type-cell";
import type { NormalizeAssetTypeRow } from "./schema";

const includesStringFilter: FilterFn<NormalizeAssetTypeRow> = (
  row,
  columnId,
  value,
) => {
  if (!value || value === "all") return true;
  return String(row.getValue(columnId) ?? "")
    .toLowerCase()
    .includes(String(value).toLowerCase());
};

export function createNormalizeAssetTypeColumns(
  assetTypeOptions: SettingsSelectOption[],
  onRowSaved?: () => void,
): ColumnDef<NormalizeAssetTypeRow>[] {
  return [
    {
      accessorKey: "bankId",
      header: "Bank",
      filterFn: includesStringFilter,
    },
    {
      accessorKey: "tag",
      header: "Tag",
      filterFn: includesStringFilter,
    },
    {
      accessorKey: "assetClass",
      header: "Asset class",
      filterFn: includesStringFilter,
    },
    {
      id: "assetType",
      header: "Asset type",
      enableColumnFilter: false,
      cell: ({ row }) => (
        <AssetTypeCell
          row={row.original}
          onSaved={onRowSaved}
          initialOptions={assetTypeOptions}
        />
      ),
    },
  ];
}
