import { buildCreateEssentials } from './buildCreateEssentials';
import { Trans, useAppLoaded, useExperiments, useTranslation, } from '@wix/yoshi-flow-bm';
import { closeSidePanel, getHostContainer, getModule, navigateTo, onNavigation, onSidePanelOpen, showToast, } from '@wix/business-manager-api';
import { dashboard } from '@wix/dashboard';
import { createAppSettingsCapsule, getWixPatternsWindowExperiments, } from '@wix/bex-core';
export function buildBMCairoEssentials(flowAPI, { overrides, } = {}) {
    const { moduleParams, moduleInfo, componentInfo, errorMonitor, httpClient, experiments, } = flowAPI;
    const { accountLanguage: language } = moduleParams;
    const { moduleId } = moduleInfo;
    const componentId = componentInfo?.componentId;
    const bmModule = getModule(moduleId);
    const _essentials = bmModule._essentials;
    const createChildEssentials = buildCreateEssentials(_essentials);
    return {
        createEssentials: (options) => {
            return createChildEssentials({
                ...options,
                experiments: options.experiments &&
                    'scopes' in options.experiments &&
                    options.experiments?.scopes[0] === 'cairo'
                    ? {
                        bag: getWixPatternsWindowExperiments() ?? {},
                    }
                    : options.experiments,
            });
        },
        createComponentSettings: createAppSettingsCapsule,
        errorMonitor: errorMonitor ?? { captureException: () => { } },
        useTranslation,
        Trans,
        useExperiments,
        experiments,
        onNavigation,
        onSidePanelOpen,
        onLayerStateChange: dashboard.onLayerStateChange,
        getHostContainer: getHostContainer,
        closeSidePanel,
        httpClient,
        queryCache: _essentials.UNSAFE_cairoQueryCache,
        navigateTo: (params) => {
            const { pageId, appState, referrer, relativeUrl, displayMode, history } = params;
            // Layered navigation (e.g. opening a collection as an overlay) is only
            // expressible through the dashboard SDK — BM's own navigateTo always
            // navigates in place. Route those through dashboard.navigate and keep the
            // BM navigation for regular in-place navigations.
            if (displayMode || history) {
                dashboard.navigate({ pageId, relativeUrl }, { displayMode, history });
                return;
            }
            navigateTo({
                pageComponentId: pageId,
                contextData: { appState: appState || relativeUrl, referrer },
            });
        },
        useAppLoaded: () => {
            useAppLoaded({ auto: true });
        },
        ...overrides,
        showToast: overrides?.showToast ??
            (({ type, timeout, priority, action, ...toastConfig }) => showToast({
                ...toastConfig,
                biName: toastConfig.biName ?? '',
                type: type,
                priority: priority,
                action: action,
                ...(timeout && { timeout: timeout }),
            })),
        user: {
            permissions: moduleParams.userPermissions,
            ...overrides?.user,
        },
        environment: {
            language,
            componentId,
            artifactId: moduleId,
            ...overrides?.environment,
            providerType: 'bm',
        },
        environmentBI: {
            artifactId: bmModule.artifactId,
            componentId,
        },
        openMediaManager: overrides?.openMediaManager ?? dashboard.openMediaManager,
    };
}
//# sourceMappingURL=buildBMCairoEssentials.js.map