"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defaultPrefixCls = exports.defaultConfigRef = exports.ConfigProvider = void 0;
exports.getDefaultConfig = getDefaultConfig;
exports.setDefaultConfig = setDefaultConfig;
exports.useConfig = useConfig;
var _tslib = require("tslib");
var _react = _interopRequireWildcard(require("react"));
var _global = require("../../global");
var _zhCN = _interopRequireDefault(require("../../locales/zh-CN"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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); }
const defaultPrefixCls = exports.defaultPrefixCls = _global.GLOBAL_PREFIX;
const defaultConfigRef = exports.defaultConfigRef = {
  current: {
    locale: _zhCN.default
  }
};
function setDefaultConfig(config) {
  defaultConfigRef.current = config;
}
function getDefaultConfig() {
  return defaultConfigRef.current;
}
const ConfigContext = _react.default.createContext(null);
const ConfigProvider = props => {
  const {
      children
    } = props,
    config = (0, _tslib.__rest)(props, ["children"]);
  const parentConfig = useConfig();
  const getPrefixCls = _react.default.useCallback((suffixCls, customizePrefixCls) => {
    if (customizePrefixCls) {
      return customizePrefixCls;
    }
    const mergedPrefixCls = config.prefixCls || parentConfig.prefixCls || defaultPrefixCls;
    return suffixCls ? `${mergedPrefixCls}-${suffixCls}` : mergedPrefixCls;
  }, [config.prefixCls, parentConfig.prefixCls]);
  return _react.default.createElement(ConfigContext.Provider, {
    value: Object.assign(Object.assign(Object.assign({}, parentConfig), config), {
      getPrefixCls
    })
  }, children);
};
exports.ConfigProvider = ConfigProvider;
function useConfig() {
  const context = (0, _react.useContext)(ConfigContext);
  if (!context) {
    const defaultConfig = getDefaultConfig();
    return Object.assign(Object.assign({}, defaultConfig), {
      getPrefixCls: (suffixCls, customizePrefixCls) => {
        if (customizePrefixCls) return customizePrefixCls;
        return suffixCls ? `${defaultPrefixCls}-${suffixCls}` : defaultPrefixCls;
      }
    });
  }
  return context;
}