"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");
var antd_1 = require("antd");
var icon_1 = require("@cocalc/frontend/components/icon");
var react_1 = require("react");
var A_1 = __importDefault(require("components/misc/A"));
var loading_1 = __importDefault(require("components/share/loading"));
var recent_files_1 = __importDefault(require("./recent-files"));
var router_1 = require("next/router");
var misc_1 = require("@cocalc/util/misc");
var post_1 = __importDefault(require("lib/api/post"));
var browser_info_1 = __importDefault(require("./browser-info"));
var customize_1 = require("lib/customize");
var util_1 = require("./util");
var tickets_1 = require("./tickets");
var site_name_1 = __importDefault(require("components/share/site-name"));
var config_1 = require("lib/config");
function VSpace(_a) {
    var children = _a.children;
    return ((0, jsx_runtime_1.jsx)(antd_1.Space, __assign({ direction: "vertical", style: { width: "100%", fontSize: "12pt" } }, { children: children })));
}
function stringToType(s) {
    if (s == "problem" || s == "question" || s == "task")
        return s;
    return "problem"; // default;
}
function Create() {
    var _a;
    var _b = (0, customize_1.useCustomize)(), contactEmail = _b.contactEmail, zendesk = _b.zendesk, account = _b.account;
    var router = (0, router_1.useRouter)();
    // The URL the user was viewing when they requested support.
    // This could easily be blank, but if it is set it can be useful.
    var url = router.query.url;
    var _c = __read((0, react_1.useState)([]), 2), files = _c[0], setFiles = _c[1];
    var _d = __read((0, react_1.useState)(stringToType(router.query.type)), 2), type = _d[0], setType = _d[1];
    var _e = __read((0, react_1.useState)((_a = account === null || account === void 0 ? void 0 : account.email_address) !== null && _a !== void 0 ? _a : ""), 2), email = _e[0], setEmail = _e[1];
    var _f = __read((0, react_1.useState)(router.query.body ? "".concat(router.query.body) : ""), 2), body = _f[0], setBody = _f[1];
    var _g = __read((0, react_1.useState)(router.query.subject ? "".concat(router.query.subject) : ""), 2), subject = _g[0], setSubject = _g[1];
    var _h = __read((0, react_1.useState)(""), 2), submitError = _h[0], setSubmitError = _h[1];
    var _j = __read((0, react_1.useState)(false), 2), submitting = _j[0], setSubmitting = _j[1];
    var _k = __read((0, react_1.useState)(""), 2), success = _k[0], setSuccess = _k[1];
    var submittable = (0, react_1.useRef)(false);
    submittable.current = !!(!submitting &&
        !submitError &&
        !success &&
        (0, misc_1.is_valid_email_address)(email) &&
        subject &&
        body);
    if (!zendesk) {
        return (0, jsx_runtime_1.jsx)(util_1.NoZendesk, {});
    }
    function createSupportTicket() {
        return __awaiter(this, void 0, void 0, function () {
            var info, options, result, err_1;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        info = (0, browser_info_1.default)();
                        options = { type: type, files: files, email: email, body: body, url: url, subject: subject, info: info };
                        setSubmitError("");
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, 4, 5]);
                        setSubmitting(true);
                        return [4 /*yield*/, (0, post_1.default)("/support/create-ticket", { options: options })];
                    case 2:
                        result = _a.sent();
                        return [3 /*break*/, 5];
                    case 3:
                        err_1 = _a.sent();
                        setSubmitError(err_1.message);
                        return [2 /*return*/];
                    case 4:
                        setSubmitting(false);
                        return [7 /*endfinally*/];
                    case 5:
                        setSuccess((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("p", { children: ["Please save this URL: ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: result.url }, { children: result.url }))] }), (0, jsx_runtime_1.jsxs)("p", { children: ["You can also see the", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "/support/tickets" }, { children: "status of your support tickets" })), "."] })] }));
                        return [2 /*return*/];
                }
            });
        });
    }
    return ((0, jsx_runtime_1.jsx)(antd_1.Layout.Content, __assign({ style: {
            backgroundColor: "white",
        } }, { children: (0, jsx_runtime_1.jsxs)("div", __assign({ style: {
                maxWidth: config_1.MAX_WIDTH,
                margin: "15px auto",
                padding: "15px",
                backgroundColor: "white",
                color: "#555",
            } }, { children: [" ", (0, jsx_runtime_1.jsx)("h1", __assign({ style: { textAlign: "center", fontSize: "24pt" } }, { children: "Create a New Support Ticket" })), router.query.hideExtra != "true" && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("p", __assign({ style: { fontSize: "12pt" } }, { children: ["Create a new support ticket below or", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "/support/tickets" }, { children: "check the status of your support tickets" })), ".", " ", contactEmail && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["You can also email us directly at", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "mailto:".concat(contactEmail) }, { children: contactEmail })), "."] }))] })), (0, jsx_runtime_1.jsx)(FAQ, {}), (0, jsx_runtime_1.jsx)("h1", { children: "Create Your Ticket" }), (0, jsx_runtime_1.jsx)(Instructions, {}), (0, jsx_runtime_1.jsx)(antd_1.Divider, { children: "Support Ticket" })] })), (0, jsx_runtime_1.jsxs)("form", { children: [(0, jsx_runtime_1.jsxs)(VSpace, { children: [(0, jsx_runtime_1.jsxs)("b", { children: [(0, jsx_runtime_1.jsx)(Status, { done: (0, misc_1.is_valid_email_address)(email) }), " Your Email Address"] }), (0, jsx_runtime_1.jsx)(antd_1.Input, { prefix: (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "envelope", style: { color: "rgba(0,0,0,.25)" } }), defaultValue: email, placeholder: "Email address...", style: { maxWidth: "500px" }, onChange: function (e) { return setEmail(e.target.value); } }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsxs)("b", { children: [(0, jsx_runtime_1.jsx)(Status, { done: subject }), " Subject"] }), (0, jsx_runtime_1.jsx)(antd_1.Input, { placeholder: "Summarize what this ticket is about...", onChange: function (e) { return setSubject(e.target.value); }, defaultValue: subject }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsxs)("b", { children: ["Is this a ", (0, jsx_runtime_1.jsx)("i", { children: "Problem" }), ", ", (0, jsx_runtime_1.jsx)("i", { children: "Question" }), ", or", " ", (0, jsx_runtime_1.jsx)("i", { children: "Software Install Task" }), "?"] }), (0, jsx_runtime_1.jsx)(antd_1.Radio.Group, __assign({ name: "radiogroup", defaultValue: type, onChange: function (e) { return setType(e.target.value); } }, { children: (0, jsx_runtime_1.jsxs)(VSpace, { children: [(0, jsx_runtime_1.jsxs)(antd_1.Radio, __assign({ value: "problem" }, { children: [(0, jsx_runtime_1.jsx)(tickets_1.Type, { type: "problem" }), " Something is not working the way I think it should work."] })), (0, jsx_runtime_1.jsxs)(antd_1.Radio, __assign({ value: "question" }, { children: [(0, jsx_runtime_1.jsx)(tickets_1.Type, { type: "question" }), " I have a question about billing, functionality, teaching, something not working, etc."] })), (0, jsx_runtime_1.jsxs)(antd_1.Radio, __assign({ value: "task" }, { children: [(0, jsx_runtime_1.jsx)(tickets_1.Type, { type: "task" }), " Is it possible for you to install some software that I need in order to use ", (0, jsx_runtime_1.jsx)(site_name_1.default, {}), "?"] }))] }) })), (0, jsx_runtime_1.jsx)("br", {}), router.query.hideExtra != "true" && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Files, { onChange: setFiles }), (0, jsx_runtime_1.jsx)("br", {})] })), (0, jsx_runtime_1.jsxs)("b", { children: [(0, jsx_runtime_1.jsx)(Status, { done: body && body.length > 10 }), " Description"] }), (0, jsx_runtime_1.jsxs)("div", __assign({ style: {
                                        marginLeft: "30px",
                                        borderLeft: "1px solid lightgrey",
                                        paddingLeft: "15px",
                                    } }, { children: [type == "problem" && (0, jsx_runtime_1.jsx)(Problem, { onChange: setBody }), type == "question" && ((0, jsx_runtime_1.jsx)(Question, { onChange: setBody, defaultValue: body })), type == "task" && (0, jsx_runtime_1.jsx)(Task, { onChange: setBody })] }))] }), (0, jsx_runtime_1.jsxs)("p", __assign({ style: { marginTop: "30px" } }, { children: ["After submitting this ticket, you'll receive a link, which you should save until you receive a confirmation email. You can also", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "/support/tickets" }, { children: "check the status of your support tickets" })), "."] })), (0, jsx_runtime_1.jsxs)("div", __assign({ style: { textAlign: "center", marginTop: "30px" } }, { children: [(0, jsx_runtime_1.jsxs)(antd_1.Button, __assign({ shape: "round", size: "large", disabled: !submittable.current, type: "primary", onClick: createSupportTicket }, { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "paper-plane" }), " ", submitting
                                            ? "Submitting..."
                                            : success
                                                ? "Thank you for creating a ticket"
                                                : submitError
                                                    ? "Close the error box to try again"
                                                    : !(0, misc_1.is_valid_email_address)(email)
                                                        ? "Enter Valid Email Address above"
                                                        : !subject
                                                            ? "Enter Subject above"
                                                            : !body
                                                                ? "Describe your issue above"
                                                                : "Create Support Ticket"] })), submitting && (0, jsx_runtime_1.jsx)(loading_1.default, { style: { fontSize: "32pt" } }), submitError && ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: "Error creating support ticket", description: submitError, closable: true, showIcon: true, onClose: function () { return setSubmitError(""); }, style: { margin: "15px auto", maxWidth: "500px" } }), (0, jsx_runtime_1.jsx)("br", {}), contactEmail && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["If you continue to have problems, email us directly at", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "mailto:".concat(contactEmail) }, { children: contactEmail })), "."] }))] })), success && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "success", message: "Successfully created support ticket", description: success, onClose: function () {
                                        // simplest way to reset all the information in the form.
                                        router.reload();
                                    }, closable: true, showIcon: true, style: { margin: "15px auto", maxWidth: "500px" } }))] }))] })] })) })));
}
exports.default = Create;
function Files(_a) {
    var onChange = _a.onChange;
    return ((0, jsx_runtime_1.jsxs)(VSpace, { children: [(0, jsx_runtime_1.jsx)("b", { children: "Relevant Files" }), "Select any relevant projects and files below. This will make it much easier for us to quickly understand your problem.", (0, jsx_runtime_1.jsx)(recent_files_1.default, { interval: "1 day", onChange: onChange })] }));
}
function Problem(_a) {
    var onChange = _a.onChange;
    var answers = (0, react_1.useRef)(["", "", ""]);
    function update(i, value) {
        answers.current[i] = value;
        onChange === null || onChange === void 0 ? void 0 : onChange(answers.current.join("\n\n\n").trim());
    }
    return ((0, jsx_runtime_1.jsxs)(VSpace, { children: [(0, jsx_runtime_1.jsx)("b", { children: "What did you do exactly?" }), (0, jsx_runtime_1.jsx)(antd_1.Input.TextArea, { rows: 3, placeholder: "Describe what you did...", onChange: function (e) {
                    return update(0, e.target.value
                        ? "\n\nWHAT DID YOU DO EXACTLY?\n\n" + e.target.value
                        : "");
                } }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("b", { children: "What happened?" }), (0, jsx_runtime_1.jsx)(antd_1.Input.TextArea, { rows: 3, placeholder: "Tell us what happened...", onChange: function (e) {
                    return update(1, e.target.value ? "\n\nWHAT HAPPENED?\n\n" + e.target.value : "");
                } }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("b", { children: "How did this differ from what you expected?" }), (0, jsx_runtime_1.jsx)(antd_1.Input.TextArea, { rows: 3, placeholder: "Explain how this differs from what you expected...", onChange: function (e) {
                    return update(2, e.target.value
                        ? "\n\nHOW DID THIS DIFFER FROM WHAT YOU EXPECTED?\n\n" +
                            e.target.value
                        : "");
                } })] }));
}
function Question(_a) {
    var defaultValue = _a.defaultValue, onChange = _a.onChange;
    return ((0, jsx_runtime_1.jsx)(antd_1.Input.TextArea, { rows: 8, defaultValue: defaultValue, placeholder: "Your question...", onChange: function (e) { return onChange(e.target.value); } }));
}
function Task(_a) {
    var onChange = _a.onChange;
    var answers = (0, react_1.useRef)(["", "", ""]);
    function update(i, value) {
        answers.current[i] = value;
        onChange === null || onChange === void 0 ? void 0 : onChange(answers.current.join("\n\n\n").trim());
    }
    return ((0, jsx_runtime_1.jsxs)("div", { children: ["Each ", (0, jsx_runtime_1.jsx)(site_name_1.default, {}), " project is a Docker image running Ubuntu Linux on 64-bit x86 hardware, so it is possible for us to install most standard Linux software, and we have already installed", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "/software" }, { children: "a huge amount" })), ". If there is something you need that is missing, let us know below.", (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("b", { children: "What software do you need?" }), " In particular, if this is a Python library, explain which of the", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "software/python" }, { children: "many Python environments" })), " you need it installed into and why you can't just", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://doc.cocalc.com/howto/install-python-lib.html" }, { children: "install it yourself" })), ".", (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(antd_1.Input.TextArea, { style: { marginTop: "10px" }, rows: 4, placeholder: "Describe what software you need installed...", onChange: function (e) {
                    return update(0, e.target.value
                        ? "\n\nWHAT SOFTWARE DO YOU NEED?\n\n" + e.target.value
                        : "");
                } }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("b", { children: "How do you plan to use this software?" }), " For example, does it need to be installed across ", (0, jsx_runtime_1.jsx)(site_name_1.default, {}), " for a course you are teaching that starts in 3 weeks?", (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(antd_1.Input.TextArea, { style: { marginTop: "10px" }, rows: 3, placeholder: "Explain how you will use the software ...", onChange: function (e) {
                    return update(1, e.target.value
                        ? "\n\nHOW DO YOU PLAN TO USE THIS SOFTWARE?\n\n" + e.target.value
                        : "");
                } }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("b", { children: "How can we test that the software is properly installed?" }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(antd_1.Input.TextArea, { style: { marginTop: "10px" }, rows: 3, placeholder: "Explain how we can test the software...", onChange: function (e) {
                    return update(2, e.target.value
                        ? "\n\nHOW CAN WE TEST THAT THE SOFTWARE IS PROPERLY INSTALLED?\n\n" +
                            e.target.value
                        : "");
                } })] }));
}
function Instructions() {
    return ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsxs)("p", { children: ["If you still need help, create a support ticket below. Support is currently available in ", (0, jsx_runtime_1.jsx)("b", { children: "English and German" }), " only."] }) }));
}
function FAQ() {
    return ((0, jsx_runtime_1.jsxs)("div", { children: [" ", (0, jsx_runtime_1.jsx)("h2", { children: "Frequently Asked Questions" }), (0, jsx_runtime_1.jsx)(antd_1.Alert, { message: "", style: { margin: "20px 30px" }, type: "warning", description: (0, jsx_runtime_1.jsxs)("ul", __assign({ style: { marginBottom: 0, fontSize: "11pt" } }, { children: [(0, jsx_runtime_1.jsxs)("li", { children: [" ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://doc.cocalc.com/howto/missing-project.html" }, { children: "My file or project is gone?" })), " "] }), (0, jsx_runtime_1.jsxs)("li", { children: [" ", "My Jupyter notebook or SageMath worksheet is", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://doc.cocalc.com/howto/slow-worksheet.html" }, { children: "slow" })), " ", "or", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://doc.cocalc.com/howto/jupyter-kernel-terminated.html" }, { children: "crashing" })), "?", " "] }), (0, jsx_runtime_1.jsxs)("li", { children: [" ", "I have", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://doc.cocalc.com/howto/sage-question.html" }, { children: "questions about SageMath." }))] }), (0, jsx_runtime_1.jsxs)("li", { children: ["I just need to", " ", (0, jsx_runtime_1.jsx)(A_1.default, __assign({ href: "https://discord.gg/nEHs2GK" }, { children: "quickly chat" })), "."] })] })) })] }));
}
function Status(_a) {
    var done = _a.done;
    return ((0, jsx_runtime_1.jsx)(icon_1.Icon, { style: {
            color: done ? "green" : "red",
            fontWeight: "bold",
            fontSize: "12pt",
        }, name: done ? "check" : "arrow-right" }));
}
//# sourceMappingURL=create.js.map