"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AjaxAction = void 0;
var tslib_1 = require("tslib");
var api_1 = require("../utils/api");
var errors_1 = require("../utils/errors");
var helper_1 = require("../utils/helper");
var Action_1 = require("./Action");
/**
 * 发送请求动作
 *
 * @export
 * @class AjaxAction
 * @implements {Action}
 */
var AjaxAction = /** @class */ (function () {
    function AjaxAction() {
    }
    AjaxAction.prototype.run = function (action, renderer, event) {
        var _a, _b, _c, _d;
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
            var env, options, result, responseData, e_1, result;
            var _e;
            return (0, tslib_1.__generator)(this, function (_f) {
                switch (_f.label) {
                    case 0:
                        env = event.context.env;
                        _f.label = 1;
                    case 1:
                        _f.trys.push([1, 3, , 4]);
                        options = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, ((_a = action.options) !== null && _a !== void 0 ? _a : {})), { method: (_b = action.method) !== null && _b !== void 0 ? _b : 'post' });
                        return [4 /*yield*/, env.fetcher(action.api, action.args, options)];
                    case 2:
                        result = _f.sent();
                        if (!(0, helper_1.isEmpty)(result.data) || result.ok) {
                            responseData = (0, api_1.normalizeApiResponseData)(result.data);
                            // 记录请求返回的数据
                            event.setData((0, helper_1.createObject)(event.data, action.outputVar
                                ? (_e = {},
                                    _e["".concat(action.outputVar)] = responseData,
                                    _e) : responseData));
                        }
                        if (!result.ok) {
                            throw new errors_1.ServerError((_c = (action.messages && action.messages.failed)) !== null && _c !== void 0 ? _c : result.msg, result);
                        }
                        else {
                            env.notify('success', (_d = (action.messages && action.messages.success)) !== null && _d !== void 0 ? _d : result.msg, result.msgTimeout !== undefined
                                ? {
                                    closeButton: true,
                                    timeout: result.msgTimeout
                                }
                                : undefined);
                        }
                        return [2 /*return*/, result.data];
                    case 3:
                        e_1 = _f.sent();
                        if (e_1.type === 'ServerError') {
                            result = e_1.response;
                            env.notify('error', e_1.message, result.msgTimeout !== undefined
                                ? {
                                    closeButton: true,
                                    timeout: result.msgTimeout
                                }
                                : undefined);
                        }
                        else {
                            env.notify('error', e_1.message);
                        }
                        return [3 /*break*/, 4];
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    return AjaxAction;
}());
exports.AjaxAction = AjaxAction;
(0, Action_1.registerAction)('ajax', new AjaxAction());
//# sourceMappingURL=./actions/AjaxAction.js.map
