import { createAppSettingsCapsule, getErrorMonitorCompat, getExperimentsCompat, getHostContainerCompat, getWixPatternsWindowExperiments, showToastCompat, } from '@wix/bex-core';
import { usePageLocation } from '@wix/bex-core/react';
import { Trans, useTranslation } from '@wix/bex-utils/@wix/wix-i18n-config';
import React, { useState } from 'react';
import { CairoProvider as CairoProviderBase } from '../providers';
import { getInstanceJwtData } from './getInstanceJwtData';
import { bi, errorHandler, experiments } from '@wix/essentials';
export function WixPatternsDashboardBaseProvider({ children, dashboard, environment: { locale, pageLocation }, essentials, ...rest }) {
    const [value] = useState(() => {
        const jwtData = getInstanceJwtData(window.location.search);
        const user = {
            id: jwtData?.uid,
            permissions: ['DATA_EXTENSION_SCHEMA.WRITE'],
        };
        const environment = {
            language: locale || 'en',
            artifactId: essentials.environment.artifactId,
            componentId: pageLocation.pageId,
            providerType: 'bm',
            msid: jwtData?.metaSiteId,
        };
        // ready doesn't exist in the dashboard-sdk because it's always true. adding it for backwards compatibility
        const experimentsWithReady = {
            ...experiments,
            ready: async () => true,
        };
        let biLogger;
        try {
            biLogger = bi.getLogger();
        }
        catch (error) {
            console.error(error);
        }
        return {
            useExperiments: () => ({
                ready: true,
                experiments: getExperimentsCompat({
                    experiments: experimentsWithReady,
                }),
            }),
            experiments: getExperimentsCompat({ experiments: experimentsWithReady }),
            Trans,
            useTranslation,
            showToast: showToastCompat(dashboard.showToast),
            usePageLocation,
            navigateTo: ({ pageId = pageLocation.pageId, relativeUrl, displayMode, history, }) => dashboard.navigate({ pageId, relativeUrl }, { displayMode, history }),
            onSidePanelOpen: undefined,
            onBeforeUnload: dashboard.onBeforeUnload,
            onLayerStateChange: dashboard.onLayerStateChange,
            openMediaManager: dashboard.openMediaManager,
            closeSidePanel: undefined,
            getHostContainer: getHostContainerCompat(dashboard.getDashboardMenuPlugins, dashboard.getDashboardPlugins),
            environment,
            user,
            environmentBI: {
                componentId: environment.componentId,
            },
            httpClient: essentials.httpClient,
            errorMonitor: getErrorMonitorCompat(essentials.asPartial),
            errorHandler,
            biLogger,
            createEssentials: (options) => {
                return essentials.createChildEssentials({
                    ...options,
                    experiments: options.experiments &&
                        'scopes' in options.experiments &&
                        options.experiments?.scopes[0] === 'cairo'
                        ? {
                            bag: getWixPatternsWindowExperiments() ?? {},
                        }
                        : options.experiments,
                });
            },
            createComponentSettings: createAppSettingsCapsule,
        };
    });
    return (React.createElement(CairoProviderBase, { ...rest, value: value }, children));
}
//# sourceMappingURL=WixPatternsDashboardBaseProvider.js.map