import type { AdvancedMaterial, AdvancedMaterialImport, AdvancedMaterialValidationAttempt, } from "../../../../shared/types/advanced-material" export type MaterialLibraryView = "standard" | "advanced" | "advanced-import" export interface AdvancedMaterialStatusMeta { label: string detail: string tone: "idle" | "good" | "warn" | "bad" | "active" busy: boolean } const MATERIAL_VIEWS = new Set(["standard", "advanced", "advanced-import"]) export function parseMaterialLibraryView(value: string | null | undefined): MaterialLibraryView { return value && MATERIAL_VIEWS.has(value as MaterialLibraryView) ? (value as MaterialLibraryView) : "standard" } export function advancedImportStatusMeta(status: string): AdvancedMaterialStatusMeta { switch (normalize(status)) { case "pending_recognition": case "recognition_queued": case "style_source_recognition_queued": return { label: "等待识别", detail: "源图已保存,等待视觉模型提取特征。", tone: "idle", busy: true } case "recognizing": case "recognizing_style_sources": return { label: "正在识别", detail: "正在提取关键词、提示词和视觉特征。", tone: "active", busy: true } case "style_source_review_required": return { label: "待处理样本", detail: "部分样本识别未完成;可单独重试失败项或排除后继续。", tone: "warn", busy: false } case "style_consensus_queued": case "style_consensus_running": return { label: "正在交叉归纳", detail: "正在对全部有效样本和逐图报告进行共性与差异分析。", tone: "active", busy: true } case "style_consensus_failed": return { label: "交叉归纳失败", detail: "逐图结果仍已保留,可重新发起交叉归纳。", tone: "bad", busy: false } case "pending_validation": case "ready_for_validation": return { label: "等待验证", detail: "识别完成,可以生成第一张还原度验证图。", tone: "warn", busy: false } case "validating": case "validation_queued": return { label: "正在验证", detail: "验证图已入队,完成后可对照源图审核。", tone: "active", busy: true } case "validation_partial_failed": return { label: "部分验证失败", detail: "已完成的验证图会保留,只需单独重试失败场景。", tone: "warn", busy: false } case "pending_review": case "awaiting_review": return { label: "等待审核", detail: "请比较源图与验证图,决定封存或继续调整。", tone: "warn", busy: false } case "accepted": return { label: "已封存", detail: "已作为高优先级进阶素材入库。", tone: "good", busy: false } case "rejected": case "adjustment_required": return { label: "待调整", detail: "上一轮未通过,可补充描述重新验证。", tone: "warn", busy: false } case "failed": case "validation_failed": case "recognition_failed": return { label: "处理失败", detail: "可以查看失败原因并重试当前阶段。", tone: "bad", busy: false } case "cancelled": return { label: "已取消", detail: "当前导入任务已取消。", tone: "idle", busy: false } case "archived": return { label: "已归档", detail: "当前导入记录已归档。", tone: "idle", busy: false } default: return { label: status || "未知状态", detail: "后端返回了尚未识别的状态。", tone: "idle", busy: false } } } export function advancedAttemptStatusMeta(status: string): AdvancedMaterialStatusMeta { switch (normalize(status)) { case "queued": return { label: "已入队", detail: "验证任务正在等待执行。", tone: "idle", busy: true } case "running": return { label: "生成中", detail: "正在生成本轮还原度验证图。", tone: "active", busy: true } case "succeeded": case "completed": case "awaiting_review": return { label: "待审核", detail: "验证图已生成,请判断还原度。", tone: "warn", busy: false } case "accepted": return { label: "已采纳", detail: "本轮验证图已经封存入库。", tone: "good", busy: false } case "rejected": return { label: "未采纳", detail: "本轮已保留,可基于反馈继续调整。", tone: "warn", busy: false } case "failed": return { label: "生成失败", detail: "检查错误后可再次发起验证。", tone: "bad", busy: false } case "cancelled": return { label: "已取消", detail: "本轮验证任务已取消。", tone: "idle", busy: false } default: return { label: status || "未知状态", detail: "后端返回了尚未识别的状态。", tone: "idle", busy: false } } } export function sortAdvancedMaterials(items: AdvancedMaterial[]): AdvancedMaterial[] { return [...items].sort((left, right) => { const activeDelta = Number(normalize(right.status) === "active") - Number(normalize(left.status) === "active") if (activeDelta) return activeDelta const priorityDelta = Number(right.priority || 0) - Number(left.priority || 0) if (priorityDelta) return priorityDelta return timestamp(right.accepted_at || right.updated_at) - timestamp(left.accepted_at || left.updated_at) }) } export function sortAdvancedImports(items: AdvancedMaterialImport[]): AdvancedMaterialImport[] { return [...items].sort((left, right) => timestamp(right.updated_at) - timestamp(left.updated_at)) } export function sortValidationAttempts(items: AdvancedMaterialValidationAttempt[]): AdvancedMaterialValidationAttempt[] { return [...items].sort((left, right) => { const attemptDelta = Number(right.attempt_number || 0) - Number(left.attempt_number || 0) return attemptDelta || timestamp(right.created_at) - timestamp(left.created_at) }) } export function advancedMaterialErrorMessage(value: unknown): string { if (!value || typeof value !== "object") return "" const record = value as Record const message = String(record.message || record.detail || record.reason || "").trim() if (message) return message const code = String(record.code || record.error_code || "").trim() return code ? `错误代码:${code}` : "" } export function firstRecord(payload: unknown, keys: string[]): T | null { if (!payload || typeof payload !== "object") return null const record = payload as Record for (const key of keys) { const value = record[key] if (value && typeof value === "object" && !Array.isArray(value)) return value as T } return null } export function listItems(payload: unknown): T[] { if (Array.isArray(payload)) return payload as T[] if (!payload || typeof payload !== "object") return [] const record = payload as Record if (Array.isArray(record.items)) return record.items as T[] if (record.data && typeof record.data === "object") return listItems(record.data) return [] } function normalize(status: string) { return String(status || "").trim().toLowerCase() } function timestamp(value: string | null | undefined) { const parsed = Date.parse(String(value || "")) return Number.isFinite(parsed) ? parsed : 0 }