"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.AccountChangePasswordPage = 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 react_router_dom_1 = require("react-router-dom");
const Translation_1 = require("translations/Translation");
const BEButton_1 = __importDefault(require("../../../components/generic/BEButton"));
const Headline_1 = __importDefault(require("../../../components/generic/Headline"));
const Icon_1 = require("../../../components/generic/Icon/Icon");
const PasswordBox_1 = require("../../../components/generic/PasswordBox/PasswordBox");
const Text_1 = __importStar(require("../../../components/generic/Text"));
const Color_1 = require("../../../util/Color");
const Validation_1 = require("../../../util/Validation");
const AutoAutoHeight_1 = __importDefault(require("../../../animations/AutoAutoHeight"));
const Alert_1 = __importStar(require("../../../components/generic/Alert"));
const AccountRoutes_1 = require("../AccountRoutes");
const LabelAndInputGroup = (props) => {
    const { label, component, hint, error } = props;
    return (react_1.default.createElement("div", { className: "row u-marg-bottom--heavy" },
        react_1.default.createElement("div", { className: "col-md-4" },
            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, bold: true }, label)),
        react_1.default.createElement("div", { className: "col-md-8" },
            component,
            !!error && (react_1.default.createElement("p", { className: "text-small", style: { color: '#ED4362' } }, error.message)),
            !!hint && (react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Label, color: Color_1.Color.Grey }, hint)))));
};
const AccountChangePasswordPage = () => {
    const { loading: isLoadingMember, changePassword } = (0, react_1.useContext)(contexts_1.AuthenticationContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { t } = (0, react_i18next_1.useTranslation)();
    const { trigger, watch, register, formState: { errors, isDirty }, handleSubmit, } = (0, react_hook_form_1.useForm)({
        mode: 'onChange',
        reValidateMode: 'onChange',
        defaultValues: {
            currentPassword: '',
            newPassword: '',
            confirmPassword: '',
        },
    });
    const newPasswordWatch = watch('newPassword');
    (0, react_1.useEffect)(() => {
        trigger('confirmPassword');
    }, [newPasswordWatch, trigger]);
    const getErrorValidationClass = (name) => (errors[name] ? 'error' : undefined);
    const [alert, setAlert] = (0, react_1.useState)(null);
    const notificationMap = {
        [contexts_1.CognitoLoginState.Success]: {
            type: Alert_1.AlertType.Success,
            icon: Icon_1.IconType.Check,
            message: t(Translation_1.Translation.UserPortal.ChangePassword.ChangePasswordSuccessfully),
        },
        [contexts_1.CognitoLoginState.NotAuthorizedException]: {
            type: Alert_1.AlertType.Danger,
            icon: Icon_1.IconType.Error,
            message: t(Translation_1.Translation.UserPortal.ChangePassword.NotAuthorizedException),
        },
        [contexts_1.CognitoLoginState.LimitExceededException]: {
            type: Alert_1.AlertType.Danger,
            icon: Icon_1.IconType.Error,
            message: t(Translation_1.Translation.UserPortal.ChangePassword.LimitExceededException),
        },
    };
    const onChangePassword = ({ currentPassword, newPassword }) => {
        setAlert(null);
        changePassword === null || changePassword === void 0 ? void 0 : changePassword(currentPassword, newPassword, { hotel }).then((state) => {
            if (state) {
                const notification = notificationMap[state];
                if (notification) {
                    setAlert(notification);
                }
            }
        });
    };
    const saveButtonDisabled = !isDirty || isLoadingMember || !!Object.keys(errors).length;
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "u-marg-top--heavy@m u-marg-top@- u-marg-bottom--heavy" },
            react_1.default.createElement(react_router_dom_1.Link, { to: AccountRoutes_1.AccountRoutes.ProfileDetails.path },
                react_1.default.createElement(BEButton_1.default, { isText: true, icon: Icon_1.IconType.ArrowLeft, size: "small" }, t(Translation_1.Translation.UserPortal.ChangePassword.BackToProfile)))),
        react_1.default.createElement("div", { className: "u-marg-bottom--heavy@m" },
            react_1.default.createElement(Headline_1.default, { size: "extra-large", bold: true }, t(Translation_1.Translation.Navigation.Menu.ChangePassword))),
        react_1.default.createElement("div", { className: "u-marg-bottom--heavy@m" },
            react_1.default.createElement(AutoAutoHeight_1.default, { open: !!alert },
                react_1.default.createElement("div", { className: "row" },
                    react_1.default.createElement("div", { className: "col-md-8" },
                        react_1.default.createElement(Alert_1.default, { type: alert === null || alert === void 0 ? void 0 : alert.type, icon: alert === null || alert === void 0 ? void 0 : alert.icon },
                            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, alert === null || alert === void 0 ? void 0 : alert.message)))))),
        react_1.default.createElement("form", { onSubmit: handleSubmit(onChangePassword) },
            react_1.default.createElement("div", { className: "row" },
                react_1.default.createElement("div", { className: "col-md-8" },
                    react_1.default.createElement("div", null,
                        react_1.default.createElement(LabelAndInputGroup, { label: t(Translation_1.Translation.UserPortal.ChangePassword.Field.CurrentPassword), component: react_1.default.createElement(PasswordBox_1.PasswordBox, Object.assign({ placeholder: "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022" }, register('currentPassword', {
                                pattern: (0, Validation_1.makePasswordPatternRule)(t),
                                required: (0, Validation_1.makeRequiredRule)(t)(true, t(Translation_1.Translation.UserPortal.ChangePassword.Field.CurrentPassword)),
                            }), { validationStatus: getErrorValidationClass('currentPassword') })), error: errors.currentPassword }),
                        react_1.default.createElement(LabelAndInputGroup, { label: t(Translation_1.Translation.UserPortal.ChangePassword.Field.NewPassword), component: react_1.default.createElement(PasswordBox_1.PasswordBox, Object.assign({ placeholder: "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022" }, register('newPassword', {
                                pattern: (0, Validation_1.makePasswordPatternRule)(t),
                                required: (0, Validation_1.makeRequiredRule)(t)(true, t(Translation_1.Translation.UserPortal.ChangePassword.Field.NewPassword)),
                            }), { validationStatus: getErrorValidationClass('newPassword') })), error: errors.newPassword }),
                        react_1.default.createElement(LabelAndInputGroup, { label: t(Translation_1.Translation.UserPortal.ChangePassword.Field.ConfirmPassword), component: react_1.default.createElement(PasswordBox_1.PasswordBox, Object.assign({ placeholder: "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022" }, register('confirmPassword', {
                                required: (0, Validation_1.makeRequiredRule)(t)(!!newPasswordWatch, t(Translation_1.Translation.Step.Confirmation.Inputs.ConfirmPassword)),
                                validate: {
                                    isSame: (0, Validation_1.isPasswordMatch)(t)(newPasswordWatch),
                                },
                            }), { validationStatus: getErrorValidationClass('confirmPassword') })), error: errors.confirmPassword })))),
            react_1.default.createElement("div", { className: "row" },
                react_1.default.createElement("div", { className: "col-md-8" },
                    react_1.default.createElement("div", { className: "u-flex justify-content-start u-marg-top--heavy" },
                        react_1.default.createElement(react_router_dom_1.Link, { to: "/account/home", className: "u-marg-right" },
                            react_1.default.createElement(BEButton_1.default, null, t(Translation_1.Translation.Misc.Cancel))),
                        react_1.default.createElement(BEButton_1.default, { disabled: saveButtonDisabled, isLoading: isLoadingMember, primary: true, filled: true, htmlType: "submit" }, t(Translation_1.Translation.Misc.Save))))))));
};
exports.AccountChangePasswordPage = AccountChangePasswordPage;
//# sourceMappingURL=AccountChangePasswordPage.js.map