"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
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 path_1 = require("path");
var react_1 = require("react");
var antd_1 = require("antd");
var mounted_1 = __importDefault(require("lib/hooks/mounted"));
var defaults_1 = require("@cocalc/util/db-schema/defaults");
var misc_1 = require("@cocalc/util/misc");
var copy_public_path_1 = __importDefault(require("lib/share/copy-public-path"));
var loading_1 = __importDefault(require("components/share/loading"));
var post_1 = __importDefault(require("lib/api/post"));
var A_1 = __importDefault(require("components/misc/A"));
var listing_1 = __importDefault(require("components/project/listing"));
var create_1 = __importDefault(require("components/project/create"));
var select_1 = __importDefault(require("components/project/select"));
var edit_url_1 = __importDefault(require("lib/share/edit-url"));
var icon_1 = require("@cocalc/frontend/components/icon");
var embed_1 = __importDefault(require("components/app/embed"));
var site_name_1 = __importDefault(require("components/share/site-name"));
function ChooseProject(_a) {
    var id = _a.id, src_project_id = _a.src_project_id, path = _a.path, relativePath = _a.relativePath, isDir = _a.isDir, image = _a.image, description = _a.description;
    var isMounted = (0, mounted_1.default)();
    var _b = __read((0, react_1.useState)(undefined), 2), project = _b[0], setProject = _b[1];
    var _c = __read((0, react_1.useState)("before"), 2), copying = _c[0], setCopying = _c[1];
    var _d = __read((0, react_1.useState)(""), 2), errorCopying = _d[0], setErrorCopying = _d[1];
    var _e = __read((0, react_1.useState)(false), 2), showListing = _e[0], setShowListing = _e[1];
    var _f = __read((0, react_1.useState)(false), 2), hideSelect = _f[0], setHideSelect = _f[1];
    var _g = __read((0, react_1.useState)(false), 2), hideCreate = _g[0], setHideCreate = _g[1];
    var targetPath = (0, path_1.join)(path, relativePath);
    function doCopy() {
        return __awaiter(this, void 0, void 0, function () {
            var err_1;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 4, 5, 6]);
                        if (project == null)
                            throw Error("no target specified");
                        // Possibly upgrade the project using a public_path license
                        return [4 /*yield*/, (0, post_1.default)("/projects/public-path-license", {
                                public_path_id: id,
                                project_id: project.project_id,
                            })];
                    case 1:
                        // Possibly upgrade the project using a public_path license
                        _a.sent();
                        // Start the *target* project!
                        setCopying("starting");
                        return [4 /*yield*/, (0, post_1.default)("/projects/start", { project_id: project.project_id })];
                    case 2:
                        _a.sent();
                        if (!isMounted.current)
                            return [2 /*return*/];
                        setCopying("during");
                        return [4 /*yield*/, (0, copy_public_path_1.default)({
                                id: id,
                                src_project_id: src_project_id,
                                path: path,
                                relativePath: relativePath,
                                target_project_id: project.project_id,
                            })];
                    case 3:
                        _a.sent();
                        return [3 /*break*/, 6];
                    case 4:
                        err_1 = _a.sent();
                        if (!isMounted.current)
                            return [2 /*return*/];
                        setErrorCopying(err_1.message);
                        return [3 /*break*/, 6];
                    case 5:
                        if (!isMounted.current)
                            return [2 /*return*/];
                        setCopying("after");
                        return [7 /*endfinally*/];
                    case 6: return [2 /*return*/];
                }
            });
        });
    }
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("div", { children: [image && image != defaults_1.DEFAULT_COMPUTE_IMAGE && ((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, defaultTitle: description, onCreate: function (project) {
                            setProject(project);
                            setHideSelect(true);
                        } })), !hideSelect && ((0, jsx_runtime_1.jsx)(select_1.default, { label: "In one of your existing projects", onChange: function (_a) {
                            var project_id = _a.project_id, title = _a.title;
                            setProject({ project_id: project_id, title: title });
                            setHideCreate(true);
                        } }))] }), " ", project && ((0, jsx_runtime_1.jsxs)(antd_1.Space, __assign({ direction: "vertical", style: { width: "100%", marginTop: "15px" } }, { children: [(0, jsx_runtime_1.jsxs)("div", __assign({ style: { textAlign: "center" } }, { children: [copying == "before" && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(antd_1.Button, __assign({ 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 ", (0, path_1.join)(path, relativePath), " to", (0, jsx_runtime_1.jsx)("b", __assign({ style: { marginLeft: "5px" } }, { children: project.title }))] })), !hideSelect && ((0, jsx_runtime_1.jsxs)(antd_1.Checkbox, __assign({ disabled: !project, style: {
                                            float: "right",
                                            marginTop: "15px",
                                            fontSize: "10pt",
                                            color: "#666",
                                        }, onChange: function (e) { return setShowListing(e.target.checked); } }, { children: ["Show contents of", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ 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, __assign({ 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, __assign({ 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 ", (0, path_1.join)(path, relativePath), " to", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ 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, __assign({ 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: "There might have been an issue copying files." })) : (""), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsxs)(antd_1.Button, __assign({ href: (0, edit_url_1.default)({
                                            type: "collaborator",
                                            project_id: project.project_id,
                                            path: targetPath,
                                        }), target: "_blank", size: "large", type: "primary", style: {
                                            maxWidth: "100%",
                                            overflow: "hidden",
                                            margin: "15px 0",
                                        }, shape: "round" }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "paper-plane" }), " Open your copy of \"", (0, path_1.join)(path, relativePath), "\" in the ", (0, jsx_runtime_1.jsx)(site_name_1.default, {}), " app..."] })), !isDir && ((0, jsx_runtime_1.jsx)(embed_1.default, { project_id: project.project_id, path: (0, path_1.join)(path, relativePath) }))] }))] })), errorCopying && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "warning", message: errorCopying, showIcon: true })), showListing && ((0, jsx_runtime_1.jsx)("div", __assign({ 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