import type { PathBinding as ContractPathBinding, PathBindingCreate } from "@umt/contracts"; import type { PathBinding as DomainPathBinding, NewBinding } from "@umt/domain"; // 路径绑定:domain {entityId, scope, season, templateVersion:number, active} // ↔ contracts / path_binding {mediaEntityId, bindingKind, seasonNumber, templateVersion:string|null, status} export type { DomainPathBinding }; /** 模板版本:库里是文本(可能是 "3" 或历史导入的任意串),domain 用整数;无法解析按 0(未知)处理 */ export function parseTemplateVersion(v: string | null): number { return v !== null && /^\d+$/.test(v) ? Number(v) : 0; } /** path_binding 行 → domain;只有 status=active 的绑定参与目标目录决策 */ export function toDomainPathBinding(b: ContractPathBinding): DomainPathBinding { return { id: b.id, entityId: b.mediaEntityId, scope: b.bindingKind, ...(b.seasonNumber !== null ? { season: b.seasonNumber } : {}), libraryRootId: b.libraryRootId, relativePath: b.relativePath, templateVersion: parseTemplateVersion(b.templateVersion), active: b.status === "active", }; } /** domain 决策里的新绑定 → path_binding 新建输入(status=active) */ export function toPathBindingCreate( nb: NewBinding, target: { mediaEntityId: string; contentUnitId?: string | null; note?: string | null }, ): PathBindingCreate { return { mediaEntityId: target.mediaEntityId, ...(target.contentUnitId !== undefined ? { contentUnitId: target.contentUnitId } : {}), bindingKind: nb.scope, ...(nb.scope === "season" ? { seasonNumber: nb.season ?? null } : {}), libraryRootId: nb.libraryRootId, relativePath: nb.relativePath, templateVersion: String(nb.templateVersion), status: "active", ...(target.note !== undefined ? { note: target.note } : {}), }; }