var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
(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", "../../App", "../../core/AtomBridge", "../../core/AtomLoader", "../../core/AtomUri", "../../di/Inject", "../../di/RegisterSingleton", "../../services/JsonService", "../../services/NavigationService", "../../view-model/AtomWindowViewModel"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var App_1 = require("../../App");
    var AtomBridge_1 = require("../../core/AtomBridge");
    var AtomLoader_1 = require("../../core/AtomLoader");
    var AtomUri_1 = require("../../core/AtomUri");
    var Inject_1 = require("../../di/Inject");
    var RegisterSingleton_1 = require("../../di/RegisterSingleton");
    var JsonService_1 = require("../../services/JsonService");
    var NavigationService_1 = require("../../services/NavigationService");
    var AtomWindowViewModel_1 = require("../../view-model/AtomWindowViewModel");
    var XFNavigationService = /** @class */ (function (_super) {
        __extends(XFNavigationService, _super);
        function XFNavigationService(app, jsonService) {
            var _this = _super.call(this, app) || this;
            _this.jsonService = jsonService;
            _this.stack = [];
            return _this;
        }
        Object.defineProperty(XFNavigationService.prototype, "title", {
            get: function () {
                // return bridge.getTitle();
                // throw new Error("Not supported");
                return undefined;
            },
            set: function (v) {
                // bridge.setTitle(v);
                // throw new Error("Not supported");
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(XFNavigationService.prototype, "location", {
            // private mLocation: ILocation;
            get: function () {
                return new AtomUri_1.AtomUri(bridge.navigationService.getLocation());
            },
            set: function (v) {
                bridge.navigationService.setLocation(v.toString());
            },
            enumerable: false,
            configurable: true
        });
        XFNavigationService.prototype.alert = function (message, title) {
            if (typeof message !== "string") {
                message = message.toString();
            }
            return new Promise(function (resolve, reject) {
                bridge.alert(message, title, function () {
                    resolve();
                }, function (f) {
                    reject(f);
                });
            });
        };
        XFNavigationService.prototype.confirm = function (message, title) {
            return new Promise(function (resolve, reject) {
                bridge.confirm(message, title, function (r) {
                    resolve(r);
                }, function (f) {
                    reject(f);
                });
            });
        };
        XFNavigationService.prototype.notify = function (message, title, type, delay) {
            // display toast pending..
            // tslint:disable-next-line: no-console
            console.warn("Display toast not yet implemented");
        };
        XFNavigationService.prototype.navigate = function (url) {
            var _this = this;
            var uri = new AtomUri_1.AtomUri(url);
            this.stack.push(url);
            this.app.runAsync(function () { return __awaiter(_this, void 0, void 0, function () {
                var popup;
                return __generator(this, function (_a) {
                    switch (_a.label) {
                        case 0: return [4 /*yield*/, AtomLoader_1.AtomLoader.loadView(uri, this.app, true)];
                        case 1:
                            popup = (_a.sent()).view;
                            bridge.setRoot(popup.element);
                            return [2 /*return*/];
                    }
                });
            }); });
        };
        XFNavigationService.prototype.back = function () {
            var _this = this;
            if (this.stack.length) {
                var url_1 = this.stack.pop();
                this.app.runAsync(function () { return __awaiter(_this, void 0, void 0, function () {
                    var uri, popup;
                    return __generator(this, function (_a) {
                        switch (_a.label) {
                            case 0:
                                uri = new AtomUri_1.AtomUri(url_1);
                                return [4 /*yield*/, AtomLoader_1.AtomLoader.loadView(uri, this.app, true)];
                            case 1:
                                popup = (_a.sent()).view;
                                bridge.setRoot(popup.element);
                                return [2 /*return*/];
                        }
                    });
                }); });
            }
        };
        XFNavigationService.prototype.refresh = function () {
            AtomBridge_1.AtomBridge.instance.reset();
        };
        XFNavigationService.prototype.openWindow = function (url, options) {
            return __awaiter(this, void 0, void 0, function () {
                var _a, popup, disposables, returnPromise, id, cancelToken, ve;
                var _this = this;
                return __generator(this, function (_b) {
                    switch (_b.label) {
                        case 0: return [4 /*yield*/, AtomLoader_1.AtomLoader.loadView(url, this.app, true, function () { return _this.app.resolve(AtomWindowViewModel_1.AtomWindowViewModel, true); })];
                        case 1:
                            _a = _b.sent(), popup = _a.view, disposables = _a.disposables, returnPromise = _a.returnPromise, id = _a.id;
                            cancelToken = options.cancelToken;
                            if (cancelToken) {
                                if (cancelToken.cancelled) {
                                    this.app.callLater(function () {
                                        _this.remove(popup, true);
                                    });
                                }
                                cancelToken.registerForCancel(function () {
                                    _this.remove(popup, true);
                                });
                            }
                            ve = popup.element;
                            AtomBridge_1.AtomBridge.instance.setValue(ve, "name", id);
                            bridge.pushPage(ve, options || {}, function () {
                                // reject("cancelled");
                                // do nothing...
                            }, function (e) {
                                _this.remove(popup, true);
                            });
                            disposables.add(function () {
                                AtomBridge_1.AtomBridge.instance.popPage(ve, function () {
                                    // do nothing
                                }, function (e) {
                                    // tslint:disable-next-line: no-console
                                    console.error(e);
                                });
                            });
                            return [2 /*return*/, returnPromise];
                    }
                });
            });
        };
        XFNavigationService = __decorate([
            RegisterSingleton_1.RegisterSingleton,
            __param(0, Inject_1.Inject),
            __param(1, Inject_1.Inject),
            __metadata("design:paramtypes", [App_1.App,
                JsonService_1.JsonService])
        ], XFNavigationService);
        return XFNavigationService;
    }(NavigationService_1.NavigationService));
    exports.default = XFNavigationService;
});
//# sourceMappingURL=XFNavigationService.js.map