import { arkKind, DynamicBase, flatMorph, type anyOrNever } from "@ark/util" import type { BaseRoot } from "./roots/root.ts" import type { BaseScope, InternalResolution, InternalResolutions } from "./scope.ts" import { hasArkKind } from "./shared/utils.ts" export type PreparsedNodeResolution = { [arkKind]: "generic" | "module" } export class RootModule extends DynamicBase { // ensure `[arkKind]` is non-enumerable so it doesn't get spread on import/export get [arkKind](): "module" { return "module" } } export interface InternalModule< exports extends InternalResolutions = InternalResolutions > extends RootModule { root?: BaseRoot } export const bindModule = ( module: InternalModule, $: BaseScope ): InternalModule => new RootModule( flatMorph(module, (alias, value) => [ alias, hasArkKind(value, "module") ? bindModule(value, $) : $.bindRef(value) ]) ) as never type exportSchemaScope<$> = { [k in keyof $]: instantiateRoot<$[k]> } export type instantiateRoot = t extends InternalResolution ? [t] extends [anyOrNever] ? BaseRoot : t : BaseRoot export const SchemaModule: new <$ = {}>( types: exportSchemaScope<$> ) => SchemaModule<$> = RootModule export interface SchemaModule<$ = {}> extends RootModule> {}