const EXACT_ACTIONS = new Map([
  ["刷新", { label: "刷新数据", icon: "refresh", tone: "neutral", kind: "tool" }],
  ["查看", { label: "查看详情", icon: "view", tone: "info", kind: "table" }],
  ["详情", { label: "查看详情", icon: "view", tone: "info", kind: "table" }],
  ["编辑", { label: "编辑资料", icon: "edit", tone: "info", kind: "table" }],
  ["删除", { label: "删除记录", icon: "delete", tone: "danger", kind: "danger" }],
  ["移除", { label: "移除记录", icon: "delete", tone: "danger", kind: "danger" }],
  ["增加一行", { label: "添加明细行", icon: "rowAdd", tone: "info", kind: "secondary" }],
  ["新增工序", { label: "添加工序", icon: "rowAdd", tone: "info", kind: "secondary" }],
  ["添加联系人", { label: "添加联系人", icon: "plus", tone: "info", kind: "secondary" }],
  ["保存", { label: "保存信息", icon: "save", tone: "primary", kind: "primary" }],
  ["取消", { label: "取消操作", icon: "close", tone: "neutral", kind: "secondary" }],
  ["关闭", { label: "关闭窗口", icon: "close", tone: "neutral", kind: "secondary" }],
  ["放大", { label: "放大视图", icon: "zoomIn", tone: "neutral", kind: "tool" }],
  ["缩小", { label: "缩小视图", icon: "zoomOut", tone: "neutral", kind: "tool" }],
  ["一屏展示", { label: "一屏展示", icon: "fitScreen", tone: "neutral", kind: "tool" }],
  ["授权", { label: "人员授权", icon: "authorize", tone: "primary", kind: "primary" }],
  ["放行", { label: "质检放行", icon: "check", tone: "success", kind: "success" }],
  ["处置", { label: "处理退货", icon: "edit", tone: "warning", kind: "warning" }],
  ["处置记录", { label: "查看处置记录", icon: "ledger", tone: "info", kind: "table" }]
]);

const PARTIAL_ACTIONS = [
  { match: "保存授权", icon: "authorize", tone: "primary", kind: "primary" },
  { match: "保存单据", icon: "save", tone: "primary", kind: "primary" },
  { match: "保存", icon: "save", tone: "primary", kind: "primary" },
  { match: "确认", icon: "check", tone: "primary", kind: "primary" },
  { match: "登记", icon: "save", tone: "primary", kind: "primary" },
  { match: "提交", icon: "save", tone: "primary", kind: "primary" },
  { match: "新建", icon: "plus", tone: "primary", kind: "primary" },
  { match: "新增", icon: "plus", tone: "primary", kind: "primary", labelPrefix: "新建" },
  { match: "导入", icon: "import", tone: "primary", kind: "primary" },
  { match: "导出", icon: "export", tone: "success", kind: "tool" },
  { match: "下载", icon: "download", tone: "success", kind: "tool" },
  { match: "上传", icon: "upload", tone: "primary", kind: "primary" },
  { match: "打印", icon: "print", tone: "neutral", kind: "tool" },
  { match: "刷新", icon: "refresh", tone: "neutral", kind: "tool", explicitLabel: "刷新数据" },
  { match: "重置", icon: "refresh", tone: "neutral", kind: "tool" },
  { match: "搜索", icon: "search", tone: "info", kind: "tool" },
  { match: "查看", icon: "view", tone: "info", kind: "table" },
  { match: "详情", icon: "view", tone: "info", kind: "table" },
  { match: "编辑", icon: "edit", tone: "info", kind: "table" },
  { match: "质检放行", icon: "check", tone: "success", kind: "success" },
  { match: "合格", icon: "check", tone: "success", kind: "success" },
  { match: "放行", icon: "check", tone: "success", kind: "success" },
  { match: "驳回", icon: "reject", tone: "danger", kind: "danger" },
  { match: "不合格", icon: "reject", tone: "danger", kind: "danger" },
  { match: "拒绝", icon: "reject", tone: "danger", kind: "danger" },
  { match: "删除", icon: "delete", tone: "danger", kind: "danger" },
  { match: "移除", icon: "delete", tone: "danger", kind: "danger" },
  { match: "结单", icon: "check", tone: "warning", kind: "warning" },
  { match: "添加", icon: "rowAdd", tone: "info", kind: "secondary" },
  { match: "增加", icon: "rowAdd", tone: "info", kind: "secondary" },
  { match: "下一步", icon: "arrowRight", tone: "primary", kind: "primary" },
  { match: "上一步", icon: "arrowLeft", tone: "neutral", kind: "secondary" }
];

function normalizeLabel(label) {
  return String(label || "").replace(/\s+/g, " ").trim();
}

function hasDangerClass(className) {
  return /\b(danger|danger-ghost|error|reject)\b/i.test(String(className || ""));
}

function normalizeExplicitLabel(label, rule) {
  if (rule.explicitLabel) {
    return rule.explicitLabel;
  }

  if (rule.labelPrefix && label.startsWith(rule.match)) {
    return `${rule.labelPrefix}${label.slice(rule.match.length)}`;
  }

  return label;
}

export function resolveButtonSemantics(label, className = "") {
  const normalizedLabel = normalizeLabel(label);
  if (!normalizedLabel) {
    return null;
  }

  if (hasDangerClass(className) && ["删除", "移除"].includes(normalizedLabel)) {
    return EXACT_ACTIONS.get(normalizedLabel);
  }

  const exact = EXACT_ACTIONS.get(normalizedLabel);
  if (exact) {
    return exact;
  }

  const partial = PARTIAL_ACTIONS.find((rule) => normalizedLabel.includes(rule.match));
  if (partial) {
    return {
      label: normalizeExplicitLabel(normalizedLabel, partial),
      icon: partial.icon,
      tone: hasDangerClass(className) ? "danger" : partial.tone,
      kind: hasDangerClass(className) ? "danger" : partial.kind
    };
  }

  if (hasDangerClass(className)) {
    return {
      label: normalizedLabel,
      icon: "warning",
      tone: "danger",
      kind: "danger"
    };
  }

  return null;
}
