import {ChannelId} from "../types/DiscordTypes"; import {Nullable} from "../types/Nullable"; import {ArrayUtils} from "../Utilities/ArrayUtils"; import {ConfigurationModel} from "../Database/Models/ConfigurationModel"; export enum TransformerType { Channel, PermissionBoolean, String, Paragraph, Timezone } type ConfigurationTransformerItem = { path: string[]; type: TransformerType, } export type TransformerResults = ChannelId | boolean | string | null export class ConfigurationTransformer { constructor( private readonly transformers: ConfigurationTransformerItem[] ) { } public getValue(configValue: ConfigurationModel): TransformerResults { 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.Channel: return configValue.value; case TransformerType.PermissionBoolean: return configValue.value === '1'; case TransformerType.Timezone: case TransformerType.Paragraph: case TransformerType.String: return configValue.value; } return null; } public getTransformerType(configKey: string): Nullable { const path = configKey.split('.'); return this.transformers.find( transformer => { return ArrayUtils.arraysEqual(transformer.path, path); } )?.type; } }