import type { DictData } from "../../../api/crm";

export const DICT = {
  COMMON_STATUS: "common_status",
  MES_MD_ITEM_OR_PRODUCT: "mes_md_item_or_product",
};

export const RULE = {
  MD_ITEM_CODE: "MD_ITEM_CODE",
  MD_WORKSHOP_CODE: "MD_WORKSHOP_CODE",
  MD_WORKSTATION_CODE: "MD_WORKSTATION_CODE",
};

export function dateText(value?: string | Date) {
  if (!value) return "--";
  return String(value).replace("T", " ").slice(0, 16);
}

export function boolText(value?: boolean) {
  return value ? "是" : "否";
}

export function dictLabel(dicts: DictData[], type: string, value?: string | number | boolean, fallback = "--") {
  const hit = dicts.find(item => item.dictType === type && String(item.value) === String(value));
  return hit?.label || fallback;
}

export function dictOptions(dicts: DictData[], type: string) {
  return dicts.filter(item => item.dictType === type);
}

export function statusText(status?: number) {
  return Number(status) === 0 ? "开启" : Number(status) === 1 ? "关闭" : "--";
}

export function statusClass(status?: number) {
  return Number(status) === 0 ? "tag-enabled" : "tag-disabled";
}

export function num(value?: string | number) {
  if (value === "" || value === undefined || value === null) return undefined;
  const numberValue = Number(value);
  return Number.isNaN(numberValue) ? undefined : numberValue;
}
