import { __rest } from "tslib";
import React, { useContext } from 'react';
import { GLOBAL_PREFIX } from '../../global';
import zhCN from '../../locales/zh-CN';
export const defaultPrefixCls = GLOBAL_PREFIX;
export const defaultConfigRef = {
  current: {
    locale: zhCN
  }
};
export function setDefaultConfig(config) {
  defaultConfigRef.current = config;
}
export function getDefaultConfig() {
  return defaultConfigRef.current;
}
const ConfigContext = React.createContext(null);
export const ConfigProvider = props => {
  const {
      children
    } = props,
    config = __rest(props, ["children"]);
  const parentConfig = useConfig();
  const getPrefixCls = React.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.createElement(ConfigContext.Provider, {
    value: Object.assign(Object.assign(Object.assign({}, parentConfig), config), {
      getPrefixCls
    })
  }, children);
};
export function useConfig() {
  const context = 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;
}