import { type EnumConfig, getEnumKeyByValue } from "@/types/enums" import type { JSX } from "react" import type { TranslationType } from "@/lib/translations" /** * ENUM VALUE CONVERSION FUNCTIONS */ /** * Converts a numeric enum value to its Prisma enum string representation * This is used for display purposes, not for database storage * @param config The enum configuration * @param value The numeric value * @returns The string key for Prisma */ export function toPrismaEnum(config: EnumConfig, value: number): string | undefined { return getEnumKeyByValue(config, value) } /** * Converts a Prisma enum string to its numeric value * This is used when working with string enum keys from external sources * @param config The enum configuration * @param enumKey The Prisma enum key (e.g., "PENDING") * @returns The numeric value */ export function fromPrismaEnum(config: EnumConfig, enumKey: string): number | undefined { return config[enumKey as T]?.value } /** * ENUM DISPLAY FUNCTIONS */ /** * Gets the display text for an enum value with translation support * @param config The enum configuration * @param value The numeric value * @param translations Optional translations object * @returns The display text (translated if available) */ export function getEnumDisplay( config: EnumConfig, value: number, translations?: TranslationType, ): string { const key = getEnumKeyByValue(config, value) if (!key) return translations?.["enums.unknown"] || "Unknown" const enumItem = config[key] // If translations are provided and the display is a translation key, use translation if (translations && enumItem.display.startsWith("enums.")) { return translations[enumItem.display] || enumItem.display } // Otherwise return the display value as is (for backward compatibility) return enumItem.display } /** * Gets the color class for an enum value * @param config The enum configuration * @param value The numeric value * @returns The color class string */ export function getEnumColorByValue(config: EnumConfig, value: number): string { const key = getEnumKeyByValue(config, value) return key && config[key].color ? config[key].color : "" } /** * Gets the icon component for an enum value * @param config The enum configuration * @param value The numeric value * @returns The Lucide icon component */ export function getEnumIcon(config: EnumConfig, value: number) { const key = getEnumKeyByValue(config, value) return key ? config[key].icon : undefined } /** * UI COMPONENT FUNCTIONS */ /** * Creates a badge element with the appropriate styling for an enum value * @param config The enum configuration * @param value The numeric value * @param translations Optional translations object * @returns JSX element with appropriate styling */ export function getEnumBadge( config: EnumConfig, value: number, translations?: TranslationType, ): JSX.Element | null { const key = getEnumKeyByValue(config, value) if (!key) return null const { display, icon: IconComponent, color } = config[key] // Get translated display text const displayText = translations && display.startsWith("enums.") ? translations[display] || display : display return ( {IconComponent && } {displayText} ) }