"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 database_1 = __importDefault(require("lib/hooks/database"));
var use_customize_1 = __importDefault(require("lib/use-customize"));
var loading_1 = __importDefault(require("./loading"));
var licenses_1 = require("@cocalc/frontend/share/licenses");
var save_button_1 = __importDefault(require("components/misc/save-button"));
var edit_row_1 = __importDefault(require("components/misc/edit-row"));
var A_1 = __importDefault(require("components/misc/A"));
var select_site_license_1 = __importDefault(require("components/misc/select-site-license"));
var icon_1 = require("@cocalc/frontend/components/icon");
var latex_1 = __importDefault(require("components/landing/latex"));
var ui_1 = require("@cocalc/util/consts/ui");
var Option = antd_1.Select.Option;
var QUERY = {
    name: null,
    description: null,
    disabled: null,
    unlisted: null,
    authenticated: null,
    license: null,
    compute_image: null,
};
function get_visibility(edited) {
    if (edited.disabled)
        return "private";
    if (edited.unlisted)
        return "unlisted";
    if (edited.authenticated)
        return "authenticated";
    return "listed";
}
function ConfigurePublicPath(_a) {
    var id = _a.id, project_id = _a.project_id, path = _a.path;
    var publicPaths = (0, database_1.default)({
        public_paths: __assign(__assign({}, QUERY), { id: id, project_id: project_id, path: path }),
    });
    var siteLicense = (0, database_1.default)({
        public_paths_site_license_id: {
            site_license_id: null,
            id: id,
            project_id: project_id,
            path: path,
        },
    });
    var onCoCalcCom = (0, use_customize_1.default)().onCoCalcCom;
    var _b = __read((0, react_1.useState)(false), 2), loaded = _b[0], setLoaded = _b[1];
    var _c = __read((0, react_1.useState)({}), 2), edited = _c[0], setEdited = _c[1];
    var _d = __read((0, react_1.useState)({}), 2), original = _d[0], setOriginal = _d[1];
    var _e = __read((0, react_1.useState)(""), 2), error = _e[0], setError = _e[1];
    // After loading finishes, either editor or error is set.
    (0, react_1.useEffect)(function () {
        if (publicPaths.loading || siteLicense.loading)
            return;
        if (publicPaths.error) {
            setError(publicPaths.error);
            return;
        }
        if (siteLicense.error) {
            setError(siteLicense.error);
            return;
        }
        var site_license_id = siteLicense.value.public_paths_site_license_id.site_license_id;
        var public_paths = publicPaths.value.public_paths;
        var x = __assign(__assign({}, public_paths), { site_license_id: site_license_id });
        setEdited(x);
        setOriginal(x);
        setLoaded(true);
    }, [publicPaths.loading, siteLicense.loading]);
    if (!loaded) {
        return (0, jsx_runtime_1.jsx)(loading_1.default, { delay: 0.2 });
    }
    // cheap to compute, so we compute every time.
    var visibility = get_visibility(edited);
    // we don't show "authenticated" on cocalc.com, unless it is set to it
    var showAuthenticated = !onCoCalcCom || edited.authenticated;
    var save = edited == null || original == null ? null : ((0, jsx_runtime_1.jsx)(save_button_1.default, { edited: edited, original: original, setOriginal: setOriginal, table: "public_paths" }));
    return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: {
            width: "100%",
            border: "1px solid #eee",
            padding: "15px",
            marginTop: "15px",
        } }, { children: [error && (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error, showIcon: true }), save, (0, jsx_runtime_1.jsxs)(antd_1.Divider, { children: ["How you are sharing \"", path, "\""] }), (0, jsx_runtime_1.jsxs)(antd_1.Space, __assign({ direction: "vertical", style: { width: "100%" } }, { children: [(0, jsx_runtime_1.jsx)(edit_row_1.default, __assign({ label: "Describe what you are sharing", description: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Use relevant keywords, inspire curiosity by providing just enough information to explain what this is about, and keep your description to about two lines. Use Markdown and ", (0, jsx_runtime_1.jsx)(latex_1.default, {}), ". You can change this at any time."] }) }, { children: (0, jsx_runtime_1.jsx)(antd_1.Input.TextArea, { style: { width: "100%" }, value: edited.description, onChange: function (e) {
                                return setEdited(__assign(__assign({}, edited), { description: e.target.value }));
                            }, autoSize: { minRows: 2, maxRows: 6 } }) })), (0, jsx_runtime_1.jsx)(edit_row_1.default, __assign({ label: "Choose a name for a nicer URL", description: "An optional name can provide a much nicer and more memorable URL.  You must also name your project (in project settings) and the owner of the project to get a nice URL.  (WARNING: Changing this once it is set can break links, since automatic redirection is not implemented.)" }, { children: (0, jsx_runtime_1.jsx)(antd_1.Input, { style: { maxWidth: "100%", width: "30em" }, value: edited.name, onChange: function (e) { return setEdited(__assign(__assign({}, edited), { name: e.target.value })); } }) })), (0, jsx_runtime_1.jsx)(edit_row_1.default, __assign({ label: showAuthenticated
                            ? "Listed, Unlisted, Authenticated or Private?"
                            : "Listed, Unlisted or Private?", description: "You make files or directories public to the world, either indexed by\n      search engines (listed), only visible with the link (unlisted), or only those who are authenticated.\n      Public files are automatically copied to the public server within about 30 seconds\n      after you explicitly edit them.  You can also set a site license for unlisted public shares." }, { children: (0, jsx_runtime_1.jsx)(antd_1.Space, __assign({ direction: "vertical" }, { children: (0, jsx_runtime_1.jsx)(antd_1.Radio.Group, __assign({ value: visibility, onChange: function (e) {
                                    switch (e.target.value) {
                                        case "listed":
                                            setEdited(__assign(__assign({}, edited), ui_1.SHARE_FLAGS.LISTED));
                                            break;
                                        case "unlisted":
                                            setEdited(__assign(__assign({}, edited), ui_1.SHARE_FLAGS.UNLISTED));
                                            break;
                                        case "authenticated":
                                            setEdited(__assign(__assign({}, edited), ui_1.SHARE_FLAGS.AUTHENTICATED));
                                            break;
                                        case "private":
                                            setEdited(__assign(__assign({}, edited), ui_1.SHARE_FLAGS.DISABLED));
                                            break;
                                    }
                                } }, { children: (0, jsx_runtime_1.jsxs)(antd_1.Space, __assign({ direction: "vertical" }, { children: [(0, jsx_runtime_1.jsxs)(antd_1.Radio, __assign({ value: "listed" }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "eye" }), " ", (0, jsx_runtime_1.jsx)("em", { children: "Public (listed): " }), " anybody can find this via search."] })), (0, jsx_runtime_1.jsxs)(antd_1.Radio, __assign({ value: "unlisted" }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "eye-slash" }), " ", (0, jsx_runtime_1.jsx)("em", { children: "Public (unlisted):" }), " only people with the link can view this."] })), showAuthenticated && ((0, jsx_runtime_1.jsxs)(antd_1.Radio, __assign({ value: "authenticated" }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: ui_1.SHARE_AUTHENTICATED_ICON }), " ", (0, jsx_runtime_1.jsx)("em", { children: "Authenticated:" }), " ", ui_1.SHARE_AUTHENTICATED_EXPLANATION, "."] }))), (0, jsx_runtime_1.jsxs)(antd_1.Radio, __assign({ value: "private" }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "lock" }), " ", (0, jsx_runtime_1.jsx)("em", { children: "Private:" }), " only collaborators on the project can view this."] }))] })) })) })) })), visibility == "unlisted" && ((0, jsx_runtime_1.jsx)(edit_row_1.default, __assign({ label: "Upgrade with a site license?", description: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["For unlisted shares, you can select a site license that you manage, and anybody who edits a copy of this share will have this site license applied to their project. You can track and remove usage of this license in the", " ", (0, jsx_runtime_1.jsx)(A_1.default, { children: "license management page" }), " (coming soon)."] }) }, { children: (0, jsx_runtime_1.jsx)(select_site_license_1.default, { defaultLicenseId: original.site_license_id, onChange: function (site_license_id) {
                                setEdited(__assign(__assign({}, edited), { site_license_id: site_license_id }));
                            } }) }))), (0, jsx_runtime_1.jsx)(edit_row_1.default, __assign({ label: "Permission", description: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["An optional", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://opensource.org/licenses" }, { children: "open source license" })), " ", "tells people how they may use what you are sharing."] }) }, { children: (0, jsx_runtime_1.jsx)(License, { license: edited.license, onChange: function (license) { return setEdited(__assign(__assign({}, edited), { license: license })); } }) }))] })), save] })));
}
exports.default = ConfigurePublicPath;
function License(_a) {
    var license = _a.license, onChange = _a.onChange;
    var options = [];
    for (var value in licenses_1.LICENSES) {
        options.push((0, jsx_runtime_1.jsx)(Option, __assign({ value: value }, { children: licenses_1.LICENSES[value] }), value));
    }
    return ((0, jsx_runtime_1.jsx)(antd_1.Select, __assign({ showSearch: true, value: license, style: { width: "100%" }, placeholder: "Select an open source license", optionFilterProp: "children", onChange: onChange, filterOption: function (input, option) {
            return option
                ? "".concat(option.children).toLowerCase().includes(input.toLowerCase())
                : false;
        } }, { children: options })));
}
//# sourceMappingURL=configure-public-path.js.map