"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ConfigProvider () {
        return ConfigProvider;
    },
    get defaultConfigRef () {
        return defaultConfigRef;
    },
    get getDefaultConfig () {
        return getDefaultConfig;
    },
    get setDefaultConfig () {
        return setDefaultConfig;
    },
    get useConfig () {
        return useConfig;
    },
    get useRtl () {
        return useRtl;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _lodashkebabcase = /*#__PURE__*/ _interop_require_default._(require("lodash.kebabcase"));
var _reactfastcompare = /*#__PURE__*/ _interop_require_default._(require("react-fast-compare"));
var _components = require("@tarojs/components");
var _usememo = require("../../hooks/use-memo");
var _zhCN = /*#__PURE__*/ _interop_require_default._(require("../../locales/zh-CN"));
var classPrefix = 'nut-configprovider';
var defaultConfigRef = {
    current: {
        locale: _zhCN.default,
        direction: 'ltr'
    }
};
var setDefaultConfig = function setDefaultConfig(config) {
    defaultConfigRef.current = config;
};
var getDefaultConfig = function getDefaultConfig() {
    return defaultConfigRef.current;
};
var ConfigContext = /*#__PURE__*/ (0, _react.createContext)(null);
var useConfig = function useConfig() {
    var _useContext;
    return (_useContext = (0, _react.useContext)(ConfigContext)) !== null && _useContext !== void 0 ? _useContext : getDefaultConfig();
};
function convertThemeVarsToCSSVars(themeVars) {
    var cssVars = {};
    Object.keys(themeVars).forEach(function(key) {
        cssVars["--".concat((0, _lodashkebabcase.default)(key))] = themeVars[key];
    });
    return cssVars;
}
var useRtl = function useRtl() {
    var direction = useConfig().direction;
    return direction === 'rtl';
};
var ConfigProvider = function ConfigProvider(props) {
    var style = props.style, className = props.className, children = props.children, direction = props.direction, config = (0, _object_without_properties._)(props, [
        "style",
        "className",
        "children",
        "direction"
    ]);
    var mergedConfig = (0, _usememo.useMemo)(function() {
        return (0, _object_spread_props._)((0, _object_spread._)({}, getDefaultConfig(), config), {
            direction: direction
        });
    }, [
        config,
        direction
    ], function(prev, next) {
        return prev.some(function(prevTheme, index) {
            var nextTheme = next[index];
            return !(0, _reactfastcompare.default)(prevTheme, nextTheme);
        });
    });
    var cssVarStyle = _react.default.useMemo(function() {
        return convertThemeVarsToCSSVars(mergedConfig.theme || {});
    }, [
        mergedConfig.theme
    ]);
    return /*#__PURE__*/ _react.default.createElement(ConfigContext.Provider, {
        value: mergedConfig
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)(classPrefix, className, "nut-".concat(direction)),
        style: (0, _object_spread_props._)((0, _object_spread._)({}, cssVarStyle, style), {
            direction: direction
        })
    }, children));
};
ConfigProvider.displayName = 'NutConfigProvider';
