"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppRender = exports.generateSlotMetaMap = exports.getComponentChildren = void 0;
var tslib_1 = require("tslib");
var React = (0, tslib_1.__importStar)(require("react"));
var NodeRenderer_1 = require("./NodeRenderer");
var lodash_set_1 = (0, tslib_1.__importDefault)(require("lodash.set"));
var lodash_clonedeep_1 = (0, tslib_1.__importDefault)(require("lodash.clonedeep"));
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");
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, dataContext = context.dataContext, updateContext = context.updateContext, injectContext = context.injectContext, forContext = context.forContext;
    var slots = {};
    var _loop_1 = function (child) {
        // TODO 做到不依赖组件 id
        var key = child.id;
        if ((0, common_1.isSlot)(child)) {
            var isHOC = (0, common_1.isScopeSlot)(componentSchema, key);
            slots[key] = {
                type: isHOC ? 'HOC' : 'ELEMENT',
                node: isHOC
                    ? function (props) {
                        var clonedScopeContext = (0, lodash_clonedeep_1.default)(scopeContext);
                        (0, lodash_set_1.default)(clonedScopeContext, componentSchema.id + "." + key, props);
                        return (React.createElement(router_1.InjectContext.Provider, { value: injectContext },
                            React.createElement(exports.AppRender, { key: key, componentSchema: child, renderSlot: options.renderSlot, virtualFields: virtualFields, codeContext: codeContext, scopeContext: clonedScopeContext, dataContext: dataContext, updateContext: updateContext })));
                    }
                    : function () {
                        return (React.createElement(router_1.InjectContext.Provider, { value: injectContext },
                            React.createElement(NodeRenderer_1.ForContext.Provider, { value: forContext },
                                React.createElement(exports.AppRender, { key: key, componentSchema: child, renderSlot: options === null || options === void 0 ? void 0 : options.renderSlot, virtualFields: virtualFields, codeContext: codeContext, scopeContext: scopeContext, dataContext: dataContext, updateContext: updateContext }))));
                    },
            };
        }
    };
    try {
        // eslint-disable-next-line guard-for-in
        for (var items_1 = (0, 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, _b;
    var key = props.key, className = props.className, virtualFields = props.virtualFields, componentSchema = props.componentSchema, renderSlot = props.renderSlot, _c = props.rootNode, rootNode = _c === void 0 ? true : _c, codeContext = props.codeContext, _d = props.scopeContext, scopeContext = _d === void 0 ? {} : _d, _e = props.forContext, forContext = _e === void 0 ? {} : _e, _f = props.injectContext, injectContext = _f === void 0 ? {} : _f, dataContext = props.dataContext, updateContext = props.updateContext;
    var _g = componentSchema.items, items = _g === void 0 ? [] : _g;
    var _h = (componentSchema.extra || {}).genericComp, genericComp = _h === void 0 ? {} : _h;
    // 判断是否为 slot
    if (checkDoNotRenderSlot(componentSchema, renderSlot, rootNode)) {
        return null;
    }
    // wrapperClass;
    // const preClassName = useRef<string>('');
    var containerEl = items[0];
    if ((containerEl === null || containerEl === void 0 ? void 0 : containerEl.attributes) && className) {
        var classNameList = (((_a = containerEl.attributes) === null || _a === void 0 ? void 0 : _a.class) || '')
            .split(/\s/)
            .filter(function (item) { return !!item; });
        // 先替换掉先前计算出来的className部分
        // if (preClassName.current) {
        //   if (preClassName.current !== className) {
        //     classNameList = classNameList.filter(
        //       (clsName) => clsName !== preClassName.current
        //     );
        //     preClassName.current = className;
        //   }
        // } else {
        //   preClassName.current = className;
        // }
        // containerEl.attributes.class = [className, ...classNameList].join(' ');
        containerEl.attributes.class = Array.from(new Set((0, tslib_1.__spreadArray)([className], (0, tslib_1.__read)(classNameList), false))).join(' ');
    }
    if (componentSchema.module && componentSchema.component) {
        var sourceKey = componentSchema.module + ":" + componentSchema.component;
        var Field = virtualFields[sourceKey];
        if (!Field) {
            return (React.createElement(error_1.ComponentNotFound, { key: key, module: componentSchema.module, component: componentSchema.component }));
        }
    }
    var context = {
        virtualFields: virtualFields,
        codeContext: codeContext,
        scopeContext: scopeContext,
        forContext: forContext,
        dataContext: dataContext,
        updateContext: updateContext,
        injectContext: injectContext,
    };
    return (0, NodeRenderer_1.getComponentRenderList)({
        key: key,
        componentSchema: componentSchema,
        rootNode: rootNode,
        emitEvents: ((_b = componentSchema.extra) === null || _b === void 0 ? void 0 : _b.emitEvents) || [],
        renderSlot: renderSlot,
        virtualFields: virtualFields,
        codeContext: codeContext,
        scopeContext: scopeContext,
        forContext: forContext,
        dataContext: dataContext,
        updateContext: updateContext,
        injectContext: injectContext,
        genericComp: genericComp,
    });
}
exports.AppRender = (0, mobx_react_lite_1.observer)(function (props) {
    return getRenderList((0, tslib_1.__assign)((0, tslib_1.__assign)({}, props), { forContext: React.useContext(NodeRenderer_1.ForContext), injectContext: React.useContext(router_1.InjectContext) }));
});
//# sourceMappingURL=RenderWrapper.js.map