"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");
const react_1 = require("react");
const register_1 = __importDefault(require("../register"));
const antd_1 = require("antd");
const api_1 = __importDefault(require("lib/hooks/api"));
const loading_1 = __importDefault(require("components/share/loading"));
const post_1 = __importDefault(require("lib/api/post"));
const A_1 = __importDefault(require("components/misc/A"));
(0, register_1.default)({
    path: "account/api",
    title: "API Key",
    icon: "key",
    desc: "View, create, remove or regenerate the API key for accessing your account.  If you want API access only to a specific list of projects, create a new CoCalc account, add that account as a collaborator to those projects, and create an API key for that account.",
    Component: () => {
        const [validPassword, setValidPassword] = (0, react_1.useState)("");
        const [password, setPassword] = (0, react_1.useState)("");
        const { result, error } = (0, api_1.default)("auth/has-password");
        const [apiError, setApiError] = (0, react_1.useState)("");
        const [apiKey, setApiKey] = (0, react_1.useState)("");
        const [showApi, setShowApi] = (0, react_1.useState)(false);
        const [loading, setLoading] = (0, react_1.useState)(false);
        if (!result)
            return (0, jsx_runtime_1.jsx)(loading_1.default, {});
        if (error) {
            // e.g., not signed in or database down...
            return (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error, showIcon: true });
        }
        if (loading) {
            return (0, jsx_runtime_1.jsx)(loading_1.default, {});
        }
        async function submitPassword(password) {
            try {
                setLoading(true);
                setApiError("");
                const { api_key } = await (0, post_1.default)("api-key", {
                    action: "get",
                    password,
                });
                setValidPassword(password);
                setApiKey(api_key);
            }
            catch (err) {
                setApiError(err.message);
            }
            finally {
                setLoading(false);
            }
        }
        async function apiAction(action) {
            try {
                setLoading(true);
                setApiError("");
                const { api_key } = await (0, post_1.default)("api-key", {
                    action,
                    password: validPassword,
                });
                setApiKey(api_key);
            }
            catch (err) {
                setApiError(err.message);
            }
            finally {
                setLoading(false);
            }
        }
        if (result.hasPassword && !validPassword) {
            return ((0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", children: [apiError && (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: apiError, showIcon: true }), (0, jsx_runtime_1.jsx)(antd_1.Input.Password, { value: password, style: { maxWidth: "50ex" }, placeholder: "Enter your password...", onChange: (e) => setPassword(e.target.value), onPressEnter: () => {
                            if (password.length >= 6) {
                                submitPassword(password);
                            }
                        } }), (0, jsx_runtime_1.jsx)(antd_1.Button, { disabled: password.length < 6, type: "primary", onClick: () => submitPassword(password), children: "Show API Key" })] }));
        }
        if (!result.hasPassword && !showApi) {
            return ((0, jsx_runtime_1.jsx)(antd_1.Button, { type: "primary", onClick: () => {
                    setShowApi(true);
                    apiAction("get");
                }, children: "Show API Key" }));
        }
        let body;
        if (apiKey) {
            body = ((0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", children: ["API Key:", (0, jsx_runtime_1.jsx)(antd_1.Input, { value: apiKey, readOnly: true, style: { width: "60ex" } }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(A_1.default, { href: "https://doc.cocalc.com/api/", children: "Learn about the API..." }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)(antd_1.Popconfirm, { title: (0, jsx_runtime_1.jsxs)("div", { style: { maxWidth: "50ex" }, children: ["Are you sure you want to delete your API key?", " ", (0, jsx_runtime_1.jsx)("b", { children: (0, jsx_runtime_1.jsx)("i", { children: "Anything using the current API key will stop working until you create and enter a new key." }) })] }), onConfirm: () => apiAction("delete"), okText: "Yes, delete my key", cancelText: "Cancel", children: (0, jsx_runtime_1.jsx)(antd_1.Button, { danger: true, children: "Delete API Key" }) })] }));
        }
        else {
            body = ((0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", children: ["You do not have an API key.", (0, jsx_runtime_1.jsx)(antd_1.Button, { onClick: () => apiAction("regenerate"), children: "Create API Key" })] }));
        }
        return ((0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", style: { width: "100%" }, children: [apiError && (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: apiError, showIcon: true }), body] }));
    },
});
//# sourceMappingURL=api.js.map