import { BackendRegistry, createSessionAPI } from "../Session";
import { createServiceManager } from "../ServiceManager";
import { createArticleState } from "../ArticleState";
import { createArticleQuery } from "./ArticleQuery";
import { createArticleManager } from "./ArticleManager";
import { createNotificationCenter } from "../NotificationCenter";
import { createConnectionManager } from "../ConnectionManager";
import { createArticleRegistry } from "../ArticleRegistry";
import { createProjectManager } from "../ProjectManager";
import { createOperationResultHandler } from "../OperationResultHandler";
import { createHooks } from "../Hooks";
import { createSourceRegistry } from "../SourceRegistry";
import { createLocalizationManager } from "../LocalizationManager";
import { NoopApplicationState } from "../NoopApplicationState";
import { createScheduler } from "../Scheduler";
export const createSingleArticleSession = ({ language, currency, locale, applicationState = NoopApplicationState, } = {}) => {
    const hooks = createHooks();
    const connectionManager = createConnectionManager();
    const notificationCenter = createNotificationCenter();
    const localizationManager = createLocalizationManager({
        language,
        currency,
        locale,
    }, notificationCenter);
    const serviceManager = createServiceManager(BackendRegistry);
    const articleState = createArticleState();
    const articleQuery = createArticleQuery(articleState);
    const scheduler = createScheduler(articleQuery);
    const articleRegistry = createArticleRegistry(serviceManager, articleState);
    const operationResultHandler = createOperationResultHandler(articleRegistry, articleQuery, notificationCenter, hooks);
    const articleManager = createArticleManager(serviceManager, articleState, articleQuery, operationResultHandler, applicationState);
    const sourceRegistry = createSourceRegistry(serviceManager, articleQuery, scheduler);
    const projectManager = createProjectManager(serviceManager, articleQuery, articleManager, operationResultHandler, hooks, sourceRegistry, connectionManager, localizationManager);
    const services = {
        scheduler,
        notificationCenter,
        backendManager: BackendRegistry,
        serviceManager,
        articleState,
        articleQuery,
        articleManager,
        connectionManager,
        articleRegistry,
        projectManager,
        operationResultHandler,
        hooks,
        sourceRegistry,
        localizationManager,
        applicationState,
    };
    const withMainArticleComId = (func) => (...args) => {
        const mainComId = articleQuery.getMainArticleComId();
        if (mainComId === undefined) {
            return undefined;
        }
        return func(mainComId, ...args);
    };
    const getProductDataManager = withMainArticleComId(serviceManager.getProductDataManagerForComId);
    const getCatalogManager = withMainArticleComId(serviceManager.getCatalogManagerForComId);
    const getOperator = withMainArticleComId(serviceManager.getOperatorForComId);
    const getConfigurationService = withMainArticleComId(serviceManager.getConfigurationServiceForComId);
    return {
        ...createSessionAPI(services),
        getMainArticleComId: articleQuery.getMainArticleComId,
        getMainArticle: articleQuery.getMainArticle,
        changeMainArticleProperties: articleManager.changeMainArticleProperties,
        changeMainArticleProperty: articleManager.changeMainArticleProperty,
        deleteMainArticle: articleManager.deleteMainArticle,
        getCatalogManager,
        getProductDataManager,
        getConfigurationService,
        getOperator,
    };
};
