"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountHelpPage = void 0;
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 AutoAutoHeight_1 = __importDefault(require("../../../animations/AutoAutoHeight"));
const BookingAPI_1 = __importDefault(require("../../../api/BookingAPI"));
const Alert_1 = __importStar(require("../../../components/generic/Alert"));
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 Text_1 = __importStar(require("../../../components/generic/Text"));
const TextArea_1 = __importDefault(require("../../../components/generic/TextArea"));
const TextBox_1 = __importDefault(require("../../../components/generic/TextBox"));
const Color_1 = require("../../../util/Color");
const Validation_1 = require("../../../util/Validation");
const AccountRoutes_1 = require("../AccountRoutes");
const AccountHelpPage = () => {
    const { t } = (0, react_i18next_1.useTranslation)();
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { user, fullName } = (0, hooks_1.useMember)();
    const { register, formState, handleSubmit, reset, control } = (0, react_hook_form_1.useForm)({
        mode: 'onChange',
        reValidateMode: 'onBlur',
    });
    const [apiStatus, setApiStatus] = (0, react_1.useState)({});
    const [isLoading, setIsLoading] = (0, react_1.useState)(false);
    (0, react_1.useEffect)(() => {
        if (user) {
            reset({
                name: fullName,
                email: user.email,
                telephone: user.phoneNumber,
            });
        }
    }, [fullName, user]);
    const onSubmit = (values) => __awaiter(void 0, void 0, void 0, function* () {
        setApiStatus({});
        setIsLoading(true);
        try {
            yield BookingAPI_1.default.Error.sendMemberAreaContactForm(hotel === null || hotel === void 0 ? void 0 : hotel.hotelID, {
                name: values.name,
                telephone: values.telephone,
                email: values.email,
                message: values.message,
            });
            setApiStatus({ success: true });
        }
        catch (ex) {
            setApiStatus({ success: false, message: t(Translation_1.Translation.Step.Room.ErrorForm.APIError) });
        }
        setIsLoading(false);
    });
    const getValidationClass = (name) => {
        if (formState.errors) {
            if (formState.errors[name]) {
                return 'error';
            }
            else {
                return 'success';
            }
        }
    };
    return (react_1.default.createElement("div", null,
        react_1.default.createElement("div", { className: "u-marg-top--heavy@m u-marg-top@m- 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 u-pad-top@m" },
            react_1.default.createElement(Headline_1.default, { size: "extra-large", className: "u-marg-bottom" }, t(Translation_1.Translation.UserPortal.HelpScreen.NeedSomeHelp))),
        react_1.default.createElement("form", { onSubmit: handleSubmit(onSubmit), className: "u-marg-bottom", style: { maxWidth: 500 } },
            react_1.default.createElement(AutoAutoHeight_1.default, { open: !!formState.errors },
                react_1.default.createElement("div", { className: "u-flex flex-column" }, Object.keys(formState.errors).map((errorName, index) => {
                    var _a;
                    return (react_1.default.createElement(Alert_1.default, { type: Alert_1.AlertType.Danger, icon: Icon_1.IconType.Error, key: index },
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, (_a = formState.errors[errorName]) === null || _a === void 0 ? void 0 : _a.message)));
                }))),
            react_1.default.createElement("div", { className: "u-marg-bottom" },
                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.UserPortal.HelpScreen.Name))),
                react_1.default.createElement(TextBox_1.default, Object.assign({ required: true, validationStatus: getValidationClass('name') }, register('name', {
                    required: (0, Validation_1.makeRequiredRule)(t)(true, t(Translation_1.Translation.UserPortal.HelpScreen.Name)),
                })))),
            react_1.default.createElement("div", { className: "u-marg-bottom" },
                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.SignIn.EmailAddress))),
                react_1.default.createElement(TextBox_1.default, Object.assign({ placeholder: "example@mail.com", required: true, validationStatus: getValidationClass('email') }, register('email', {
                    required: (0, Validation_1.makeRequiredRule)(t)(true, t(Translation_1.Translation.Navigation.SignIn.EmailAddress)),
                    pattern: (0, Validation_1.makeEmailPatternRule)(t),
                })))),
            react_1.default.createElement("div", { className: "u-marg-bottom" },
                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.UserPortal.HelpScreen.Telephone))),
                react_1.default.createElement(react_hook_form_1.Controller, { rules: {
                        pattern: (0, Validation_1.makePhonePatternRule)(t),
                    }, control: control, name: "telephone", render: ({ field }) => {
                        return (react_1.default.createElement(EnhancedPhoneNumberField_1.default, Object.assign({}, field, { onChange: (e) => {
                                field.onChange(e);
                            }, validationStatus: getValidationClass('telephone') })));
                    } })),
            react_1.default.createElement("div", { className: "u-marg-bottom" },
                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.UserPortal.HelpScreen.Message))),
                react_1.default.createElement(TextArea_1.default, Object.assign({ required: true }, register('message', {
                    required: (0, Validation_1.makeRequiredRule)(t)(true, t(Translation_1.Translation.UserPortal.HelpScreen.Message)),
                })))),
            react_1.default.createElement(BEButton_1.default, { icon: Icon_1.IconType.ArrowRight, iconPosition: "right", filled: true, primary: true, htmlType: "submit", isLoading: isLoading, disabled: isLoading || apiStatus.success }, t(Translation_1.Translation.Misc.Submit))),
        react_1.default.createElement(AutoAutoHeight_1.default, { open: !!apiStatus.success },
            react_1.default.createElement("div", { className: "u-pad-top" },
                react_1.default.createElement(Text_1.default, { color: Color_1.Color.Success }, t(Translation_1.Translation.Step.Room.ErrorForm.ConfirmationMessage)))),
        react_1.default.createElement(AutoAutoHeight_1.default, { open: apiStatus.success === false },
            react_1.default.createElement("div", { className: "u-pad-top" },
                react_1.default.createElement(Text_1.default, { color: Color_1.Color.Alert }, apiStatus.message)))));
};
exports.AccountHelpPage = AccountHelpPage;
//# sourceMappingURL=AccountHelpPage.js.map