import {ChannelId} from "../types/DiscordTypes"; import {GroupConfigurationModel} from "../Models/GroupConfigurationModel"; import {config} from "dotenv"; import {transform} from "esbuild"; import {Nullable} from "../types/Nullable"; import {ArrayUtils} from "../Utilities/ArrayUtils"; export enum TransformerType { Locale, Channel, } type GroupConfigurationTransformer = { path: string[]; type: TransformerType, } export type GroupConfigurationResult = ChannelId | Intl.Locale export class GroupConfigurationTransformers { static TRANSFORMERS: GroupConfigurationTransformer[] = [ { path: ['channels', 'newPlaydates'], type: TransformerType.Channel, }, { path: ['channels', 'playdateReminders'], type: TransformerType.Channel, }, { path: ['locale'], type: TransformerType.Locale, } ]; public getValue(configValue: GroupConfigurationModel): GroupConfigurationResult { const transformerType = this.getTransformerType(configValue.key); if (transformerType === undefined || transformerType === null) { throw new Error(`Can't find transformer for ${configValue.key}`); } switch (transformerType) { case TransformerType.Locale: return new Intl.Locale(configValue.value) case TransformerType.Channel: return configValue.value; } } public getTransformerType(configKey: string): Nullable { const path = configKey.split('.'); return GroupConfigurationTransformers.TRANSFORMERS.find( transformer => { return ArrayUtils.arraysEqual(transformer.path, path); } )?.type; } }