"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 antd_1 = require("antd");
const database_1 = __importDefault(require("lib/hooks/database"));
const loading_1 = __importDefault(require("components/share/loading"));
const save_button_1 = __importDefault(require("components/misc/save-button"));
const post_1 = __importDefault(require("lib/api/post"));
const register_1 = __importDefault(require("../register"));
const api_1 = __importDefault(require("lib/hooks/api"));
const misc_1 = require("@cocalc/util/misc");
const emailDesc = `You can sign in using this email address and use it
to reset your password.  You
will also receive email notifications about chats and other activity.   People
can add you as collaborators to projects by searching for your exact
email address. (There is no way to do a search for partial email
addresses, and your email address is never revealed to other users.)`;
(0, register_1.default)({
    path: "account/email",
    title: "Email Address",
    icon: "paper-plane",
    desc: "Change your email address.",
    search: emailDesc,
    Component: () => {
        const hasPassword = (0, api_1.default)("auth/has-password");
        const { loading, value } = (0, database_1.default)({
            accounts: { email_address: null },
        });
        const [password, setPassword] = (0, react_1.useState)("");
        const [lastSuccess, setLastSuccess] = (0, react_1.useState)("");
        const [original, setOriginal] = (0, react_1.useState)(undefined);
        const [edited, setEdited] = (0, react_1.useState)(undefined);
        (0, react_1.useEffect)(() => {
            if (!loading && original === undefined && value.accounts != null) {
                setOriginal(value.accounts);
                setEdited(value.accounts);
            }
        }, [loading]);
        function onChange(field) {
            return (e) => setEdited({ ...edited, [field]: e.target.value });
        }
        if (original == null || edited == null || !hasPassword.result) {
            return (0, jsx_runtime_1.jsx)(loading_1.default, {});
        }
        return ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)("form", { children: (0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", style: { width: "100%", maxWidth: "500px" }, children: [(0, jsx_runtime_1.jsx)("b", { children: "Your email address" }), " ", emailDesc, (0, jsx_runtime_1.jsx)(antd_1.Input, { addonBefore: "Email address", defaultValue: original.email_address, onChange: onChange("email_address") }), (0, jsx_runtime_1.jsx)(antd_1.Input.Password, { addonBefore: hasPassword.result.hasPassword
                                ? "Current password"
                                : "Choose a password", onChange: (e) => setPassword(e.target.value) }), (0, jsx_runtime_1.jsx)(save_button_1.default, { disabled: password.length < 6 ||
                                !(0, misc_1.is_valid_email_address)(edited.email_address ?? "") ||
                                lastSuccess == password + (edited.email_address ?? ""), edited: edited, original: original, setOriginal: setOriginal, onSave: async ({ email_address }) => {
                                await (0, post_1.default)("/accounts/set-email-address", {
                                    email_address,
                                    password,
                                });
                                setLastSuccess(password + email_address);
                            }, isValid: () => password.length >= 6 }), lastSuccess == password + edited.email_address && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { showIcon: true, type: "success", message: "Email address and password successfully saved." }))] }) }) }));
    },
});
//# sourceMappingURL=email.js.map