"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.OdysseyThemeProvider = void 0;
var _styled = _interopRequireDefault(require("@emotion/styled"));
var _styles = require("@mui/material/styles");
var _utils = require("@mui/utils");
var Tokens = _interopRequireWildcard(require("@okta/odyssey-design-tokens"));
var _react = require("react");
var _OdysseyDesignTokensContext = require("./OdysseyDesignTokensContext.cjs");
var _OdysseyThemeProviderPropsContext = require("./OdysseyThemeProviderPropsContext.cjs");
var _theme = require("./theme/theme.cjs");
var _useContrastMode = require("./useContrastMode.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * Copyright (c) 2022-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

const StyledContrastContainerStyles = (0, _styled.default)("div")({
  height: "inherit"
});
const OdysseyThemeProvider = ({
  children,
  contrastMode: contrastModeProp,
  designTokensOverride: designTokensOverrideProp,
  hasWrapperElement = true,
  shadowDomElement,
  shadowRootElement: shadowRootElementProp,
  themeOverride: themeOverrideProp
}) => {
  const odysseyThemeProviderPropsContext = (0, _OdysseyThemeProviderPropsContext.useOdysseyThemeProviderPropsContext)();
  const designTokensOverride = designTokensOverrideProp ?? odysseyThemeProviderPropsContext.designTokensOverride;
  const shadowRootElement = shadowRootElementProp ?? shadowDomElement ?? odysseyThemeProviderPropsContext.shadowRootElement;
  const themeOverride = themeOverrideProp ?? odysseyThemeProviderPropsContext.themeOverride;
  const {
    contrastMode,
    contrastContainerRef
  } = (0, _useContrastMode.useContrastMode)({
    contrastMode: contrastModeProp
  });
  const odysseyTokens = (0, _react.useMemo)(() => ({
    ...Tokens,
    ...designTokensOverride
  }), [designTokensOverride]);
  const odysseyTheme = (0, _react.useMemo)(() => (0, _theme.createOdysseyMuiTheme)({
    contrastMode,
    odysseyTokens,
    shadowRootElement
  }), [contrastMode, odysseyTokens, shadowRootElement]);
  const customOdysseyTheme = (0, _react.useMemo)(() => (0, _styles.createTheme)((0, _utils.deepmerge)(odysseyTheme, themeOverride || {})), [odysseyTheme, themeOverride]);
  const contrastModeProviderValue = (0, _react.useMemo)(() => ({
    contrastMode
  }), [contrastMode]);
  const providerComponents = (0, _react.useMemo)(() => (0, _jsxRuntime.jsx)(_useContrastMode.ContrastModeContext.Provider, {
    value: contrastModeProviderValue,
    children: (0, _jsxRuntime.jsx)(_styles.ThemeProvider, {
      theme: customOdysseyTheme,
      children: (0, _jsxRuntime.jsx)(_OdysseyThemeProviderPropsContext.OdysseyThemeProviderPropsProvider, {
        designTokensOverride: designTokensOverride,
        shadowRootElement: shadowRootElement,
        themeOverride: themeOverride,
        children: (0, _jsxRuntime.jsx)(_OdysseyDesignTokensContext.OdysseyDesignTokensContext.Provider, {
          value: odysseyTokens,
          children: children
        })
      })
    })
  }), [children, contrastModeProviderValue, customOdysseyTheme, designTokensOverride, odysseyTokens, shadowRootElement, themeOverride]);
  return hasWrapperElement ? (0, _jsxRuntime.jsx)(StyledContrastContainerStyles, {
    ref: contrastContainerRef,
    children: providerComponents
  }) : providerComponents;
};
const MemoizedOdysseyThemeProvider = exports.OdysseyThemeProvider = (0, _react.memo)(OdysseyThemeProvider);
MemoizedOdysseyThemeProvider.displayName = "OdysseyThemeProvider";
//# sourceMappingURL=OdysseyThemeProvider.cjs.map