import { EnumDef } from './dsl.js'; // Enum driver: renders an EnumDef into a standalone TypeScript enum file. // Shape matches the generated-enum product consumed by the TypeBox driver (Type.Enum): // // export enum UserStatus { // ACTIVE = 'ACTIVE', // DISABLED = 'DISABLED', // } // // export const USER_STATUS_LABEL: Record = { // [UserStatus.ACTIVE]: '启用', // [UserStatus.DISABLED]: '禁用', // }; function renderString(s: string): string { return `'${s.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`; } function renderValue(value: string | number): string { return typeof value === 'number' ? String(value) : renderString(value); } /** 'UserStatus' → 'USER_STATUS_LABEL' (matches the label map naming convention) */ function labelName(jsName: string): string { return `${jsName.replace(/([a-z0-9])([A-Z])/g, '$1_$2').toUpperCase()}_LABEL`; } export function renderEnum(def: EnumDef): string { const name = def.jsName; const members = def.values.map((v) => ` ${v.symbol} = ${renderValue(v.value)},`); const labels = def.values.map((v) => ` [${name}.${v.symbol}]: ${renderString(v.label)},`); return [ `export enum ${name} {`, ...members, '}', '', `export const ${labelName(name)}: Record<${name}, string> = {`, ...labels, '};', '', ].join('\n'); }