"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;
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
            var env, result, responseData, e_1, result;
            var _d;
            return (0, tslib_1.__generator)(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        env = event.context.env;
                        _e.label = 1;
                    case 1:
                        _e.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, env.fetcher(action.api, action.args, (_a = action.options) !== null && _a !== void 0 ? _a : {})];
                    case 2:
                        result = _e.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
                                ? (_d = {},
                                    _d["".concat(action.outputVar)] = responseData,
                                    _d) : responseData));
                        }
                        if (!result.ok) {
                            throw new errors_1.ServerError((_b = (action.messages && action.messages.failed)) !== null && _b !== void 0 ? _b : result.msg, result);
                        }
                        else {
                            env.notify('success', (_c = (action.messages && action.messages.success)) !== null && _c !== void 0 ? _c : result.msg, result.msgTimeout !== undefined
                                ? {
                                    closeButton: true,
                                    timeout: result.msgTimeout
                                }
                                : undefined);
                        }
                        return [2 /*return*/, result.data];
                    case 3:
                        e_1 = _e.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
