| 1 | import { normalizeFontSelection, type DesignContract } from '@shared/generation' |
| 2 | import { parseJsonObject } from '../ipc/utils' |
| 3 | |
| 4 | const DEFAULT_TITLE_FONT = 'Inter' |
| 5 | const DEFAULT_BODY_FONT = 'Inter' |
| 6 | |
| 7 | const readText = (record: Record<string, unknown>, key: keyof Omit<DesignContract, 'palette'>): string => { |
| 8 | const text = String(record[key] ?? '').replace(/\s+/g, ' ').trim() |
| 9 | return text.length > 220 ? `${text.slice(0, 220).trimEnd()}...` : text |
| 10 | } |
| 11 | |
| 12 | function normalizePalette(raw: unknown): string[] { |
| 13 | if (!Array.isArray(raw)) return ['#ffffff', '#111827', '#64748b'] |
| 14 | const colors = raw.map((item) => String(item ?? '').trim()).filter(Boolean).slice(0, 6) |
| 15 | return colors.length >= 3 ? colors : ['#ffffff', '#111827', '#64748b'] |
| 16 | } |
| 17 | |
| 18 | export function resolveTemplateDesignContract( |
| 19 | value: unknown, |
| 20 | metadataValue?: unknown |
| 21 | ): DesignContract { |
| 22 | const record = parseJsonObject(value) |
| 23 | const metadata = parseJsonObject(metadataValue) |
| 24 | const fontSelection = normalizeFontSelection(metadata.fontSelection) |
| 25 | const selectedTitleFont = |
| 26 | fontSelection.mode === 'pair' ? String(fontSelection.title.family || '').trim() : '' |
| 27 | const selectedBodyFont = |
| 28 | fontSelection.mode === 'pair' ? String(fontSelection.body.family || '').trim() : '' |
| 29 | |
| 30 | return { |
| 31 | theme: readText(record, 'theme'), |
| 32 | background: readText(record, 'background'), |
| 33 | palette: normalizePalette(record.palette), |
| 34 | titleStyle: readText(record, 'titleStyle'), |
| 35 | layoutMotif: readText(record, 'layoutMotif'), |
| 36 | chartStyle: readText(record, 'chartStyle'), |
| 37 | shapeLanguage: readText(record, 'shapeLanguage'), |
| 38 | titleFont: readText(record, 'titleFont') || selectedTitleFont || DEFAULT_TITLE_FONT, |
| 39 | bodyFont: readText(record, 'bodyFont') || selectedBodyFont || DEFAULT_BODY_FONT |
| 40 | } |
| 41 | } |
| 42 |