var _meta, _GoogleAnalytics;
import NextHead from 'next/head';
import * as React from 'react';
import { CodeCopyProvider } from "../CodeCopy/index.js";
import DemoContext from "../DemoContext/index.js";
import { DocsProvider } from "../DocsProvider/index.js";
import PageContext from "../PageContext/index.js";
import { ThemeProvider } from "../ThemeContext/index.js";
import { CodeStylingProvider } from "../codeStyling/index.js";
import { CodeVariantProvider } from "../codeVariant/index.js";
import { AnalyticsProvider } from "./AnalyticsProvider.js";
import GoogleAnalytics from "./GoogleAnalytics.js";
import DocsStyledEngineProvider from "./StyledEngineProvider.js";
import createEmotionCache from "./createEmotionCache.js";
import { loadDependencies } from "./loadDependencies.js";
import { registerServiceWorker } from "./serviceWorker.js";

// Client-side cache, shared for the whole session of the user in the browser.
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const clientSideEmotionCache = createEmotionCache();
function DocsApp(props) {
  const {
    Component,
    emotionCache = clientSideEmotionCache,
    pageProps,
    docsConfig,
    serviceWorkerPath,
    activePage,
    activePageParents,
    pageList,
    productIdentifier,
    productId,
    productCategoryId,
    demoDisplayName,
    csbConfig,
    adConfig,
    ThemeWrapper = ThemeProvider
  } = props;
  const pageContextValue = React.useMemo(() => ({
    activePage,
    activePageParents,
    pages: pageList,
    productIdentifier,
    productId,
    productCategoryId
  }), [activePage, activePageParents, pageList, productIdentifier, productId, productCategoryId]);
  const demoContextValue = React.useMemo(() => ({
    productDisplayName: demoDisplayName,
    csb: csbConfig
  }), [demoDisplayName, csbConfig]);
  const getLayout = Component.getLayout ?? (page => page);
  React.useEffect(() => {
    loadDependencies();
    registerServiceWorker(serviceWorkerPath);

    // Remove the server-side injected CSS.
    const jssStyles = document.querySelector('#jss-server-side');
    if (jssStyles) {
      jssStyles.parentElement.removeChild(jssStyles);
    }
  }, []); // eslint-disable-line react-hooks/exhaustive-deps

  return /*#__PURE__*/_jsxs(React.Fragment, {
    children: [/*#__PURE__*/_jsxs(NextHead, {
      children: [_meta || (_meta = /*#__PURE__*/_jsx("meta", {
        name: "viewport",
        content: "initial-scale=1, width=device-width"
      })), /*#__PURE__*/_jsx("meta", {
        name: "mui:productId",
        content: productId
      }), /*#__PURE__*/_jsx("meta", {
        name: "mui:productCategoryId",
        content: productCategoryId
      })]
    }), /*#__PURE__*/_jsx(DocsProvider, {
      config: docsConfig,
      adConfig: adConfig,
      defaultUserLanguage: pageProps.userLanguage,
      translations: pageProps.translations,
      children: /*#__PURE__*/_jsx(CodeCopyProvider, {
        children: /*#__PURE__*/_jsx(CodeStylingProvider, {
          children: /*#__PURE__*/_jsx(CodeVariantProvider, {
            children: /*#__PURE__*/_jsx(PageContext.Provider, {
              value: pageContextValue,
              children: /*#__PURE__*/_jsx(DemoContext.Provider, {
                value: demoContextValue,
                children: /*#__PURE__*/_jsx(ThemeWrapper, {
                  children: /*#__PURE__*/_jsx(DocsStyledEngineProvider, {
                    cacheLtr: emotionCache,
                    children: /*#__PURE__*/_jsxs(AnalyticsProvider, {
                      children: [getLayout(/*#__PURE__*/_jsx(Component, {
                        ...pageProps
                      })), _GoogleAnalytics || (_GoogleAnalytics = /*#__PURE__*/_jsx(GoogleAnalytics, {}))]
                    })
                  })
                })
              })
            })
          })
        })
      })
    })]
  });
}
export { DocsApp };