"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoopAction = void 0;
var tslib_1 = require("tslib");
var helper_1 = require("../utils/helper");
var Action_1 = require("./Action");
var tpl_builtin_1 = require("../utils/tpl-builtin");
/**
 * 循环动作
 *
 * @export
 * @class LoopAction
 * @implements {Action}
 */
var LoopAction = /** @class */ (function () {
    function LoopAction() {
    }
    LoopAction.prototype.run = function (action, renderer, event, mergeData) {
        var _a;
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
            var loopData, protoData, _i, loopData_1, data, _b, _c, subAction;
            return (0, tslib_1.__generator)(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        if (typeof action.loopName !== 'string') {
                            console.warn('loopName 必须是字符串类型');
                            return [2 /*return*/];
                        }
                        loopData = (0, tpl_builtin_1.resolveVariable)(action.loopName, mergeData) || [];
                        if (!!loopData) return [3 /*break*/, 1];
                        console.warn("\u6CA1\u6709\u627E\u5230\u6570\u636E " + action.loopName);
                        return [3 /*break*/, 10];
                    case 1:
                        if (!!Array.isArray(loopData)) return [3 /*break*/, 2];
                        console.warn(action.loopName + " \u6570\u636E\u4E0D\u662F\u6570\u7EC4");
                        return [3 /*break*/, 10];
                    case 2:
                        if (!((_a = action.children) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 10];
                        protoData = event.data;
                        _i = 0, loopData_1 = loopData;
                        _d.label = 3;
                    case 3:
                        if (!(_i < loopData_1.length)) return [3 /*break*/, 9];
                        data = loopData_1[_i];
                        renderer.loopStatus = Action_1.LoopStatus.NORMAL;
                        // 追加逻辑处理中的数据，事件数据优先，用完还要还原
                        event.setData((0, helper_1.createObject)(event.data, data));
                        _b = 0, _c = action.children;
                        _d.label = 4;
                    case 4:
                        if (!(_b < _c.length)) return [3 /*break*/, 7];
                        subAction = _c[_b];
                        // @ts-ignore
                        if (renderer.loopStatus === Action_1.LoopStatus.CONTINUE) {
                            return [3 /*break*/, 6];
                        }
                        return [4 /*yield*/, (0, Action_1.runActions)(subAction, renderer, event)];
                    case 5:
                        _d.sent();
                        // @ts-ignore
                        if (renderer.loopStatus === Action_1.LoopStatus.BREAK || event.stoped) {
                            // 还原事件数据
                            event.setData(protoData);
                            return [3 /*break*/, 7];
                        }
                        _d.label = 6;
                    case 6:
                        _b++;
                        return [3 /*break*/, 4];
                    case 7:
                        if (event.stoped) {
                            // 还原事件数据
                            event.setData(protoData);
                            return [3 /*break*/, 9];
                        }
                        _d.label = 8;
                    case 8:
                        _i++;
                        return [3 /*break*/, 3];
                    case 9:
                        renderer.loopStatus = Action_1.LoopStatus.NORMAL;
                        event.setData(protoData);
                        _d.label = 10;
                    case 10: return [2 /*return*/];
                }
            });
        });
    };
    return LoopAction;
}());
exports.LoopAction = LoopAction;
(0, Action_1.registerAction)('loop', new LoopAction());
//# sourceMappingURL=./actions/LoopAction.js.map
