"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var jsx_runtime_1 = require("react/jsx-runtime");
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var react_1 = require("react");
var antd_1 = require("antd");
var A_1 = __importDefault(require("components/misc/A"));
var site_name_1 = __importDefault(require("components/share/site-name"));
var router_1 = require("next/router");
function ProxyInput() {
    var router = (0, router_1.useRouter)();
    var _a = __read((0, react_1.useState)(""), 2), error = _a[0], setError = _a[1];
    return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: { margin: "15px 0" } }, { children: [(0, jsx_runtime_1.jsxs)(A_1.default, __assign({ href: "https://doc.cocalc.com/share.html" }, { children: ["Share what you create in ", (0, jsx_runtime_1.jsx)(site_name_1.default, {})] })), " ", "or paste a URL from anywhere on the web of a notebook, GitHub repo, or markdown file:", (0, jsx_runtime_1.jsx)(antd_1.Input.Search, { style: { marginTop: "10px" }, size: "large", placeholder: "URL to notebook or GitHub username or GitHub username/repo or Gist id", allowClear: true, enterButton: "Enter", onSearch: function (url) {
                    try {
                        router.push(urlToProxyURL(url));
                    }
                    catch (err) {
                        setError("Please enter a valid URL -- ".concat(err));
                    }
                } }), error && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { style: { marginTop: "15px" }, type: "error", message: error, showIcon: true }))] })));
}
exports.default = ProxyInput;
// INPUT: a URL to something on the internet
// OUTPUT: a URL on the share serve (without the http, host stuff)
//         that proxies that input URL.
// The cases we treat are:
//     - gist
//     - github user or repo
//     - general URL fallback, if none of the above apply
function urlToProxyURL(url) {
    var _a = new URL(url), host = _a.host, pathname = _a.pathname;
    if (host == new URL(document.URL).host) {
        // URL on this very server - just go to it
        return url;
    }
    else if (host == "github.com") {
        return "/github".concat(pathname);
    }
    else if (host == "gist.github.com") {
        return "/gist".concat(pathname);
    }
    else {
        return "/url/".concat(host).concat(pathname);
    }
}
//# sourceMappingURL=proxy-input.js.map