var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../services/NavigationService", "./baseTypes"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const NavigationService_1 = require("../services/NavigationService");
    const baseTypes_1 = require("./baseTypes");
    /**
     * Reports an alert to user when an error has occurred
     * or validation has failed.
     * If you set success message, it will display an alert with success message.
     * If you set confirm message, it will ask form confirmation before executing this method.
     * You can configure options to enable/disable certain
     * alerts.
     * @param reportOptions
     */
    function Action({ success = null, successTitle = "Done", confirm = null, confirmTitle = null, validate = false, validateTitle = null } = {}) {
        // tslint:disable-next-line: only-arrow-functions
        return function (target, key) {
            baseTypes_1.registerInit(target, (vm) => {
                // tslint:disable-next-line: ban-types
                const oldMethod = vm[key];
                const app = vm.app;
                // tslint:disable-next-line:only-arrow-functions
                vm[key] = function (...a) {
                    return __awaiter(this, void 0, void 0, function* () {
                        const ns = app.resolve(NavigationService_1.NavigationService);
                        try {
                            if (validate) {
                                if (!vm.isValid) {
                                    const vMsg = typeof validate === "boolean"
                                        ? "Please enter correct information"
                                        : validate;
                                    yield ns.alert(vMsg, validateTitle || "Error");
                                    return;
                                }
                            }
                            if (confirm) {
                                if (!(yield ns.confirm(confirm, confirmTitle || "Confirm"))) {
                                    return;
                                }
                            }
                            const pe = oldMethod.apply(vm, a);
                            if (pe && pe.then) {
                                const result = yield pe;
                                if (success) {
                                    yield ns.alert(success, successTitle);
                                }
                                return result;
                            }
                        }
                        catch (e) {
                            if (/^(cancelled|canceled)$/i.test(e.toString().trim())) {
                                // tslint:disable-next-line: no-console
                                console.warn(e);
                                return;
                            }
                            yield ns.alert(e, "Error");
                        }
                    });
                };
            });
        };
    }
    exports.default = Action;
});
//# sourceMappingURL=Action.js.map