import catalog from '../data/templates.json'; import type {IndustryDef, IndustryType, LayoutId, TemplateDef} from '../types'; export type TemplateId = keyof typeof catalog.templates; export const LAYOUT_IDS: readonly LayoutId[] = ['basic', 'paper', 'round', 'cinema', 'bigtype', 'boutique', 'graphic', 'coral', 'minimal', 'pine']; export const TEMPLATES = catalog.templates as Record; export const INDUSTRIES = catalog.industries as Record; export function isTemplateId(id: string): id is TemplateId { return Object.hasOwn(TEMPLATES, id); } export function templateOf(id: string): TemplateDef { if (!isTemplateId(id)) throw new Error(`등록되지 않은 템플릿: ${id}`); return TEMPLATES[id]; } export function industryTemplates(industry: IndustryType): TemplateId[] { return INDUSTRIES[industry].templates as TemplateId[]; } export function resolveTemplateId(industry: IndustryType, stored: string | null | undefined): TemplateId { const id = stored?.trim() || INDUSTRIES[industry].defaultTemplate; if (!INDUSTRIES[industry].templates.includes(id)) { throw new Error(`${industry} 업종에서 쓸 수 없는 템플릿: ${id}`); } return id as TemplateId; } for (const [id, template] of Object.entries(TEMPLATES)) { if (!LAYOUT_IDS.includes(template.layout)) { throw new Error(`템플릿 ${id}의 레이아웃이 등록되지 않았다: ${template.layout}`); } } for (const [industry, def] of Object.entries(INDUSTRIES)) { for (const id of [def.defaultTemplate, ...def.templates]) { if (!isTemplateId(id)) throw new Error(`${industry} 업종이 없는 템플릿을 가리킨다: ${id}`); } if (!def.templates.includes(def.defaultTemplate)) { throw new Error(`${industry} 업종의 기본 템플릿이 허용 목록에 없다: ${def.defaultTemplate}`); } }