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", "../../core/types", "../../core/XNode", "../../services/NavigationService", "../styles/AtomPageLinkStyle", "./AtomControl"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.AtomPageLink = void 0;
    const types_1 = require("../../core/types");
    const XNode_1 = require("../../core/XNode");
    const NavigationService_1 = require("../../services/NavigationService");
    const AtomPageLinkStyle_1 = require("../styles/AtomPageLinkStyle");
    const AtomControl_1 = require("./AtomControl");
    class AtomPageLink extends AtomControl_1.AtomControl {
        constructor(app, e) {
            super(app, e || document.createElement("span"));
        }
        preCreate() {
            this.page = null;
            this.parameters = null;
            this.isOpen = false;
            this.options = null;
            this.modal = false;
            this.toggle = true;
            this.defaultControlStyle = AtomPageLinkStyle_1.default;
            super.preCreate();
            this.bindEvent(this.element, "click", () => {
                if (this.modal) {
                    return this.openPopup();
                }
                return this.app.runAsync(() => this.openPopup());
            });
            this.bind(this.element, "styleClass", [["this", "isOpen"]], false, (v) => ({
                [this.controlStyle.name]: 1,
                "is-open": v,
                "atom-page-link": 1
            }), this);
            this.registerDisposable({
                dispose: () => {
                    const ct = this.cancelToken;
                    if (ct) {
                        ct.cancel();
                    }
                }
            });
        }
        openPopup() {
            return __awaiter(this, void 0, void 0, function* () {
                if (this.cancelToken) {
                    if (this.toggle) {
                        this.cancelToken.cancel();
                        this.cancelToken = null;
                        this.isOpen = false;
                        return;
                    }
                    else {
                        this.cancelToken.dispose();
                    }
                }
                this.cancelToken = new types_1.CancelToken();
                let o = null;
                try {
                    const navigationService = this.app.resolve(NavigationService_1.NavigationService);
                    const pt = this.page;
                    if (!pt) {
                        // tslint:disable-next-line:no-console
                        console.error("No popup template specified in PopupButton");
                        return;
                    }
                    this.isOpen = true;
                    o = this.options ? Object.assign(Object.assign({}, this.options), { cancelToken: this.cancelToken }) :
                        { cancelToken: this.cancelToken };
                    o.onInit = (view) => {
                        view.setLocalValue(view.element, "styleClass", `${this.controlStyle.name} page`);
                    };
                    const getParametersEvent = new CustomEvent("getParameters", { detail: {} });
                    this.element.dispatchEvent(getParametersEvent);
                    const p = getParametersEvent.detail.parameters || this.parameters;
                    const result = yield navigationService.openPage(pt, p, o);
                    this.element.dispatchEvent(new CustomEvent("result", { detail: result }));
                }
                catch (e) {
                    // if element is disposed or null, ignore
                    if (this.element) {
                        this.element.dispatchEvent(new CustomEvent("error", { detail: e }));
                    }
                }
                finally {
                    this.cancelToken = null;
                    this.isOpen = false;
                    if (o) {
                        o.onInit = null;
                    }
                }
            });
        }
    }
    exports.AtomPageLink = AtomPageLink;
    AtomPageLink.page = XNode_1.default.prepare("page", true, true);
});
//# sourceMappingURL=AtomPageLink.js.map