"use strict";

exports.__esModule = true;
exports.buildBMCairoEssentials = buildBMCairoEssentials;
var _buildCreateEssentials = require("./buildCreateEssentials");
var _yoshiFlowBm = require("@wix/yoshi-flow-bm");
var _businessManagerApi = require("@wix/business-manager-api");
var _dashboard = require("@wix/dashboard");
var _bexCore = require("@wix/bex-core");
function buildBMCairoEssentials(flowAPI, {
  overrides
} = {}) {
  const {
    moduleParams,
    moduleInfo,
    componentInfo,
    errorMonitor,
    httpClient,
    experiments
  } = flowAPI;
  const {
    accountLanguage: language
  } = moduleParams;
  const {
    moduleId
  } = moduleInfo;
  const componentId = componentInfo == null ? void 0 : componentInfo.componentId;
  const bmModule = (0, _businessManagerApi.getModule)(moduleId);
  const _essentials = bmModule._essentials;
  const createChildEssentials = (0, _buildCreateEssentials.buildCreateEssentials)(_essentials);
  return {
    createEssentials: options => {
      var _options$experiments;
      return createChildEssentials({
        ...options,
        experiments: options.experiments && 'scopes' in options.experiments && ((_options$experiments = options.experiments) == null ? void 0 : _options$experiments.scopes[0]) === 'cairo' ? {
          bag: (0, _bexCore.getWixPatternsWindowExperiments)() ?? {}
        } : options.experiments
      });
    },
    createComponentSettings: _bexCore.createAppSettingsCapsule,
    errorMonitor: errorMonitor ?? {
      captureException: () => {}
    },
    useTranslation: _yoshiFlowBm.useTranslation,
    Trans: _yoshiFlowBm.Trans,
    useExperiments: _yoshiFlowBm.useExperiments,
    experiments,
    onNavigation: _businessManagerApi.onNavigation,
    onSidePanelOpen: _businessManagerApi.onSidePanelOpen,
    onLayerStateChange: _dashboard.dashboard.onLayerStateChange,
    getHostContainer: _businessManagerApi.getHostContainer,
    closeSidePanel: _businessManagerApi.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.dashboard.navigate({
          pageId,
          relativeUrl
        }, {
          displayMode,
          history
        });
        return;
      }
      (0, _businessManagerApi.navigateTo)({
        pageComponentId: pageId,
        contextData: {
          appState: appState || relativeUrl,
          referrer
        }
      });
    },
    useAppLoaded: () => {
      (0, _yoshiFlowBm.useAppLoaded)({
        auto: true
      });
    },
    ...overrides,
    showToast: (overrides == null ? void 0 : overrides.showToast) ?? (({
      type,
      timeout,
      priority,
      action,
      ...toastConfig
    }) => (0, _businessManagerApi.showToast)({
      ...toastConfig,
      biName: toastConfig.biName ?? '',
      type: type,
      priority: priority,
      action: action,
      ...(timeout && {
        timeout: timeout
      })
    })),
    user: {
      permissions: moduleParams.userPermissions,
      ...(overrides == null ? void 0 : overrides.user)
    },
    environment: {
      language,
      componentId,
      artifactId: moduleId,
      ...(overrides == null ? void 0 : overrides.environment),
      providerType: 'bm'
    },
    environmentBI: {
      artifactId: bmModule.artifactId,
      componentId
    },
    openMediaManager: (overrides == null ? void 0 : overrides.openMediaManager) ?? _dashboard.dashboard.openMediaManager
  };
}
//# sourceMappingURL=buildBMCairoEssentials.js.map