(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", "../web/core/AtomUI"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.AtomUri = void 0;
    const AtomUI_1 = require("../web/core/AtomUI");
    class AtomUri {
        /**
         *
         */
        constructor(url) {
            let path;
            let query = "";
            let hash = "";
            let t = url.split("?");
            path = t[0];
            if (t.length === 2) {
                query = t[1] || "";
                t = query.split("#");
                query = t[0];
                hash = t[1] || "";
            }
            else {
                t = path.split("#");
                path = t[0];
                hash = t[1] || "";
            }
            // extract protocol and domain...
            let scheme = "";
            let host = "";
            let port = "";
            let i = path.indexOf("//");
            if (i !== -1) {
                scheme = path.substr(0, i);
                path = path.substr(i + 2);
                i = path.indexOf("/");
                if (i !== -1) {
                    host = path.substr(0, i);
                    path = path.substr(i + 1);
                    t = host.split(":");
                    if (t.length > 1) {
                        host = t[0];
                        port = t[1];
                    }
                }
            }
            this.host = host;
            this.protocol = scheme;
            this.port = port;
            this.path = path;
            this.query = AtomUI_1.AtomUI.parseUrl(query);
            this.hash = AtomUI_1.AtomUI.parseUrl(hash);
        }
        get pathAndQuery() {
            const q = [];
            const h = [];
            for (const key in this.query) {
                if (this.query.hasOwnProperty(key)) {
                    const element = this.query[key];
                    if (element === undefined || element === null) {
                        continue;
                    }
                    q.push(`${encodeURIComponent(key)}=${encodeURIComponent(element.toString())}`);
                }
            }
            for (const key in this.hash) {
                if (this.hash.hasOwnProperty(key)) {
                    const element = this.hash[key];
                    if (element === undefined || element === null) {
                        continue;
                    }
                    h.push(`${encodeURIComponent(key)}=${encodeURIComponent(element.toString())}`);
                }
            }
            const query = q.length ? "?" + q.join("&") : "";
            const hash = h.length ? "#" + h.join("&") : "";
            let path = this.path || "/";
            if (path.startsWith("/")) {
                path = path.substr(1);
            }
            return `${path}${query}${hash}`;
        }
        toString() {
            const port = this.port ? ":" + this.port : "";
            return `${this.protocol}//${this.host}${port}/${this.pathAndQuery}`;
        }
    }
    exports.AtomUri = AtomUri;
});
//# sourceMappingURL=AtomUri.js.map