import { __assign, __awaiter, __generator } from "tslib";
import { resolveDataBinds } from '../common';
import { set as lodashSet } from 'lodash-es';
var DEFAULT_MAX_TIMEOUT = 10 * 1000;
export function emitEvent(trigger, listeners, args, scopeContext, dataContext) {
    var _this = this;
    if (listeners === void 0) { listeners = []; }
    if (scopeContext === void 0) { scopeContext = {}; }
    var targetListeners = listeners.filter(function (l) { return l.trigger === trigger; });
    targetListeners.forEach(function (listener) { return __awaiter(_this, void 0, void 0, function () {
        var res, eventName, event_1, e_1, eventName, event_2;
        var _a, _b, _c, _d, _e, _f;
        return __generator(this, function (_g) {
            switch (_g.label) {
                case 0:
                    // 当前非捕获Event，再判断冒泡行为
                    if (!((_a = args === null || args === void 0 ? void 0 : args.event) === null || _a === void 0 ? void 0 : _a.isCapturePhase) && listener.noPropagation) {
                        ((_b = args === null || args === void 0 ? void 0 : args.domEvent) === null || _b === void 0 ? void 0 : _b.stopPropagation) && ((_c = args === null || args === void 0 ? void 0 : args.domEvent) === null || _c === void 0 ? void 0 : _c.stopPropagation());
                    }
                    if (!(((listener === null || listener === void 0 ? void 0 : listener.isCapturePhase) || false) ===
                        (((_d = args === null || args === void 0 ? void 0 : args.customEventData) === null || _d === void 0 ? void 0 : _d.isCapturePhase) || false))) return [3 /*break*/, 4];
                    _g.label = 1;
                case 1:
                    _g.trys.push([1, 3, , 4]);
                    return [4 /*yield*/, invokeListener(listener, args, scopeContext, dataContext)];
                case 2:
                    res = _g.sent();
                    eventName = listener.key + ".success";
                    event_1 = {
                        detail: res,
                        isCapturePhase: !!((_e = args.event) === null || _e === void 0 ? void 0 : _e.isCapturePhase),
                        origin: args.event,
                        name: eventName,
                    };
                    emitEvent(eventName, listeners, __assign(__assign({}, args), { event: event_1, customEventData: event_1 }), scopeContext, dataContext);
                    if (res) {
                        console.warn("@deprecated \u4E8B\u4EF6\u7F16\u6392\u4E2D\u540E\u7EED\u4E8B\u4EF6(" + eventName + ") event.detail.value.xxx \u5DF2\u5E9F\u5F03\uFF0C\u5EFA\u8BAE\u4F7F\u7528 event.detail \u76F4\u63A5\u8BBF\u95EE\u524D\u4E00\u52A8\u4F5C\u8FD4\u56DE\u503C");
                    }
                    return [3 /*break*/, 4];
                case 3:
                    e_1 = _g.sent();
                    eventName = listener.key + ".fail";
                    event_2 = {
                        detail: e_1,
                        origin: args.event,
                        isCapturePhase: !!((_f = args.event) === null || _f === void 0 ? void 0 : _f.isCapturePhase),
                        name: eventName,
                    };
                    emitEvent(eventName, listeners, __assign(__assign({}, args), { event: event_2, customEventData: event_2 }), scopeContext, dataContext);
                    console.warn("@deprecated \u4E8B\u4EF6\u7F16\u6392\u4E2D\u540E\u7EED\u4E8B\u4EF6(" + eventName + ") event.detail.value.xxx \u5DF2\u5E9F\u5F03\uFF0C\u5EFA\u8BAE\u4F7F\u7528 event.detail \u76F4\u63A5\u8BBF\u95EE\u524D\u4E00\u52A8\u4F5C\u8FD4\u56DE\u503C");
                    // 之前 invoke 内部catch 了错误，不会抛错
                    // throw e
                    console.error("Action error: [" + listener.trigger + "]", e_1);
                    return [3 /*break*/, 4];
                case 4: return [2 /*return*/];
            }
        });
    }); });
}
function invokeListener(_a, args, scopeContext, dataContext) {
    var instanceFunction = _a.instanceFunction, _b = _a.data, data = _b === void 0 ? {} : _b, _c = _a.dataBinds, dataBinds = _c === void 0 ? {} : _c;
    // ToDo resolve databinds
    var action = instanceFunction;
    var resolvedData = __assign({}, data);
    var resolvedDataBinds = resolveDataBinds(dataBinds, args.forItems, { event: args.event }, scopeContext, true, dataContext);
    // eslint-disable-next-line no-restricted-syntax
    for (var key in resolvedDataBinds) {
        if (resolvedDataBinds[key] &&
            resolvedDataBinds[key].__type === 'scopedValue') {
            try {
                lodashSet(resolvedData, key, resolvedDataBinds[key].getValue(scopeContext));
            }
            catch (e) {
                lodashSet(resolvedData, key, '');
            }
        }
        else {
            lodashSet(resolvedData, key, resolvedDataBinds[key]);
        }
    }
    var params = __assign({ data: resolvedData }, args);
    return action(params);
}
//# sourceMappingURL=actionHandler.js.map