"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppRender = exports.generateSlotMetaMap = exports.getComponentChildren = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var NodeRenderer_1 = require("./NodeRenderer");
var common_1 = require("../utils/common");
var mobx_react_lite_1 = require("mobx-react-lite");
var error_1 = require("../utils/error");
var router_1 = require("../router");
var utils_1 = require("../utils");
function checkDoNotRenderSlot(componentSchema, renderSlot, rootNode) {
    // 判断是否为 slot
    return (0, common_1.isSlot)(componentSchema) && !(renderSlot || rootNode);
}
function getComponentChildren(component, context) {
    var _a = component.items, items = _a === void 0 ? [] : _a;
    if (!items.length) {
        return [];
    }
    var list = items.sort(function (a, b) { var _a, _b; return (((_a = a.extra) === null || _a === void 0 ? void 0 : _a.xIndex) || 0) - (((_b = b.extra) === null || _b === void 0 ? void 0 : _b.xIndex) || 0); });
    var virtualFields = context.virtualFields, codeContext = context.codeContext, scopeContext = context.scopeContext, forContext = context.forContext, injectContext = context.injectContext, dataContext = context.dataContext, updateContext = context.updateContext;
    return list.map(function (schema) {
        return getRenderList({
            key: schema.id,
            componentSchema: schema,
            rootNode: false,
            renderSlot: false,
            virtualFields: virtualFields,
            codeContext: codeContext,
            scopeContext: scopeContext,
            forContext: forContext,
            injectContext: injectContext,
            dataContext: dataContext,
            updateContext: updateContext,
        });
    });
}
exports.getComponentChildren = getComponentChildren;
function generateSlotMetaMap(componentSchema, context, options) {
    var e_1, _a;
    var _b = componentSchema.items, items = _b === void 0 ? [] : _b;
    var scopeContext = context.scopeContext, codeContext = context.codeContext, virtualFields = context.virtualFields, injectContext = context.injectContext;
    var slots = {};
    var _loop_1 = function (child) {
        // TODO 做到不依赖组件 id
        var key = child.id;
        if ((0, common_1.isSlot)(child)) {
            var slotId = "".concat(componentSchema === null || componentSchema === void 0 ? void 0 : componentSchema.id, ".").concat(key);
            if ((injectContext.checkVisible || utils_1.checkVisible)({ id: slotId })) {
                var isHOC = (0, common_1.isScopeSlot)(componentSchema, key, injectContext);
                slots[key] = {
                    type: isHOC ? 'HOC' : 'ELEMENT',
                    node: isHOC
                        ? function (_a) {
                            var dataContext = _a.dataContext;
                            return function (props) {
                                var _a, _b;
                                return (React.createElement(router_1.InjectContext.Provider, { value: injectContext },
                                    React.createElement(NodeRenderer_1.ScopeContext.Provider, { value: tslib_1.__assign(tslib_1.__assign({}, scopeContext), (_a = {}, _a[componentSchema.id] = tslib_1.__assign(tslib_1.__assign({}, scopeContext === null || scopeContext === void 0 ? void 0 : scopeContext[componentSchema.id]), (_b = {}, _b[key] = props, _b)), _a)) },
                                        React.createElement(NodeRenderer_1.DataContext.Provider, { value: dataContext },
                                            React.createElement(exports.AppRender, { componentSchema: child, renderSlot: options.renderSlot, virtualFields: virtualFields, codeContext: codeContext })))));
                            };
                        }
                        : function (_a) {
                            var forContext = _a.forContext, dataContext = _a.dataContext;
                            return (React.createElement(router_1.InjectContext.Provider, { value: injectContext },
                                React.createElement(NodeRenderer_1.ScopeContext.Provider, { value: scopeContext },
                                    React.createElement(NodeRenderer_1.DataContext.Provider, { value: dataContext },
                                        React.createElement(NodeRenderer_1.ForContext.Provider, { value: forContext },
                                            React.createElement(exports.AppRender, { componentSchema: child, renderSlot: options === null || options === void 0 ? void 0 : options.renderSlot, virtualFields: virtualFields, codeContext: codeContext }))))));
                        },
                };
            }
        }
    };
    try {
        // eslint-disable-next-line guard-for-in
        for (var items_1 = tslib_1.__values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
            var child = items_1_1.value;
            _loop_1(child);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return slots;
}
exports.generateSlotMetaMap = generateSlotMetaMap;
function getRenderList(props) {
    var _a;
    var key = props.key, virtualFields = props.virtualFields, componentSchema = props.componentSchema, renderSlot = props.renderSlot, _b = props.rootNode, rootNode = _b === void 0 ? true : _b, codeContext = props.codeContext, scopeContext = props.scopeContext, _c = props.forContext, forContext = _c === void 0 ? {} : _c, _d = props.injectContext, injectContext = _d === void 0 ? {} : _d, dataContext = props.dataContext, updateContext = props.updateContext;
    var _e = (componentSchema.extra || {}).genericComp, genericComp = _e === void 0 ? {} : _e;
    // 判断是否为 slot
    if (checkDoNotRenderSlot(componentSchema, renderSlot, rootNode)) {
        return null;
    }
    if (componentSchema.module && componentSchema.component) {
        var sourceKey = "".concat(componentSchema.module, ":").concat(componentSchema.component);
        var Field = virtualFields[sourceKey];
        if (!Field) {
            return React.createElement(error_1.ComponentNotFound, { key: key, module: componentSchema.module, component: componentSchema.component });
        }
    }
    return (0, NodeRenderer_1.getComponentRenderList)({
        key: key,
        componentSchema: componentSchema,
        rootNode: rootNode,
        emitEvents: ((_a = componentSchema.extra) === null || _a === void 0 ? void 0 : _a.emitEvents) || [],
        renderSlot: renderSlot,
        virtualFields: virtualFields,
        codeContext: codeContext,
        scopeContext: scopeContext,
        forContext: forContext,
        dataContext: dataContext,
        updateContext: updateContext,
        injectContext: injectContext,
        genericComp: genericComp,
    });
}
function CommonAppRender(props) {
    var _a, _b, _c, _d;
    var className = props.className, componentSchema = props.componentSchema;
    //  wrapperClass;
    var preClassNameList = React.useRef([]);
    var _e = componentSchema.items, items = _e === void 0 ? [] : _e;
    var currentInjectClassNameList = ((_c = (_b = (_a = className === null || className === void 0 ? void 0 : className.split) === null || _a === void 0 ? void 0 : _a.call(className, /\s/)) === null || _b === void 0 ? void 0 : _b.filter) === null || _c === void 0 ? void 0 : _c.call(_b, function (item) { return !!item; })) || [];
    var containerEl = items[0];
    if (className && containerEl) {
        if (!containerEl.attributes) {
            containerEl.attributes = {};
        }
        var classNameList = (((_d = containerEl.attributes) === null || _d === void 0 ? void 0 : _d.class) || '').split(/\s/).filter(function (item) { return !!item; });
        // 先替换掉先前计算出来的className部分
        if (preClassNameList.current.length) {
            var map_1 = preClassNameList.current.reduce(function (map, item) {
                map[item] = true;
                return map;
            }, {});
            classNameList = classNameList.filter(function (item) { return !map_1[item]; });
        }
        containerEl.attributes.class = Array.from(new Set(classNameList.concat(currentInjectClassNameList))).join(' ');
    }
    preClassNameList.current = currentInjectClassNameList;
    return getRenderList(tslib_1.__assign(tslib_1.__assign({}, props), { dataContext: React.useContext(NodeRenderer_1.DataContext), scopeContext: React.useContext(NodeRenderer_1.ScopeContext), forContext: React.useContext(NodeRenderer_1.ForContext), injectContext: React.useContext(router_1.InjectContext) }));
}
exports.AppRender = (0, mobx_react_lite_1.observer)(CommonAppRender);
//# sourceMappingURL=RenderWrapper.js.map