import { onPatch } from '@jbrowse/mobx-state-tree';
import { autorun } from 'mobx';
import createModel from "./createModel/index.js";
export default function createViewState(opts) {
    const { assembly, tracks, internetAccounts, configuration, aggregateTextSearchAdapters, plugins, makeWorkerInstance, onChange, } = opts;
    const { model, pluginManager } = createModel(plugins || [], makeWorkerInstance);
    let { defaultSession } = opts;
    if (!defaultSession) {
        defaultSession = {
            name: 'this session',
            view: {
                id: 'circularView',
                type: 'CircularView',
            },
        };
    }
    const stateSnapshot = {
        config: {
            configuration,
            assembly,
            tracks,
            internetAccounts,
            aggregateTextSearchAdapters,
            defaultSession,
        },
        session: defaultSession,
    };
    const stateTree = model.create(stateSnapshot, { pluginManager });
    for (const account of stateTree.config.internetAccounts) {
        const internetAccountType = pluginManager.getInternetAccountType(account.type);
        if (!internetAccountType) {
            throw new Error(`unknown internet account type ${account.type}`);
        }
        stateTree.addInternetAccount({
            type: account.type,
            configuration: account,
        });
    }
    pluginManager.setRootModel(stateTree);
    pluginManager.configure();
    autorun(reaction => {
        const { session, assemblyManager } = stateTree;
        if (!session.view.initialized) {
            return;
        }
        const regions = assemblyManager.get(assembly?.name)?.regions;
        if (!regions) {
            return;
        }
        session.view.setDisplayedRegions(regions);
        reaction.dispose();
    });
    if (onChange) {
        onPatch(stateTree, onChange);
    }
    return stateTree;
}
