import { CHARACTERS, isCharacterCode, type CharacterCode } from './characters' export type Intensity = 0 | 1 | 2 | 3 export type Config = { character: CharacterCode | null intensity: Intensity address: string | null } export const DEFAULT_INTENSITY: Intensity = 2 function toIntensity(value: unknown): Intensity { const n = typeof value === 'number' ? value : Number(value) return n === 0 || n === 1 || n === 2 || n === 3 ? n : DEFAULT_INTENSITY } // 缺失或無效的值一律退回預設,不讓設定錯誤擋住載入。 export function resolveConfig(options: Readonly>): Config { const address = typeof options.address === 'string' ? options.address.trim() : '' return { character: isCharacterCode(options.defaultCharacter) ? options.defaultCharacter : null, intensity: toIntensity(options.intensity), address: address === '' ? null : address, } } // 使用者的覆寫優先,否則用角色在原作中對使用者的稱呼。 export function addressFor(config: Config, character: CharacterCode): string { return config.address ?? CHARACTERS[character].address }