import { HttpTypes } from "@medusajs/types" import { ColumnDef } from "@tanstack/react-table" import { TFunction } from "i18next" import { FieldPath, FieldValues } from "react-hook-form" import { IncludesTaxTooltip } from "../../common/tax-badge/tax-badge" import { DataGridCurrencyCell } from "../components/data-grid-currency-cell" import { DataGridReadonlyCell } from "../components/data-grid-readonly-cell" import { FieldContext } from "../types" import { createDataGridHelper } from "./create-data-grid-column-helper" type CreateDataGridPriceColumnsProps< TData, TFieldValues extends FieldValues > = { currencies?: string[] regions?: HttpTypes.AdminRegion[] pricePreferences?: HttpTypes.AdminPricePreference[] isReadyOnly?: (context: FieldContext) => boolean getFieldName: ( context: FieldContext, value: string ) => FieldPath | null t: TFunction } export const createDataGridPriceColumns = < TData, TFieldValues extends FieldValues >({ currencies, regions, pricePreferences, isReadyOnly, getFieldName, t, }: CreateDataGridPriceColumnsProps): ColumnDef< TData, unknown >[] => { const columnHelper = createDataGridHelper() return [ ...(currencies?.map((currency) => { const preference = pricePreferences?.find( (p) => p.attribute === "currency_code" && p.value === currency ) const translatedCurrencyName = t("fields.priceTemplate", { regionOrCurrency: currency.toUpperCase(), }) return columnHelper.column({ id: `currency_prices.${currency}`, name: t("fields.priceTemplate", { regionOrCurrency: currency.toUpperCase(), }), field: (context) => { const isReadyOnlyValue = isReadyOnly?.(context) if (isReadyOnlyValue) { return null } return getFieldName(context, currency) }, type: "number", header: () => (
{translatedCurrencyName}
), cell: (context) => { if (isReadyOnly?.(context)) { return } return }, }) }) ?? []), ...(regions?.map((region) => { const preference = pricePreferences?.find( (p) => p.attribute === "region_id" && p.value === region.id ) const translatedRegionName = t("fields.priceTemplate", { regionOrCurrency: region.name, }) return columnHelper.column({ id: `region_prices.${region.id}`, name: t("fields.priceTemplate", { regionOrCurrency: region.name, }), field: (context) => { const isReadyOnlyValue = isReadyOnly?.(context) if (isReadyOnlyValue) { return null } return getFieldName(context, region.id) }, type: "number", header: () => (
{translatedRegionName}
), cell: (context) => { if (isReadyOnly?.(context)) { return } const currency = currencies?.find((c) => c === region.currency_code) if (!currency) { return null } return ( ) }, }) }) ?? []), ] }