import { dayjs } from "element-plus";
import { isSuccess, recordType, roleType, type BoolEnumType, type EnumType } from "../enums/system-enum";

export const formatDate = (row: any, column: any, cellValue: any) => {
  if (!cellValue) return "";
  return dayjs(cellValue).format("YYYY-MM-DD hh:mm:ss");
};

export const formatIsSuccess = (row: any, column: any, cellValue: any) => {
  if (cellValue !== undefined && typeof cellValue === "boolean")
    return getBoolEnumTypeDescription(isSuccess, cellValue);
  return "";
};

export const formatRecordType = (row: any, column: any, cellValue: any) => {
  if (cellValue !== undefined && typeof cellValue === "number") return getEnumTypeDescription(recordType, cellValue);
  return "-";
};

export const formatRoleType = (row: any, column: any, cellValue: any) => {
  if (cellValue !== undefined && typeof cellValue === "number") return getEnumTypeDescription(roleType, cellValue);
  return "-";
};

const getEnumTypeDescription = (enumTypes: EnumType[], code: number) => {
  return enumTypes.find(item => item.code === code)?.description ?? "-";
};

const getBoolEnumTypeDescription = (boolEnumTypes: BoolEnumType[], boolCode: boolean) => {
  return boolEnumTypes.find(item => item.code === boolCode)?.description ?? "-";
};
