"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.StepRoomErrorForm = void 0;
const contexts_1 = require("../../../contexts/index.js");
const dayjs_1 = __importDefault(require("dayjs"));
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 AutoAutoHeight_1 = __importDefault(require("../../../animations/AutoAutoHeight"));
const BookingAPI_1 = __importDefault(require("../../../api/BookingAPI"));
const Alert_1 = __importStar(require("../../generic/Alert"));
const BEButton_1 = __importDefault(require("../../generic/BEButton"));
const Headline_1 = __importDefault(require("../../generic/Headline"));
const Icon_1 = __importStar(require("../../generic/Icon/Icon"));
const Text_1 = __importStar(require("../../generic/Text"));
const TextBox_1 = __importDefault(require("../../generic/TextBox"));
const CurrentHotelHook_1 = require("../../../hooks/CurrentHotelHook");
const Color_1 = require("../../../util/Color");
const Validation_1 = require("../../../util/Validation");
const StepRoomErrorForm = () => {
    const { register, handleSubmit, formState } = (0, react_hook_form_1.useForm)();
    const { hotel } = (0, CurrentHotelHook_1.useCurrentHotel)();
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const { t } = (0, react_i18next_1.useTranslation)();
    const [apiStatus, setApiStatus] = (0, react_1.useState)({});
    const [isLoading, setIsLoading] = (0, react_1.useState)(false);
    const onSubmit = (values) => __awaiter(void 0, void 0, void 0, function* () {
        setApiStatus({});
        setIsLoading(true);
        try {
            const br = basketContext.selectedBasketRow;
            let searchQuery = '';
            [
                { name: 'Adults', value: br.getAdults() },
                { name: 'Children', value: br.getChildren() },
                { name: 'Infants', value: br.getInfants() },
                { name: 'Promo Code', value: br.getPromoCode() },
                { name: 'Dates range', value: br.getStayDateRange('ddd D MMM YYYY', ' till ') },
                { name: 'Date Sent', value: (0, dayjs_1.default)().format('dd D MMM YYYY') },
            ].forEach(({ name, value }) => {
                if (value) {
                    searchQuery += `${name}: ${value}\n`;
                }
            });
            yield BookingAPI_1.default.Error.sendRoomFallbackForm(hotel === null || hotel === void 0 ? void 0 : hotel.hotelID, {
                name: values.name,
                telephone: values.telephone,
                email: values.email,
                searchQuery,
            });
            setApiStatus({ success: true });
        }
        catch (ex) {
            setApiStatus({ success: false, message: t(Translation_1.Translation.Step.Room.ErrorForm.APIError) });
        }
        setIsLoading(false);
    });
    return (react_1.default.createElement("div", { className: "u-fill-white u-rounded" },
        react_1.default.createElement("div", { className: "row u-pad--heavy" },
            react_1.default.createElement("div", { className: "col-lg-6 d-flex justify-content-center flex-column" },
                react_1.default.createElement("div", { className: "u-marg-bottom--light" },
                    react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Error, color: Color_1.Color.Alert, size: "24px" })),
                react_1.default.createElement("div", { className: "u-marg-bottom--light" },
                    react_1.default.createElement(Headline_1.default, { bold: true, color: Color_1.Color.Alert },
                        react_1.default.createElement("span", { style: { color: 'inherit' }, dangerouslySetInnerHTML: { __html: t(Translation_1.Translation.Step.Room.ErrorForm.Title) } }))),
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small },
                    react_1.default.createElement("span", { dangerouslySetInnerHTML: { __html: t(Translation_1.Translation.Step.Room.ErrorForm.Description) } }))),
            react_1.default.createElement("div", { className: "col-lg-6" },
                react_1.default.createElement("form", null,
                    react_1.default.createElement(AutoAutoHeight_1.default, { open: !!formState.errors }, Object.values(formState.errors).map((error, index) => {
                        return (react_1.default.createElement(Alert_1.default, { key: index, type: Alert_1.AlertType.Danger },
                            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, error === null || error === void 0 ? void 0 : error.message)));
                    })),
                    react_1.default.createElement("div", { className: "u-marg-bottom--light u-marg-top" },
                        react_1.default.createElement(TextBox_1.default, Object.assign({ placeholder: t(Translation_1.Translation.Step.Room.ErrorForm.Inputs.Name) }, register('name', {
                            required: {
                                value: true,
                                message: `${t(Translation_1.Translation.Step.Room.ErrorForm.Inputs.Name)} is a required field`,
                            },
                        })))),
                    react_1.default.createElement("div", { className: "u-marg-bottom--light" },
                        react_1.default.createElement(TextBox_1.default, Object.assign({ placeholder: t(Translation_1.Translation.Step.Room.ErrorForm.Inputs.Email) }, register('email', {
                            required: {
                                value: true,
                                message: `${t(Translation_1.Translation.Step.Room.ErrorForm.Inputs.Email)} is a required field`,
                            },
                            pattern: {
                                value: Validation_1.EMAIL_REGEX,
                                message: 'Email must be valid',
                            },
                        })))),
                    react_1.default.createElement("div", { className: "u-marg-bottom--light" },
                        react_1.default.createElement(TextBox_1.default, Object.assign({ placeholder: t(Translation_1.Translation.Step.Room.ErrorForm.Inputs.Telephone) }, register('telephone', {
                            required: {
                                value: true,
                                message: `${t(Translation_1.Translation.Step.Room.ErrorForm.Inputs.Telephone)} is a required field`,
                            },
                        })))),
                    react_1.default.createElement(BEButton_1.default, { filled: true, primary: true, onClick: handleSubmit(onSubmit), isLoading: isLoading, disabled: isLoading || apiStatus.success }, t(Translation_1.Translation.Step.Room.ErrorForm.ContactUsButton)),
                    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.StepRoomErrorForm = StepRoomErrorForm;
//# sourceMappingURL=StepRoomErrorForm.js.map