const EMPTY_STATUS_VALUES = new Set(["", "-"]);

export const STATUS_LABELS = {
  ACTIVE: "启用",
  INACTIVE: "停用",
  AVAILABLE: "可用",
  CREATED: "已创建",
  RELEASED: "已下达",
  ORDERED: "已下单",
  IN_PROGRESS: "进行中",
  PARTIAL: "部分完成",
  PENDING: "待处理",
  PENDING_QC: "待质检",
  OPEN: "待处理",
  LOCKED: "锁定",
  PAUSED: "暂停",
  READY_TO_RECEIPT: "待成品入库",
  HAS_RETURN: "存在退货",
  PASSED: "已通过",
  PASS: "通过",
  COMPLETED: "已完成",
  SETTLED: "已结单",
  DONE: "已完成",
  CONFIRMED: "已确认",
  POSTED: "已过账",
  RECEIVED: "已收货",
  REJECTED: "已拒收",
  REJECT: "拒收",
  SCRAP: "报废",
  DEPLETED: "已耗尽",
  CLOSED: "已关闭",
  REWORK: "返工",
  SUPERSEDED: "已替代",
  OVERRIDDEN: "已覆盖",
  FAILED: "失败",
  ERROR: "异常",
  EXPIRED: "已过期",
  APPROVED: "已审核",
  AUDIT_FAILED: "审核失败"
};

export const STATUS_TONE_GROUPS = {
  success: [
    "ACTIVE",
    "AVAILABLE",
    "PASSED",
    "PASS",
    "COMPLETED",
    "SETTLED",
    "DONE",
    "CONFIRMED",
    "POSTED",
    "RECEIVED",
    "APPROVED",
    "启用",
    "正常",
    "成功",
    "可用",
    "已放行",
    "放行",
    "通过",
    "已通过",
    "已完成",
    "已完工",
    "已结单",
    "结单",
    "已确认",
    "已过账",
    "已收货",
    "已入库",
    "已审核",
    "审核通过",
    "已同步",
    "同步完成",
    "已匹配",
    "匹配成功",
    "在生产"
  ],
  primary: [
    "CREATED",
    "RELEASED",
    "ORDERED",
    "IN_PROGRESS",
    "PARTIAL",
    "已创建",
    "已下达",
    "已下单",
    "进行中",
    "部分完成",
    "小程序清单"
  ],
  warning: [
    "PENDING",
    "PENDING_QC",
    "OPEN",
    "LOCKED",
    "PAUSED",
    "REWORK",
    "READY_TO_RECEIPT",
    "待处理",
    "待质检",
    "待放行",
    "未开始",
    "锁定",
    "锁库",
    "锁单",
    "暂停",
    "返工",
    "返工中",
    "待成品入库",
    "待审核",
    "待同步",
    "未同步",
    "未匹配",
    "待匹配",
    "未关联"
  ],
  orange: [
    "HAS_RETURN",
    "存在退货",
    "快到期",
    "即将到期",
    "临期"
  ],
  danger: [
    "INACTIVE",
    "REJECTED",
    "REJECT",
    "SCRAP",
    "DEPLETED",
    "CLOSED",
    "SUPERSEDED",
    "OVERRIDDEN",
    "FAILED",
    "ERROR",
    "EXPIRED",
    "AUDIT_FAILED",
    "停用",
    "异常",
    "失败",
    "报废",
    "已报废",
    "拒收",
    "已拒收",
    "已耗尽",
    "已关闭",
    "已替代",
    "已覆盖",
    "已过期",
    "已驳回",
    "驳回",
    "审核失败",
    "同步失败",
    "匹配失败"
  ],
  neutral: ["多状态", "未知", "未创建账号", "未命名", "无"],
  info: ["INFO", "提示", "信息"]
};

export function normalizeStatusValue(value) {
  return String(value ?? "")
    .replace(/[：:]/g, "")
    .replace(/\s+/g, "")
    .trim();
}

function normalizeStatusKey(value) {
  return normalizeStatusValue(value).toUpperCase();
}

export function getStatusLabel(value) {
  const normalizedValue = normalizeStatusValue(value);
  if (EMPTY_STATUS_VALUES.has(normalizedValue)) {
    return normalizedValue || "-";
  }
  return STATUS_LABELS[normalizeStatusKey(normalizedValue)] || normalizedValue;
}

export function getStatusTone(value) {
  const normalizedValue = normalizeStatusValue(value);
  if (EMPTY_STATUS_VALUES.has(normalizedValue)) {
    return "";
  }

  const normalizedKey = normalizeStatusKey(normalizedValue);
  for (const [tone, values] of Object.entries(STATUS_TONE_GROUPS)) {
    if (values.some((item) => normalizeStatusValue(item) === normalizedValue || normalizeStatusKey(item) === normalizedKey)) {
      return tone;
    }
  }
  return "info";
}
