"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 __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountDetailsProfilePage = void 0;
const contexts_1 = require("../../../contexts/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 EnhancedPhoneNumberField_1 = __importDefault(require("../../../components/generic/custom/EnhancedPhoneNumberField/EnhancedPhoneNumberField"));
const Headline_1 = __importDefault(require("../../../components/generic/Headline"));
const Icon_1 = require("../../../components/generic/Icon/Icon");
const LineBreak_1 = __importDefault(require("../../../components/generic/LineBreak"));
const Text_1 = __importStar(require("../../../components/generic/Text"));
const TextBox_1 = __importDefault(require("../../../components/generic/TextBox"));
const StepConfirmationCountrySelector_1 = __importDefault(require("../../../components/steps/confirmation/StepConfirmationCountrySelector"));
const Color_1 = require("../../../util/Color");
const Validation_1 = require("../../../util/Validation");
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 AccountDetailsProfilePage = () => {
    const { roomstayMember, loading: isLoadingMember, updateMemberDetails } = (0, react_1.useContext)(contexts_1.RoomstayMemberContext);
    const notifications = (0, react_1.useContext)(contexts_1.NotificationContext);
    const { t } = (0, react_i18next_1.useTranslation)();
    const { register, formState: { errors, isDirty }, handleSubmit, reset: resetFormValues, control, } = (0, react_hook_form_1.useForm)({
        mode: 'onChange',
    });
    const getErrorValidationClass = (name) => (errors[name] ? 'error' : undefined);
    (0, react_1.useEffect)(() => {
        if (roomstayMember) {
            resetFormValues(roomstayMember);
        }
    }, [roomstayMember]);
    const onSaveDetails = (values) => {
        updateMemberDetails === null || updateMemberDetails === void 0 ? void 0 : updateMemberDetails(values).then(() => {
            notifications.addNotification({
                icon: Icon_1.IconType.Check,
                children: react_1.default.createElement(Text_1.default, null, t(Translation_1.Translation.UserPortal.ProfileScreen.DetailsSavedSuccessfully)),
            });
        });
    };
    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.Home.path },
                react_1.default.createElement(BEButton_1.default, { isText: true, icon: Icon_1.IconType.ArrowLeft, size: "small" }, t(Translation_1.Translation.Reservation.BackToYourAccount)))),
        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.ProfileInformation))),
        react_1.default.createElement("form", { onSubmit: handleSubmit(onSaveDetails) },
            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.Step.Confirmation.Inputs.Forename), component: react_1.default.createElement(TextBox_1.default, Object.assign({ validationStatus: getErrorValidationClass('forename') }, register('forename', {
                                required: (0, Validation_1.makeRequiredRule)(t)(true, t(Translation_1.Translation.Step.Confirmation.Inputs.Forename)),
                            }))), error: errors.forename }),
                        react_1.default.createElement(LabelAndInputGroup, { label: t(Translation_1.Translation.Step.Confirmation.Inputs.Surname), component: react_1.default.createElement(TextBox_1.default, Object.assign({ validationStatus: getErrorValidationClass('surname') }, register('surname', {
                                required: (0, Validation_1.makeRequiredRule)(t)(true, t(Translation_1.Translation.Step.Confirmation.Inputs.Surname)),
                            }))), error: errors.surname }),
                        react_1.default.createElement(LabelAndInputGroup, { label: t(Translation_1.Translation.Step.Confirmation.Inputs.Email), component: react_1.default.createElement(TextBox_1.default, { validationStatus: getErrorValidationClass('email'), inputProps: { disabled: true }, value: roomstayMember === null || roomstayMember === void 0 ? void 0 : roomstayMember.email }), error: errors.email }),
                        react_1.default.createElement(LabelAndInputGroup, { label: t(Translation_1.Translation.Step.Confirmation.Inputs.Phone), component: react_1.default.createElement(react_hook_form_1.Controller, { rules: {
                                    pattern: (0, Validation_1.makePhonePatternRule)(t),
                                }, control: control, name: "phoneNumber", render: ({ field }) => {
                                    return (react_1.default.createElement(EnhancedPhoneNumberField_1.default, Object.assign({}, field, { onChange: (e) => {
                                            field.onChange(e);
                                        }, validationStatus: getErrorValidationClass('phoneNumber') })));
                                } }), error: errors.phoneNumber }),
                        react_1.default.createElement(LineBreak_1.default, null),
                        react_1.default.createElement(LabelAndInputGroup, { label: t(Translation_1.Translation.Step.Confirmation.Inputs.Address), component: react_1.default.createElement(TextBox_1.default, Object.assign({}, register('address.address'))) }),
                        react_1.default.createElement(LabelAndInputGroup, { label: t(Translation_1.Translation.Step.Confirmation.Inputs.City), component: react_1.default.createElement(TextBox_1.default, Object.assign({}, register('address.city'))) }),
                        react_1.default.createElement(LabelAndInputGroup, { label: t(Translation_1.Translation.Step.Confirmation.Inputs.PostCode), component: react_1.default.createElement(TextBox_1.default, Object.assign({}, register('address.zip'))) }),
                        react_1.default.createElement(LabelAndInputGroup, { label: t(Translation_1.Translation.Step.Confirmation.Inputs.Country), component: react_1.default.createElement(react_hook_form_1.Controller, { control: control, name: "address.country", render: ({ field }) => {
                                    const { ref } = field, rest = __rest(field, ["ref"]);
                                    return react_1.default.createElement(StepConfirmationCountrySelector_1.default, Object.assign({}, rest));
                                } }) })))),
            react_1.default.createElement("div", { className: "row" },
                react_1.default.createElement("div", { className: "col-md-8" },
                    react_1.default.createElement("div", { className: "u-flex u-marg-top--heavy" },
                        react_1.default.createElement("div", { className: "u-flex" },
                            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", className: "u-marg-right" }, t(Translation_1.Translation.Misc.SaveAllChanges))),
                        react_1.default.createElement("div", { className: "u-flex-grow" },
                            react_1.default.createElement(react_router_dom_1.Link, { to: AccountRoutes_1.AccountRoutes.ChangePassword.path },
                                react_1.default.createElement(BEButton_1.default, null, t(Translation_1.Translation.Navigation.Menu.ChangePassword))))))))));
};
exports.AccountDetailsProfilePage = AccountDetailsProfilePage;
//# sourceMappingURL=AccountDetailsProfilePage.js.map