114 lines
3.8 KiB
TypeScript
114 lines
3.8 KiB
TypeScript
import type { ComponentType } from "react";
|
|
import { AlertCircle, CheckCircle2, Clock, XCircle } from "lucide-react";
|
|
import type { SelectOption } from "@/components/formInput";
|
|
import type { StatusBadgeVariant } from "@/components/ui/statusBadge";
|
|
import { buildEnumOptions, getEnumLabel, isEnumValue, type EnumConfig, type TranslateFn } from "@/constants/enumLabels";
|
|
|
|
// ─── Gateway status ───────────────────────────────────────────────────────────
|
|
|
|
export const GATEWAY_STATUS_VALUES = ["pending", "paid", "unpaid", "failed", "success", "completed"] as const;
|
|
|
|
export type GatewayStatusValue = (typeof GATEWAY_STATUS_VALUES)[number];
|
|
|
|
export const GATEWAY_STATUS_LABELS: Record<GatewayStatusValue, string> = {
|
|
pending: "Pending",
|
|
paid: "Paid",
|
|
unpaid: "Unpaid",
|
|
failed: "Failed",
|
|
success: "Success",
|
|
completed: "Completed",
|
|
};
|
|
|
|
const GATEWAY_STATUS_ENUM: EnumConfig<GatewayStatusValue> = {
|
|
values: GATEWAY_STATUS_VALUES,
|
|
labels: GATEWAY_STATUS_LABELS,
|
|
prefix: "onlinePayments.gatewayStatus",
|
|
};
|
|
|
|
export const GATEWAY_STATUS_BADGE_VARIANTS: Record<GatewayStatusValue, StatusBadgeVariant> = {
|
|
pending: "warning",
|
|
paid: "success",
|
|
unpaid: "default",
|
|
failed: "error",
|
|
success: "success",
|
|
completed: "info",
|
|
};
|
|
|
|
export const GATEWAY_STATUS_ICONS: Record<GatewayStatusValue, ComponentType> = {
|
|
pending: AlertCircle,
|
|
paid: CheckCircle2,
|
|
unpaid: Clock,
|
|
failed: XCircle,
|
|
success: CheckCircle2,
|
|
completed: CheckCircle2,
|
|
};
|
|
|
|
// ─── Paid flag ────────────────────────────────────────────────────────────────
|
|
|
|
export const PAID_VALUES = ["paid", "unpaid"] as const;
|
|
|
|
export type PaidValue = (typeof PAID_VALUES)[number];
|
|
|
|
export const PAID_LABELS: Record<PaidValue, string> = {
|
|
paid: "Paid",
|
|
unpaid: "Unpaid",
|
|
};
|
|
|
|
const PAID_ENUM: EnumConfig<PaidValue> = {
|
|
values: PAID_VALUES,
|
|
labels: PAID_LABELS,
|
|
prefix: "onlinePayments.paid",
|
|
};
|
|
|
|
export const PAID_BADGE_VARIANTS: Record<PaidValue, StatusBadgeVariant> = {
|
|
paid: "success",
|
|
unpaid: "error",
|
|
};
|
|
|
|
export const PAID_ICONS: Record<PaidValue, ComponentType> = {
|
|
paid: CheckCircle2,
|
|
unpaid: XCircle,
|
|
};
|
|
|
|
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
|
|
function withPrefix<T extends string>(config: EnumConfig<T>, prefix?: string): EnumConfig<T> {
|
|
return prefix ? { ...config, prefix } : config;
|
|
}
|
|
|
|
export function isGatewayStatusValue(status: string): status is GatewayStatusValue {
|
|
return isEnumValue(GATEWAY_STATUS_VALUES, status);
|
|
}
|
|
|
|
export function getGatewayStatusLabel(status: string, t?: TranslateFn, prefix?: string): string {
|
|
return getEnumLabel(withPrefix(GATEWAY_STATUS_ENUM, prefix), status, t);
|
|
}
|
|
|
|
export function getGatewayStatusOptions(t?: TranslateFn, prefix?: string): SelectOption[] {
|
|
return buildEnumOptions(withPrefix(GATEWAY_STATUS_ENUM, prefix), t);
|
|
}
|
|
|
|
export const PAYMENT_HISTORY_STATUS_VALUES = ["failed", "pending", "success", "completed"] as const;
|
|
|
|
export function getPaymentHistoryStatusOptions(t?: TranslateFn, prefix?: string): SelectOption[] {
|
|
return buildEnumOptions(
|
|
withPrefix(
|
|
{
|
|
values: PAYMENT_HISTORY_STATUS_VALUES,
|
|
labels: GATEWAY_STATUS_LABELS,
|
|
prefix: "onlinePayments.gatewayStatus",
|
|
},
|
|
prefix,
|
|
),
|
|
t,
|
|
);
|
|
}
|
|
|
|
export function toPaidValue(paid: boolean): PaidValue {
|
|
return paid ? "paid" : "unpaid";
|
|
}
|
|
|
|
export function getPaidLabel(paid: boolean, t?: TranslateFn, prefix?: string): string {
|
|
return getEnumLabel(withPrefix(PAID_ENUM, prefix), toPaidValue(paid), t);
|
|
}
|