import { asFactory, buildContainer } from '@hypersphere/dity'
import { App, Plugin, Vault } from 'obsidian'
import { db } from '../database/module'
import { editor } from '../editor/module'
import { sync } from '../sync/module'
import { SQLSealSettings } from '../settings/SQLSealSettingsTab'
import { Init } from './init'
import { settingsModule } from '../settings/module'
import { syntaxHighlight } from '../syntaxHighlight/module'
import { contextMenu } from '../contextMenu/module'
import { debugModule } from '../debug/module'
import { apiModule } from '../api/module'
import { globalTables } from '../globalTables/module'
import { explorer } from '../explorer/module'

const obsidian = buildContainer(c => c
    .externals<{
        app: App,
        plugin: Plugin,
        vault: Vault
    }>()
)


export const mainModule = buildContainer(c => c
    .submodules({
        obsidian,
        db,
        editor,
        sync,
        settings: settingsModule,
        syntaxHighlight,
        contextMenu,
        debug: debugModule,
        api: apiModule,
        globalTables,
        explorer
    })
    .register({
        init: asFactory(Init)
    })
    .externals<{ settings: SQLSealSettings }>()
    .resolve({
        'db.app': 'obsidian.app',
    })
    .resolve({
        'editor.app': 'obsidian.app', // THESE SHOULD BE INVALID NOW
        'editor.db': 'db.db',
        'editor.plugin': 'obsidian.plugin',
        'editor.sync': 'sync.syncBus',
        'editor.cellParser': 'syntaxHighlight.cellParser',
        'editor.settings': 'settings.settings'
    })
    .resolve({
        'sync.app': 'obsidian.app',
        'sync.db': 'db.db',
        'sync.plugin': 'obsidian.plugin',
        'sync.vault': 'obsidian.vault',
    })
    .resolve({
        'settings.app': 'obsidian.app',
        'settings.plugin': 'obsidian.plugin',
        'settings.cellParser': 'syntaxHighlight.cellParser',
        'settings.viewPluginGenerator': 'syntaxHighlight.viewPluginGenerator'
    })
    .resolve({
        'syntaxHighlight.app': 'obsidian.app',
        'syntaxHighlight.db': 'db.db',
        'syntaxHighlight.plugin': 'obsidian.plugin',
        'syntaxHighlight.rendererRegistry': 'editor.rendererRegistry'
    })
    .resolve({
        'contextMenu.app': 'obsidian.app',
        'contextMenu.plugin': 'obsidian.plugin'
    })
    .resolve({
        'debug.plugin': 'obsidian.plugin'
    })
    .resolve({
        'api.plugin': 'obsidian.plugin',
        'api.cellParser': 'syntaxHighlight.cellParser',
        'api.db': 'db.db',
        'api.rendererRegistry': 'editor.rendererRegistry'
    })
    .resolve({
        'globalTables.plugin': 'obsidian.plugin',
        'globalTables.app': 'obsidian.app',
        'globalTables.sync': 'sync.syncBus'
    })
    .resolve({
        'explorer.app': 'obsidian.app',
        'explorer.cellParser': 'syntaxHighlight.cellParser',
        'explorer.db': 'db.db',
        'explorer.settings': 'settings.settings',
        'explorer.plugin': 'obsidian.plugin',
        'explorer.rendererRegistry': 'editor.rendererRegistry',
        'explorer.sync': 'sync.syncBus',
        'explorer.viewPluginGenerator': 'syntaxHighlight.viewPluginGenerator'
    })
)

export type MainModule = typeof mainModule
