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"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.Atom = void 0;
    class Atom {
        // tslint:disable-next-line: ban-types
        static superProperty(tc, target, name) {
            let c = tc;
            do {
                c = Object.getPrototypeOf(c);
                if (!c) {
                    throw new Error("No property descriptor found for " + name);
                }
                const pd = Object.getOwnPropertyDescriptor(c.prototype, name);
                if (!pd) {
                    continue;
                }
                return pd.get.apply(target);
            } while (true);
        }
        //      public static set(arg0: any, arg1: any, arg2: any): any {
        //     throw new Error("Method not implemented.");
        // }
        static get(target, path) {
            const segments = path.split(".");
            for (const iterator of segments) {
                if (target === undefined || target === null) {
                    return target;
                }
                target = target[iterator];
            }
            return target;
        }
        /**
         * Await till given milliseconds have passed
         * @param n
         * @param ct
         */
        static delay(n, ct) {
            return new Promise((resolve, reject) => {
                const h = {};
                h.id = setTimeout(() => {
                    // if (ct && ct.cancelled) {
                    //     reject(new Error("cancelled"));
                    //     return;
                    // }
                    resolve();
                }, n);
                if (ct) {
                    ct.registerForCancel(() => {
                        clearTimeout(h.id);
                        reject(new Error("cancelled"));
                    });
                }
            });
        }
        // // tslint:disable-next-line:member-access
        // static query(arg0: any): any {
        //     throw new Error("Method not implemented.");
        // }
        static encodeParameters(p) {
            if (!p) {
                return "";
            }
            let s = "";
            for (const key in p) {
                if (p.hasOwnProperty(key)) {
                    const element = p[key];
                    let v = element;
                    if (v === undefined || v === null) {
                        continue;
                    }
                    if (v instanceof Date) {
                        v = v.toISOString();
                    }
                    else if (typeof element === "object") {
                        v = JSON.stringify(element);
                    }
                    if (s) {
                        s += "&";
                    }
                    s += `${key}=${encodeURIComponent(v)}`;
                }
            }
            return s;
        }
        static url(url, query, hash) {
            if (!url) {
                return url;
            }
            let p = this.encodeParameters(query);
            if (p) {
                if (url.indexOf("?") === -1) {
                    url += "?";
                }
                else {
                    url += "&";
                }
                url += p;
            }
            p = this.encodeParameters(hash);
            if (p) {
                if (url.indexOf("#") === -1) {
                    url += "#";
                }
                else {
                    url += "&";
                }
                url += p;
            }
            return url;
        }
        /**
         * Schedules given call in next available callLater slot and also returns
         * promise that can be awaited, calling `Atom.postAsync` inside `Atom.postAsync`
         * will create deadlock
         * @static
         * @param {()=>Promise<any>} f
         * @returns {Promise<any>}
         * @memberof Atom
         */
        static postAsync(app, f) {
            return new Promise((resolve, reject) => {
                app.callLater(() => __awaiter(this, void 0, void 0, function* () {
                    try {
                        resolve(yield f());
                    }
                    catch (error) {
                        reject(error);
                    }
                }));
            });
        }
    }
    exports.Atom = Atom;
    Atom.designMode = false;
});
//# sourceMappingURL=Atom.js.map