import type { WorkspaceSummary } from "@qjclaw/shared-types"

type Plugin = WorkspaceSummary["plugins"][number]
type Tone = "positive" | "warning" | "info"

const pluginDisplayMap: Record<string, { name: string; description: string }> = {
  "spreadsheet-tools": { name: "\u8868\u683c\u5de5\u5177", description: "\u8bfb\u53d6\u548c\u5904\u7406 Excel\u3001CSV \u7b49\u5e38\u89c1\u8868\u683c\u6587\u4ef6\u3002" },
  "sheet-plugin": { name: "\u8868\u683c\u5de5\u5177", description: "\u8bfb\u53d6\u548c\u5904\u7406 Excel\u3001CSV \u7b49\u5e38\u89c1\u8868\u683c\u6587\u4ef6\u3002" },
  "document-tools": { name: "\u6587\u6863\u5de5\u5177", description: "\u5904\u7406 txt\u3001md\u3001docx\u3001pdf \u7b49\u5e38\u89c1\u6587\u6863\u3002" },
  "doc-plugin": { name: "\u6587\u6863\u5de5\u5177", description: "\u5904\u7406 txt\u3001md\u3001docx\u3001pdf \u7b49\u5e38\u89c1\u6587\u6863\u3002" },
  "web-tools": { name: "\u7f51\u9875\u4fe1\u606f\u63d0\u53d6", description: "\u6293\u53d6\u7f51\u9875\u5185\u5bb9\u5e76\u8fdb\u884c\u63d0\u53d6\u3001\u6e05\u6d17\u548c\u6c47\u603b\u3002" },
  "web-plugin": { name: "\u7f51\u9875\u4fe1\u606f\u63d0\u53d6", description: "\u6293\u53d6\u7f51\u9875\u5185\u5bb9\u5e76\u8fdb\u884c\u63d0\u53d6\u3001\u6e05\u6d17\u548c\u6c47\u603b\u3002" },
  "file-tools": { name: "\u6587\u4ef6\u5de5\u5177", description: "\u6267\u884c\u6587\u4ef6\u590d\u5236\u3001\u79fb\u52a8\u548c\u5f52\u6863\u7b49\u64cd\u4f5c\u3002" },
  "runtime-diagnostics": { name: "\u8fd0\u884c\u65f6\u8bca\u65ad", description: "\u67e5\u770b\u8fd0\u884c\u65f6\u4fe1\u606f\u3001\u65e5\u5fd7\u548c\u72b6\u6001\u3002" },
  "browser-automation": { name: "\u7f51\u9875\u81ea\u52a8\u5316", description: "\u81ea\u52a8\u6267\u884c\u7f51\u9875\u6d4f\u89c8\u3001\u70b9\u51fb\u548c\u8868\u5355\u64cd\u4f5c\u3002" },
  "browser-plugin": { name: "\u7f51\u9875\u81ea\u52a8\u5316", description: "\u81ea\u52a8\u6267\u884c\u7f51\u9875\u6d4f\u89c8\u3001\u70b9\u51fb\u548c\u8868\u5355\u64cd\u4f5c\u3002" },
  "ocr-tools": { name: "OCR \u8bc6\u522b", description: "\u8bc6\u522b\u626b\u63cf\u4ef6\u548c\u56fe\u7247\u6587\u5b57\u5e76\u63d0\u53d6\u7ed3\u6784\u3002" }
}

export function getPluginTone(status: Plugin["status"]): Tone {
  switch (status) {
    case "included":
      return "positive"
    case "extension":
      return "info"
    default:
      return "warning"
  }
}

export function getPluginStatusLabel(status: Plugin["status"]): string {
  switch (status) {
    case "included":
      return "\u5df2\u63a5\u5165"
    case "extension":
      return "\u6269\u5c55"
    default:
      return "\u4e0d\u53ef\u7528"
  }
}

export function getPluginStatusDescription(status: Plugin["status"]): string {
  switch (status) {
    case "included":
      return "\u5df2\u7ecf\u968f\u5f53\u524d\u5de5\u4f5c\u533a\u63d0\u4f9b\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u3002"
    case "extension":
      return "\u8ba1\u5212\u4f5c\u4e3a\u6269\u5c55\u80fd\u529b\u63a5\u5165\u3002"
    default:
      return "\u5f53\u524d\u8fd0\u884c\u73af\u5883\u6682\u672a\u63d0\u4f9b\u8be5\u80fd\u529b\u3002"
  }
}

export function groupPluginsByStatus(plugins: WorkspaceSummary["plugins"] | undefined) {
  const source = plugins ?? []
  return {
    included: source.filter((plugin) => plugin.status === "included"),
    extension: source.filter((plugin) => plugin.status === "extension"),
    unavailable: source.filter((plugin) => plugin.status === "unavailable")
  }
}

export function getPluginCopy(plugin: Plugin) {
  return pluginDisplayMap[plugin.id] ?? { name: plugin.name, description: plugin.description }
}
