"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VerifyEmailForm = void 0;
const contexts_1 = require("../../../contexts/index.js");
const hooks_1 = require("../../../hooks/index.js");
const react_1 = __importStar(require("react"));
const react_hook_form_1 = require("react-hook-form");
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const BEButton_1 = __importDefault(require("../../generic/BEButton"));
const Icon_1 = require("../../generic/Icon/Icon");
const Text_1 = __importStar(require("../../generic/Text"));
const TextBox_1 = __importDefault(require("../../generic/TextBox"));
const Color_1 = require("../../../util/Color");
const Validation_1 = require("../../../util/Validation");
const VerifyEmailForm = (props) => {
    const { t } = (0, react_i18next_1.useTranslation)();
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { loading, confirmAccount } = (0, react_1.useContext)(contexts_1.AuthenticationContext);
    const [isLoading, setIsLoading] = (0, react_1.useState)(false);
    const [, setApiErrorMessages] = (0, react_1.useState)('');
    const { register, formState, handleSubmit } = (0, react_hook_form_1.useForm)({
        mode: 'onSubmit',
    });
    const errors = formState.errors;
    const getValidationClass = (name) => {
        if (errors && formState.touchedFields[name]) {
            if (errors[name]) {
                return 'error';
            }
            else {
                return 'success';
            }
        }
    };
    const onSubmit = (values) => {
        setIsLoading(true);
        setApiErrorMessages('');
        confirmAccount(values.code, { hotel })
            .then((state) => {
            var _a, _b;
            if (state === contexts_1.CognitoLoginState.Login) {
                (_a = props.onSignIn) === null || _a === void 0 ? void 0 : _a.call(props);
            }
            else {
                (_b = props.onDone) === null || _b === void 0 ? void 0 : _b.call(props);
            }
        })
            .finally(() => {
            setIsLoading(false);
        });
    };
    return (react_1.default.createElement("form", { onSubmit: handleSubmit(onSubmit), className: "u-marg-bottom" },
        react_1.default.createElement("div", { className: "u-marg-bottom" },
            react_1.default.createElement(Text_1.default, { color: Color_1.Color.Success, className: "u-marg-bottom--light", bold: true }, t(Translation_1.Translation.Navigation.SignUp.VerifyingEmail.Title)),
            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, t(Translation_1.Translation.Navigation.SignUp.VerifyingEmail.Detail))),
        react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
            react_1.default.createElement("div", { className: "u-marg-bottom--light" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Label, color: Color_1.Color.Navy, bold: true }, t(Translation_1.Translation.Navigation.ForgotPassword.Inputs.Code.Label))),
            react_1.default.createElement(TextBox_1.default, Object.assign({ placeholder: "12345", validationStatus: getValidationClass('code') }, register('code', {
                required: (0, Validation_1.makeRequiredRule)(t)(true, 'Code'),
            })))),
        react_1.default.createElement(BEButton_1.default, { wide: true, icon: Icon_1.IconType.ArrowRight, iconPosition: "right", filled: true, primary: true, name: "SignUpButton", htmlType: "submit", isLoading: loading || isLoading, disabled: isLoading || loading }, t(Translation_1.Translation.Navigation.SignUp.Inputs.SignUpButton))));
};
exports.VerifyEmailForm = VerifyEmailForm;
//# sourceMappingURL=VerifyEmailForm.js.map