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());
    });
};
(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 });
    const App_1 = require("../../App");
    const AtomBridge_1 = require("../../core/AtomBridge");
    const AtomLoader_1 = require("../../core/AtomLoader");
    const AtomUri_1 = require("../../core/AtomUri");
    const Inject_1 = require("../../di/Inject");
    const RegisterSingleton_1 = require("../../di/RegisterSingleton");
    const JsonService_1 = require("../../services/JsonService");
    const NavigationService_1 = require("../../services/NavigationService");
    const AtomWindowViewModel_1 = require("../../view-model/AtomWindowViewModel");
    let XFNavigationService = class XFNavigationService extends NavigationService_1.NavigationService {
        constructor(app, jsonService) {
            super(app);
            this.jsonService = jsonService;
            this.stack = [];
        }
        get title() {
            // return bridge.getTitle();
            // throw new Error("Not supported");
            return undefined;
        }
        set title(v) {
            // bridge.setTitle(v);
            // throw new Error("Not supported");
        }
        // private mLocation: ILocation;
        get location() {
            return new AtomUri_1.AtomUri(bridge.navigationService.getLocation());
        }
        set location(v) {
            bridge.navigationService.setLocation(v.toString());
        }
        alert(message, title) {
            if (typeof message !== "string") {
                message = message.toString();
            }
            return new Promise((resolve, reject) => {
                bridge.alert(message, title, () => {
                    resolve();
                }, (f) => {
                    reject(f);
                });
            });
        }
        confirm(message, title) {
            return new Promise((resolve, reject) => {
                bridge.confirm(message, title, (r) => {
                    resolve(r);
                }, (f) => {
                    reject(f);
                });
            });
        }
        notify(message, title, type, delay) {
            // display toast pending..
            // tslint:disable-next-line: no-console
            console.warn("Display toast not yet implemented");
        }
        navigate(url) {
            const uri = new AtomUri_1.AtomUri(url);
            this.stack.push(url);
            this.app.runAsync(() => __awaiter(this, void 0, void 0, function* () {
                const { view: popup } = yield AtomLoader_1.AtomLoader.loadView(uri, this.app, true);
                bridge.setRoot(popup.element);
            }));
        }
        back() {
            if (this.stack.length) {
                const url = this.stack.pop();
                this.app.runAsync(() => __awaiter(this, void 0, void 0, function* () {
                    const uri = new AtomUri_1.AtomUri(url);
                    const { view: popup } = yield AtomLoader_1.AtomLoader.loadView(uri, this.app, true);
                    bridge.setRoot(popup.element);
                }));
            }
        }
        refresh() {
            AtomBridge_1.AtomBridge.instance.reset();
        }
        openWindow(url, options) {
            return __awaiter(this, void 0, void 0, function* () {
                const { view: popup, disposables, returnPromise, id } = yield AtomLoader_1.AtomLoader.loadView(url, this.app, true, () => this.app.resolve(AtomWindowViewModel_1.AtomWindowViewModel, true));
                if (options && options.onInit) {
                    options.onInit(popup);
                }
                const cancelToken = options.cancelToken;
                if (cancelToken) {
                    if (cancelToken.cancelled) {
                        this.app.callLater(() => {
                            this.remove(popup, true);
                        });
                    }
                    cancelToken.registerForCancel(() => {
                        this.remove(popup, true);
                    });
                }
                const ve = popup.element;
                AtomBridge_1.AtomBridge.instance.setValue(ve, "name", id);
                bridge.pushPage(ve, options || {}, () => {
                    // reject("cancelled");
                    // do nothing...
                }, (e) => {
                    this.remove(popup, true);
                });
                disposables.add(() => {
                    AtomBridge_1.AtomBridge.instance.popPage(ve, () => {
                        // do nothing
                    }, (e) => {
                        // tslint:disable-next-line: no-console
                        console.error(e);
                    });
                });
                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);
    exports.default = XFNavigationService;
});
//# sourceMappingURL=XFNavigationService.js.map