"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetUpdates = exports.submitHandler = exports.modes = void 0;
const react_1 = __importDefault(require("react"));
const Button_1 = require("../button/Button");
const Input_1 = require("../input/Input");
exports.modes = {
    // display prompt / email input / submit button
    GET: Symbol('get'),
    // display success message / done
    SUCCESS: Symbol('success'),
    // display error message / try again button
    ERROR: Symbol('error'),
};
const submitHandler = (subscribe, mode, setMode, value) => e => {
    e.preventDefault();
    switch (mode) {
        case exports.modes.SUCCESS:
            break;
        case exports.modes.ERROR:
            setMode(exports.modes.GET);
            break;
        case exports.modes.GET:
        default:
            subscribe(value, result => {
                if (result.data) {
                    setMode(result.data.subscribeToNewsletter ? exports.modes.SUCCESS : exports.modes.ERROR);
                }
            });
            break;
    }
};
exports.submitHandler = submitHandler;
const getText = mode => {
    switch (mode) {
        case exports.modes.ERROR:
            return {
                headingText: 'Whoops.',
                messageText: 'We were unable to sign you up for updates. Please try again.',
                buttonText: 'Try Again',
            };
        case exports.modes.SUCCESS:
            return {
                headingText: 'Success!',
                messageText: 'You are now signed up to receive news updates.',
                buttonText: 'Done',
            };
        case exports.modes.GET:
        default:
            return {
                headingText: 'Get Updates',
                buttonText: 'Subscribe',
                messageText: 'Find out about new version and future releases',
            };
    }
};
const GetUpdates = ({ subscribe, initialMode = exports.modes.GET }) => {
    const [mode, setMode] = react_1.default.useState(initialMode);
    const [value, setValue] = react_1.default.useState('');
    const { headingText, messageText, buttonText } = getText(mode);
    return (react_1.default.createElement("div", { className: "get-updates-bar" },
        react_1.default.createElement("div", { className: "get-updates-label" },
            react_1.default.createElement("h2", null, headingText),
            react_1.default.createElement("h3", null, messageText)),
        react_1.default.createElement("form", { onSubmit: (0, exports.submitHandler)(subscribe, mode, setMode, value), className: "get-updates-form" },
            react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement("div", { className: "" }),
                mode === exports.modes.GET && (react_1.default.createElement(Input_1.Input, { type: "email", label: "Email Address", placeholder: "email@openneuro.com", labelStyle: "float", name: "float-example", value: value, setValue: setValue })),
                react_1.default.createElement(Button_1.Button, { type: "submit", size: "small", secondary: true, label: buttonText, disabled: mode === exports.modes.SUCCESS })))));
};
exports.GetUpdates = GetUpdates;
