"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.RuntimeConfigProvider = exports.getRuntimeConfig = exports.ConfigContext = exports.defaultConfig = void 0;

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _icons = require("@zarm-design/icons");

var React = _interopRequireWildcard(require("react"));

var _zh_CN = _interopRequireDefault(require("./locale/zh_CN"));

var _setCssVars = _interopRequireDefault(require("./setCssVars"));

var _setPrimaryColor = _interopRequireDefault(require("./setPrimaryColor"));

var _setTheme = _interopRequireDefault(require("./setTheme"));

var defaultConfig = {
  prefixCls: 'za',
  locale: _zh_CN.default,
  theme: 'light',
  safeArea: true,
  cssVars: {},
  mountContainer: function mountContainer() {
    return document.body;
  },
  scrollContainer: function scrollContainer() {
    return window;
  }
};
exports.defaultConfig = defaultConfig;
var ConfigContext = /*#__PURE__*/React.createContext(defaultConfig);
exports.ConfigContext = ConfigContext;
var runtimeConfigContext = defaultConfig;

var changeRuntimeConfigContext = function changeRuntimeConfigContext(props) {
  runtimeConfigContext = props;
};

var getRuntimeConfig = function getRuntimeConfig() {
  return runtimeConfigContext;
};

exports.getRuntimeConfig = getRuntimeConfig;

var ConfigProvider = function ConfigProvider(props) {
  var children = props.children,
      cssVars = props.cssVars,
      primaryColor = props.primaryColor,
      theme = props.theme,
      rest = (0, _objectWithoutProperties2.default)(props, ["children", "cssVars", "primaryColor", "theme"]);
  changeRuntimeConfigContext(props);
  React.useEffect(function () {
    primaryColor && (0, _setPrimaryColor.default)(primaryColor);
  }, [primaryColor]);
  React.useEffect(function () {
    theme && (0, _setTheme.default)(theme);
  }, [theme]);
  var newChildren = (0, _setCssVars.default)(children, cssVars);
  return /*#__PURE__*/React.createElement(ConfigContext.Provider, {
    value: rest
  }, /*#__PURE__*/React.createElement(_icons.IconProvider, {
    value: {
      prefixCls: rest.prefixCls
    }
  }, React.Children.only(newChildren)));
};

var RuntimeConfigProvider = function RuntimeConfigProvider(_ref) {
  var children = _ref.children;
  var props = React.useRef(runtimeConfigContext);
  React.useEffect(function () {
    props.current = runtimeConfigContext;
  }, [runtimeConfigContext]);
  return /*#__PURE__*/React.createElement(ConfigProvider, props === null || props === void 0 ? void 0 : props.current, children);
};

exports.RuntimeConfigProvider = RuntimeConfigProvider;
ConfigProvider.displayName = 'ConfigProvider';
ConfigProvider.defaultProps = defaultConfig;
var _default = ConfigProvider;
exports.default = _default;