"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 database_1 = __importDefault(require("lib/hooks/database"));
const use_customize_1 = __importDefault(require("lib/use-customize"));
const loading_1 = __importDefault(require("./loading"));
const licenses_1 = require("@cocalc/frontend/share/licenses");
const save_button_1 = __importDefault(require("components/misc/save-button"));
const edit_row_1 = __importDefault(require("components/misc/edit-row"));
const A_1 = __importDefault(require("components/misc/A"));
const select_site_license_1 = __importDefault(require("components/misc/select-site-license"));
const icon_1 = require("@cocalc/frontend/components/icon");
const latex_1 = __importDefault(require("components/landing/latex"));
const ui_1 = require("@cocalc/util/consts/ui");
const { Option } = antd_1.Select;
const 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({ id, project_id, path }) {
    const publicPaths = (0, database_1.default)({
        public_paths: { ...QUERY, id, project_id, path },
    });
    const siteLicense = (0, database_1.default)({
        public_paths_site_license_id: {
            site_license_id: null,
            id,
            project_id,
            path,
        },
    });
    const { onCoCalcCom } = (0, use_customize_1.default)();
    const [loaded, setLoaded] = (0, react_1.useState)(false);
    const [edited, setEdited] = (0, react_1.useState)({});
    const [original, setOriginal] = (0, react_1.useState)({});
    const [error, setError] = (0, react_1.useState)("");
    // After loading finishes, either editor or error is set.
    (0, react_1.useEffect)(() => {
        if (publicPaths.loading || siteLicense.loading)
            return;
        if (publicPaths.error) {
            setError(publicPaths.error);
            return;
        }
        if (siteLicense.error) {
            setError(siteLicense.error);
            return;
        }
        const { site_license_id } = siteLicense.value.public_paths_site_license_id;
        const { public_paths } = publicPaths.value;
        const x = { ...public_paths, 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.
    const visibility = get_visibility(edited);
    // we don't show "authenticated" on cocalc.com, unless it is set to it
    const showAuthenticated = !onCoCalcCom || edited.authenticated;
    const 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", { 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, { direction: "vertical", style: { width: "100%" }, children: [(0, jsx_runtime_1.jsx)(edit_row_1.default, { 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: (e) => setEdited({ ...edited, description: e.target.value }), autoSize: { minRows: 2, maxRows: 6 } }) }), (0, jsx_runtime_1.jsx)(edit_row_1.default, { 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: (e) => setEdited({ ...edited, name: e.target.value }) }) }), (0, jsx_runtime_1.jsx)(edit_row_1.default, { 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, { direction: "vertical", children: (0, jsx_runtime_1.jsx)(antd_1.Radio.Group, { value: visibility, onChange: (e) => {
                                    switch (e.target.value) {
                                        case "listed":
                                            setEdited({ ...edited, ...ui_1.SHARE_FLAGS.LISTED });
                                            break;
                                        case "unlisted":
                                            setEdited({ ...edited, ...ui_1.SHARE_FLAGS.UNLISTED });
                                            break;
                                        case "authenticated":
                                            setEdited({ ...edited, ...ui_1.SHARE_FLAGS.AUTHENTICATED });
                                            break;
                                        case "private":
                                            setEdited({ ...edited, ...ui_1.SHARE_FLAGS.DISABLED, name: "" });
                                            break;
                                    }
                                }, children: (0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", children: [(0, jsx_runtime_1.jsxs)(antd_1.Radio, { 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, { 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, { 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, { 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, { 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: (site_license_id) => {
                                setEdited({ ...edited, site_license_id });
                            } }) })), (0, jsx_runtime_1.jsx)(edit_row_1.default, { label: "Permission", description: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["An optional", " ", (0, jsx_runtime_1.jsx)(A_1.default, { 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: (license) => setEdited({ ...edited, license }) }) })] }), save] }));
}
exports.default = ConfigurePublicPath;
function License({ license, onChange }) {
    const options = [];
    for (const value in licenses_1.LICENSES) {
        options.push((0, jsx_runtime_1.jsx)(Option, { value: value, children: licenses_1.LICENSES[value] }, value));
    }
    return ((0, jsx_runtime_1.jsx)(antd_1.Select, { showSearch: true, value: license, style: { width: "100%" }, placeholder: "Select an open source license", optionFilterProp: "children", onChange: onChange, filterOption: (input, option) => option
            ? `${option.children}`.toLowerCase().includes(input.toLowerCase())
            : false, children: options }));
}
//# sourceMappingURL=configure-public-path.js.map