import { DependenciesReceipt } from "../config/setupConfig" import { SetInterfaces } from "../interfaces/setupInterfaces" import { SDKManifest } from "../manifest/ManifestTypes" import { Effects, ExpectedExports, ExposeUiPaths, ExposeUiPathsAll, } from "../types" import { Migrations } from "./migrations/setupMigrations" import { SetupExports } from "./setupExports" import { Install } from "./setupInstall" import { Uninstall } from "./setupUninstall" export function setupInit( migrations: Migrations, install: Install, uninstall: Uninstall, setInterfaces: SetInterfaces, setupExports: SetupExports, setDependencies: (options: { effects: Effects input: any }) => Promise, ): { init: ExpectedExports.init uninit: ExpectedExports.uninit } { return { init: async (opts) => { await migrations.init(opts) await install.init(opts) await setInterfaces({ ...opts, input: null, }) const { services, ui } = await setupExports(opts) await opts.effects.exposeForDependents({ paths: services }) await opts.effects.exposeUi(forExpose(ui)) await setDependencies({ effects: opts.effects, input: null }) }, uninit: async (opts) => { await migrations.uninit(opts) await uninstall.uninit(opts) }, } } function forExpose(ui: { [key: string]: ExposeUiPaths }) { return Object.fromEntries( Object.entries(ui).map(([key, value]) => [key, forExpose_(value)]), ) } function forExpose_(ui: ExposeUiPaths): ExposeUiPathsAll { if (ui.type === ("object" as const)) { return { type: "object" as const, value: Object.fromEntries( Object.entries(ui.value).map(([key, value]) => [ key, forExpose_(value), ]), ), description: ui.description ?? null, } } return { description: null, copyable: null, qr: null, ...ui, } }