"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const 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
 */
const react_1 = require("react");
const antd_1 = require("antd");
const A_1 = __importDefault(require("components/misc/A"));
const site_name_1 = __importDefault(require("components/share/site-name"));
const router_1 = require("next/router");
function ProxyInput() {
    const router = (0, router_1.useRouter)();
    const [error, setError] = (0, react_1.useState)("");
    return ((0, jsx_runtime_1.jsxs)("div", { style: { margin: "15px 0" }, children: [(0, jsx_runtime_1.jsxs)(A_1.default, { 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 to a ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "http://github.com/", children: "GitHub" }), " repository or", " ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "https://gist.github.com/", children: "Gist" }), ":", (0, jsx_runtime_1.jsx)(antd_1.Input.Search, { style: { marginTop: "10px" }, placeholder: "URL to GitHub repository or Gist", allowClear: true, enterButton: "View GitHub Repository or Gist", onSearch: (url) => {
                    try {
                        router.push(urlToProxyURL(url));
                    }
                    catch (err) {
                        setError(`${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
//
// NOTE: we implemented general URL's.  HOWEVER spammers use this to
// automate creating large numbers of links from cocalc to their bullshit
// pages to improve their SEO ranking.  Thus we restrict only to github.
//
function urlToProxyURL(url) {
    const { host, pathname } = new URL(url);
    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${pathname}`;
    }
    else if (host == "gist.github.com") {
        return `/gist${pathname}`;
    }
    else {
        throw Error("The URL most be to content on github.com.");
    }
}
//# sourceMappingURL=proxy-input.js.map