import type { ConfigDisplayFields } from "../types/templateLibrary" import { TEMPLATE_TYPES, CATEGORY_GRADIENT_MAP, DEFAULT_GRADIENT, MATERIAL_TYPE_LABELS, } from "../constants/templateLibrary" import type { TemplateSegment } from "@/api/templates" /** 获取类型对应颜色 */ export const getTypeColor = (type: string): string => { const found = TEMPLATE_TYPES.find((t) => t.type === type) return found?.color ?? "#6366f1" } /** 根据 category 生成占位渐变色 */ export const gradientForCategory = (category: string): string => { return CATEGORY_GRADIENT_MAP[category] ?? DEFAULT_GRADIENT } /** 格式化时长 */ export const formatDuration = (seconds: number | undefined | null): string => { if (!seconds || seconds <= 0) return "0秒" const totalSec = Math.round(seconds) const m = Math.floor(totalSec / 60) const s = totalSec % 60 if (m === 0) return `${s}秒` return `${m}分${s > 0 ? `${s}秒` : ""}` } /** 格式化配置对象为可读文本 */ export const formatConfig = (config?: object): string => { if (!config || Object.keys(config).length === 0) return "默认" const c = config as ConfigDisplayFields const parts: string[] = [] if (c.font_size) parts.push(`字号: ${c.font_size}`) if (c.font_family) parts.push(`字体: ${c.font_family}`) if (c.color) parts.push(`颜色: ${c.color}`) if (c.position) parts.push(`位置: ${c.position}`) if (c.volume !== undefined) parts.push(`音量: ${c.volume}%`) if (c.name) parts.push(String(c.name)) return parts.length > 0 ? parts.join(" / ") : JSON.stringify(config) } /** 获取素材类型标签文本 */ export const getMaterialTypeLabel = (materialType: string | null | undefined): string => { if (!materialType) return "不限" return MATERIAL_TYPE_LABELS[materialType] ?? materialType } /** 计算片段总时长(取每个片段 min/max 的平均值) */ export const calcTotalSegmentDuration = (segments: TemplateSegment[]): number => { return segments.reduce((sum, s) => sum + (s.duration_min + s.duration_max) / 2, 0) }