import { AutoConfigure, DeepPartial, Effects, ExpectedExports } from "../types" import { Utils, utils } from "../util/utils" import { deepEqual } from "../util/deepEqual" import { deepMerge } from "../util/deepMerge" import { Config } from "../config/builder/config" export class AutoConfig< Store, Vault, Input extends Record, RemoteConfig extends Record, > { constructor( readonly autoconfig: (options: { effects: Effects localConfig: Input remoteConfig: RemoteConfig utils: Utils }) => Promise>, ) {} async check( options: Parameters[0], ): ReturnType { const origConfig = JSON.parse(JSON.stringify(options.localConfig)) const newOptions = { ...options, utils: utils(options.effects), localConfig: options.localConfig as Input, remoteConfig: options.remoteConfig as RemoteConfig, } if ( !deepEqual( origConfig, deepMerge({}, options.localConfig, await this.autoconfig(newOptions)), ) ) throw new Error(`Check failed`) } async autoConfigure( options: Parameters[0], ): ReturnType { const newOptions = { ...options, utils: utils(options.effects), localConfig: options.localConfig as Input, remoteConfig: options.remoteConfig as any, } return deepMerge( {}, options.remoteConfig, await this.autoconfig(newOptions), ) } }