"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 path_1 = require("path");
const react_1 = require("react");
const antd_1 = require("antd");
const mounted_1 = __importDefault(require("lib/hooks/mounted"));
const misc_1 = require("@cocalc/util/misc");
const copy_public_path_1 = __importDefault(require("lib/share/copy-public-path"));
const loading_1 = __importDefault(require("components/share/loading"));
const post_1 = __importDefault(require("lib/api/post"));
const A_1 = __importDefault(require("components/misc/A"));
const listing_1 = __importDefault(require("components/project/listing"));
const create_1 = __importDefault(require("components/project/create"));
const select_1 = __importDefault(require("components/project/select"));
const edit_url_1 = __importDefault(require("lib/share/edit-url"));
const icon_1 = require("@cocalc/frontend/components/icon");
const path_2 = __importDefault(require("components/app/path"));
const use_customize_1 = __importDefault(require("lib/use-customize"));
function ChooseProject(props) {
    const { id, src_project_id, path, url, relativePath, image, description } = props;
    const isMounted = (0, mounted_1.default)();
    const { defaultComputeImage } = (0, use_customize_1.default)();
    const [project, setProject] = (0, react_1.useState)(undefined);
    const [copying, setCopying] = (0, react_1.useState)("before");
    const [errorCopying, setErrorCopying] = (0, react_1.useState)("");
    const [showListing, setShowListing] = (0, react_1.useState)(false);
    const [hideSelect, setHideSelect] = (0, react_1.useState)(false);
    const [hideCreate, setHideCreate] = (0, react_1.useState)(false);
    const [createdProject, setCreatedProject] = (0, react_1.useState)(false);
    const targetPath = url
        ? (0, misc_1.path_split)((0, path_1.join)(path, relativePath)).tail
        : (0, path_1.join)(path, relativePath);
    (0, react_1.useEffect)(() => {
        // Always immediately start copying -- don't wait for user to click a button. See
        // https://github.com/sagemathinc/cocalc/issues/6025
        if (project != null && copying == "before") {
            doCopy();
        }
    }, [project != null]);
    async function doCopy() {
        try {
            if (project == null)
                throw Error("no target specified");
            setCopying("starting");
            setHideSelect(true);
            if (!createdProject) {
                // Possibly upgrade the project using a public_path license. Only do this if we
                // didn't just create the project, since in that case the license would already
                // be applied during creation.
                await (0, post_1.default)("/projects/public-path-license", {
                    public_path_id: id,
                    project_id: project.project_id,
                });
            }
            // Start the *target* project!
            await (0, post_1.default)("/projects/start", { project_id: project.project_id });
            if (!isMounted.current)
                return;
            setCopying("during");
            // Get the content
            if (url) {
                // From a URL
                await (0, post_1.default)("/projects/copy-url", {
                    project_id: project.project_id,
                    url,
                    path: targetPath,
                });
            }
            else {
                // From another project
                await (0, copy_public_path_1.default)({
                    id,
                    src_project_id,
                    path,
                    url,
                    relativePath,
                    target_project_id: project.project_id,
                });
            }
        }
        catch (err) {
            if (!isMounted.current)
                return;
            setErrorCopying(err.message);
        }
        finally {
            if (!isMounted.current)
                return;
            setCopying("after");
        }
    }
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("div", { children: [!errorCopying && copying == "after" && project?.project_id && ((0, jsx_runtime_1.jsx)(path_2.default, { start: true, project_id: project.project_id, path: targetPath })), image && image != defaultComputeImage && ((0, jsx_runtime_1.jsxs)("div", { children: ["We recommend that you create a new project, since this public path uses the non-default image \"", image, "\"."] })), !hideCreate && ((0, jsx_runtime_1.jsx)(create_1.default, { image: image, label: "In a new project", start: true, public_path_id: id, defaultTitle: url ? targetPath : description, onCreate: (project) => {
                            setCreatedProject(true);
                            setProject(project);
                            setHideSelect(true);
                            setHideCreate(true);
                        } })), !hideSelect && ((0, jsx_runtime_1.jsx)(select_1.default, { label: "In one of your existing projects", onChange: ({ project_id, title }) => {
                            setCreatedProject(false);
                            setProject({ project_id, title });
                            setHideCreate(true);
                        } }))] }), " ", project && ((0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", style: { width: "100%", marginTop: "15px" }, children: [(0, jsx_runtime_1.jsxs)("div", { style: { textAlign: "center" }, children: [copying == "before" && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(antd_1.Button, { onClick: doCopy, size: "large", type: "primary", style: { maxWidth: "100%", overflow: "hidden" }, shape: "round", children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "copy" }), " Copy ", targetPath, " to", (0, jsx_runtime_1.jsx)("b", { style: { marginLeft: "5px" }, children: project.title })] }), !hideSelect && ((0, jsx_runtime_1.jsxs)(antd_1.Checkbox, { disabled: !project, style: {
                                            float: "right",
                                            marginTop: "15px",
                                            fontSize: "10pt",
                                            color: "#666",
                                        }, onChange: (e) => setShowListing(e.target.checked), children: ["Show contents of", " ", (0, jsx_runtime_1.jsx)(A_1.default, { href: (0, edit_url_1.default)({
                                                    type: "collaborator",
                                                    project_id: project.project_id,
                                                }), external: true, children: (0, misc_1.trunc)(project.title, 30) })] }))] })), copying == "starting" && ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsxs)(loading_1.default, { style: { fontSize: "24px" }, children: ["Starting ", project.title, "..."] }) })), copying == "during" && ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsxs)(loading_1.default, { style: { fontSize: "24px" }, children: ["Copying files to ", project.title, "..."] }) })), copying == "after" && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: errorCopying ? "times-circle" : "check", style: { color: "darkgreen", fontSize: "16pt" } }), " ", "Finished copying ", targetPath, " to", " ", (0, jsx_runtime_1.jsx)(A_1.default, { href: (0, edit_url_1.default)({
                                            type: "collaborator",
                                            project_id: project.project_id,
                                            path: targetPath,
                                        }), external: true, children: targetPath }), " ", "in your project", " ", (0, jsx_runtime_1.jsx)(A_1.default, { href: (0, edit_url_1.default)({
                                            type: "collaborator",
                                            project_id: project.project_id,
                                        }), external: true, children: project.title }), ".", " ", errorCopying ? ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)("b", { children: "There appears to have been an issue copying files." }) })) : ("")] }))] }), errorCopying && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "warning", message: errorCopying, showIcon: true })), showListing && ((0, jsx_runtime_1.jsx)("div", { style: { marginTop: "10px" }, children: (0, jsx_runtime_1.jsx)(listing_1.default, { project_id: project.project_id, title: project.title, path: "", update: copying, sort: "time" }) }))] }))] }));
}
exports.default = ChooseProject;
//# sourceMappingURL=choose-project.js.map