"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.InlineSignUpSection = void 0;
const contexts_1 = require("../../contexts/index.js");
const react_1 = __importStar(require("react"));
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 BEButton_1 = __importDefault(require("../generic/BEButton"));
const Headline_1 = __importDefault(require("../generic/Headline"));
const Text_1 = __importStar(require("../generic/Text"));
const ForgotPasswordForm_1 = require("../User/Forms/ForgotPasswordForm");
const SignInForm_1 = require("../User/Forms/SignInForm");
const SignUpForm_1 = require("../User/Forms/SignUpForm");
const MemberPortalFeature_1 = __importDefault(require("../../providers/feature/MemberPortalFeature"));
const Color_1 = require("../../util/Color");
var EViewingScreen;
(function (EViewingScreen) {
    EViewingScreen["Initial"] = "Initial";
    EViewingScreen["SignUp"] = "SignUp";
    EViewingScreen["SignIn"] = "SignIn";
    EViewingScreen["ForgotPassword"] = "ForgotPassword";
})(EViewingScreen || (EViewingScreen = {}));
const InlineSignUpSection = (props) => {
    const { reservation, hotelId } = props;
    const { t } = (0, react_i18next_1.useTranslation)();
    const { reset: cognitoReset } = (0, react_1.useContext)(contexts_1.AuthenticationContext);
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { roomstayMember } = (0, react_1.useContext)(contexts_1.RoomstayMemberContext);
    const [viewState, setViewState] = (0, react_1.useState)(EViewingScreen.Initial);
    if (!MemberPortalFeature_1.default.isActive() || roomstayMember) {
        return null;
    }
    // If the user registered an internal account during signup, use those details, otherwise, use details from the reservation
    const userDetailsForSignUp = ccx.signedInUser || (reservation === null || reservation === void 0 ? void 0 : reservation.guest);
    if (!(userDetailsForSignUp === null || userDetailsForSignUp === void 0 ? void 0 : userDetailsForSignUp.firstName) || !(userDetailsForSignUp === null || userDetailsForSignUp === void 0 ? void 0 : userDetailsForSignUp.lastName) || !(userDetailsForSignUp === null || userDetailsForSignUp === void 0 ? void 0 : userDetailsForSignUp.email)) {
        return null;
    }
    const linkBookingToMember = () => __awaiter(void 0, void 0, void 0, function* () {
        try {
            yield BookingAPI_1.default.Reservation.linkBookingToMember(hotelId, reservation.reservationNumber, reservation.guest.email);
        }
        catch (error) {
            console.error('Failed to link reservation:', error);
        }
    });
    const onSuccess = () => __awaiter(void 0, void 0, void 0, function* () {
        yield linkBookingToMember();
    });
    const onCreateAccountClick = () => {
        setViewState(EViewingScreen.SignUp);
        cognitoReset();
    };
    const showSignIn = () => {
        setViewState(EViewingScreen.SignIn);
        cognitoReset();
    };
    const showForgotPassword = () => {
        setViewState(EViewingScreen.ForgotPassword);
        cognitoReset();
    };
    return (react_1.default.createElement("div", { className: "u-marg-bottom--massive w-100" },
        react_1.default.createElement(AutoAutoHeight_1.default, { open: viewState === EViewingScreen.Initial, startOpen: true },
            react_1.default.createElement("div", { className: "u-align-center" },
                react_1.default.createElement("div", { className: "u-marg-bottom" },
                    react_1.default.createElement(Headline_1.default, { size: "large", color: Color_1.Color.Navy }, t(Translation_1.Translation.Navigation.SignUp.Title))),
                react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Body, color: Color_1.Color.DarkGrey }, t(Translation_1.Translation.Navigation.SignIn.GetLowerPrices))),
                react_1.default.createElement("div", { className: "u-marg-bottom d-flex justify-content-center" },
                    react_1.default.createElement(BEButton_1.default, { primary: true, filled: true, center: true, onClick: onCreateAccountClick }, t(Translation_1.Translation.Navigation.SignIn.SignUp))))),
        react_1.default.createElement(AutoAutoHeight_1.default, { open: viewState === EViewingScreen.SignUp },
            react_1.default.createElement("div", { className: "u-fill-white u-pad--heavy w-100" },
                react_1.default.createElement(SignUpForm_1.SignUpForm, { onDone: onSuccess, onSignIn: showSignIn, defaultValues: {
                        email: userDetailsForSignUp.email,
                        firstName: userDetailsForSignUp.firstName,
                        lastName: userDetailsForSignUp.lastName,
                    } }))),
        react_1.default.createElement(AutoAutoHeight_1.default, { open: viewState === EViewingScreen.SignIn },
            react_1.default.createElement("div", { className: "u-fill-white u-pad--heavy w-100" },
                react_1.default.createElement(SignInForm_1.SignInForm, { onForgotPassword: showForgotPassword, onSignedIn: onSuccess }))),
        react_1.default.createElement(AutoAutoHeight_1.default, { open: viewState === EViewingScreen.ForgotPassword },
            react_1.default.createElement("div", { className: "u-fill-white u-pad--heavy w-100" },
                react_1.default.createElement(ForgotPasswordForm_1.ForgotPasswordForm, { onCancelled: showSignIn, onPasswordReset: onSuccess })))));
};
exports.InlineSignUpSection = InlineSignUpSection;
//# sourceMappingURL=InlineSignUpSection.js.map