"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AjaxAction = void 0;
var tslib_1 = require("tslib");
var helper_1 = require("../utils/helper");
var tpl_1 = require("../utils/tpl");
var Action_1 = require("./Action");
/**
 * 发送请求动作
 *
 * @export
 * @class AjaxAction
 * @implements {Action}
 */
var AjaxAction = /** @class */ (function () {
    function AjaxAction() {
    }
    AjaxAction.prototype.run = function (action, renderer, event) {
        return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
            var store;
            var _this = this;
            return (0, tslib_1.__generator)(this, function (_a) {
                store = renderer.props.store;
                store.setCurrentAction(action);
                store
                    .saveRemote(action.api, action.args, {
                    successMessage: action.messages && action.messages.success,
                    errorMessage: action.messages && action.messages.failed
                })
                    .then(function () { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
                    var redirect;
                    return (0, tslib_1.__generator)(this, function (_a) {
                        switch (_a.label) {
                            case 0:
                                if (!(action.feedback && (0, helper_1.isVisible)(action.feedback, store.data))) return [3 /*break*/, 2];
                                return [4 /*yield*/, this.openFeedback(action.feedback, store)];
                            case 1:
                                _a.sent();
                                _a.label = 2;
                            case 2:
                                redirect = action.redirect && (0, tpl_1.filter)(action.redirect, store.data);
                                redirect && renderer.env.jumpTo(redirect, action);
                                return [2 /*return*/];
                        }
                    });
                }); })
                    .catch(function () { });
                return [2 /*return*/];
            });
        });
    };
    AjaxAction.prototype.openFeedback = function (dialog, store) {
        return new Promise(function (resolve) {
            store.setCurrentAction({
                type: 'button',
                actionType: 'dialog',
                dialog: dialog
            });
            store.openDialog(store.data, undefined, function (confirmed) {
                resolve(confirmed);
            });
        });
    };
    return AjaxAction;
}());
exports.AjaxAction = AjaxAction;
(0, Action_1.registerAction)('ajax', new AjaxAction());
//# sourceMappingURL=./actions/AjaxAction.js.map
