"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var next_1 = require("@alifd/next");
var hoist_non_react_statics_1 = __importDefault(require("hoist-non-react-statics"));
var ctx_1 = __importDefault(require("./ctx"));
var utils_1 = require("./utils");
var refElementStyle = {
    width: 0,
    height: 0,
    display: 'none',
};
var NextConfigConsumer = next_1.ConfigProvider.Consumer;
var ConfigProvider = (function (props) {
    var ref = (0, react_1.useRef)((0, utils_1.getInitialRefElement)());
    var _a = (0, react_1.useState)(ref.current), refElement = _a[0], setRefElement = _a[1];
    // 在初次渲染的时候，refElement还没渲染，因此需要在effect中再获取一次
    (0, react_1.useLayoutEffect)(function () {
        setRefElement(ref.current);
    }, []);
    // 让ConfigProvider内部的组件，都可以拿到ConfigProvider位置的元素
    // 从而可以从这个元素读取cssVar
    var ctxValue = (0, react_1.useMemo)(function () {
        return {
            refElement: refElement,
        };
    }, [refElement]);
    return (react_1.default.createElement(ctx_1.default.Provider, { value: ctxValue },
        react_1.default.createElement(next_1.ConfigProvider, __assign({}, props)),
        react_1.default.createElement(NextConfigConsumer, null, function (config) {
            var _a, _b;
            var prefix = (_b = (_a = props.prefix) !== null && _a !== void 0 ? _a : config === null || config === void 0 ? void 0 : config.prefix) !== null && _b !== void 0 ? _b : 'next-';
            return (react_1.default.createElement("div", { className: "".concat(prefix, "css-var-ref-element"), style: refElementStyle, ref: ref }));
        })));
});
ConfigProvider.displayName = 'ExtendedConfigProvider';
ConfigProvider.useRefElement = useRefElement;
(0, hoist_non_react_statics_1.default)(ConfigProvider, next_1.ConfigProvider);
exports.default = ConfigProvider;
/**
 * 获取微应用根元素
 */
function useRefElement() {
    var _a, _b;
    return (_b = (_a = (0, react_1.useContext)(ctx_1.default)) === null || _a === void 0 ? void 0 : _a.refElement) !== null && _b !== void 0 ? _b : (0, utils_1.getInitialRefElement)();
}
