"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.default = MemberPortalSignInModal;
const contexts_1 = require("../../../contexts/index.js");
const events_1 = require("../../../events/index.js");
const hooks_1 = require("../../../hooks/index.js");
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const BEButton_1 = __importDefault(require("../../generic/BEButton"));
const Headline_1 = __importDefault(require("../../generic/Headline"));
const Icon_1 = __importStar(require("../../generic/Icon/Icon"));
const SimpleModal_1 = __importStar(require("../../generic/modal/SimpleModal"));
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 ImageProvider_1 = __importStar(require("../../../providers/ImageProvider"));
const Color_1 = require("../../../util/Color");
var EViewingScreen;
(function (EViewingScreen) {
    EViewingScreen["SignUp"] = "SignUp";
    EViewingScreen["SignIn"] = "SignIn";
    EViewingScreen["ForgotPassword"] = "ForgotPassword";
})(EViewingScreen || (EViewingScreen = {}));
function MemberPortalSignInModal(props) {
    var _a, _b, _c, _d;
    const { t } = (0, react_i18next_1.useTranslation)();
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { cognitoLoginState, reset: cognitoReset } = (0, react_1.useContext)(contexts_1.AuthenticationContext);
    const [viewingScreen, setViewingScreen] = (0, react_1.useState)(EViewingScreen.SignUp);
    const memberImage = (0, react_1.useMemo)(() => {
        if (props.image) {
            return props.image;
        }
        if (hotel === null || hotel === void 0 ? void 0 : hotel.heroImage) {
            return hotel === null || hotel === void 0 ? void 0 : hotel.heroImage;
        }
    }, [props.image, hotel === null || hotel === void 0 ? void 0 : hotel.heroImage]);
    const { raise } = (0, hooks_1.useEvent)();
    (0, react_1.useEffect)(() => {
        if (props.isOpen) {
            const event = new events_1.MemberOnlyModalViewEvent();
            raise(event);
        }
    }, [props.isOpen, raise]);
    (0, react_1.useEffect)(() => {
        if (!props.isOpen)
            return;
        if (cognitoLoginState === contexts_1.CognitoLoginState.RequiresConfirmation) {
            setViewingScreen(EViewingScreen.SignUp);
        }
    }, [props.isOpen, cognitoLoginState]);
    const handleSignInClick = () => {
        cognitoReset();
        setViewingScreen(EViewingScreen.SignIn);
    };
    const handleBackToSignUp = () => {
        cognitoReset();
        setViewingScreen(EViewingScreen.SignUp);
    };
    const handleSignInSuccess = () => {
        var _a;
        (_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props);
    };
    const handleSignUpSuccess = () => {
        var _a;
        (_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props);
    };
    const handleForgotPasswordClick = () => {
        cognitoReset();
        setViewingScreen(EViewingScreen.ForgotPassword);
    };
    const handleBackToSignIn = () => {
        cognitoReset();
        setViewingScreen(EViewingScreen.SignIn);
    };
    const handlePasswordResetSuccess = () => {
        var _a;
        (_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props);
    };
    const modalTitle = (_b = (_a = ccx === null || ccx === void 0 ? void 0 : ccx.memberOnlyModalPromotion) === null || _a === void 0 ? void 0 : _a.memberOnlyModalTitle) !== null && _b !== void 0 ? _b : t(Translation_1.Translation.MemberOnlyPopup.Headline, {
        companyShort: ccx === null || ccx === void 0 ? void 0 : ccx.companyShort,
    });
    const modalSubtitle = (_d = (_c = ccx === null || ccx === void 0 ? void 0 : ccx.memberOnlyModalPromotion) === null || _c === void 0 ? void 0 : _c.memberOnlyModalSubtitle) !== null && _d !== void 0 ? _d : t(Translation_1.Translation.MemberOnlyPopup.Subtitle);
    const modalPrimaryColor = (hotel === null || hotel === void 0 ? void 0 : hotel.memberOnlyModalUsePrimaryColor) ? Color_1.Color.Accent : Color_1.Color.Success;
    return (react_1.default.createElement(SimpleModal_1.default, { open: props.isOpen, onClose: props.onClose, size: SimpleModal_1.SimpleModalSize.Large, alignCenter: true },
        react_1.default.createElement("div", { className: "row" },
            react_1.default.createElement("div", { className: "col-md-6 u-mh-none", style: {
                    background: 'url("' + (memberImage && ImageProvider_1.default.resizeImage(memberImage, ImageProvider_1.ImageSize.Original)) + '")',
                    backgroundRepeat: 'no-repeat',
                    backgroundPosition: 'center',
                    backgroundSize: 'cover',
                } }),
            react_1.default.createElement("div", { className: "col-md-6" },
                react_1.default.createElement("div", { className: "u-pad u-pad-heavy" },
                    viewingScreen === EViewingScreen.SignUp && (react_1.default.createElement(react_1.default.Fragment, null,
                        react_1.default.createElement("div", { className: "u-marg-bottom-half" },
                            react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Money, size: "24px", color: modalPrimaryColor, useColorAsBackground: hotel === null || hotel === void 0 ? void 0 : hotel.memberOnlyModalUsePrimaryColor })),
                        react_1.default.createElement("div", { className: "u-marg-bottom-half" },
                            react_1.default.createElement(Headline_1.default, { bold: true, color: modalPrimaryColor },
                                react_1.default.createElement("div", { style: { color: modalPrimaryColor }, dangerouslySetInnerHTML: {
                                        __html: modalTitle,
                                    } }))),
                        react_1.default.createElement("div", { className: "u-marg-bottom" },
                            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey },
                                react_1.default.createElement("span", { dangerouslySetInnerHTML: { __html: modalSubtitle } }))),
                        react_1.default.createElement(SignUpForm_1.SignUpForm, { onSignIn: handleSignInClick, onDone: handleSignUpSuccess, hideTitle: true, reduceMargins: true }),
                        react_1.default.createElement("div", { className: "u-marg-top--light" },
                            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Label, color: Color_1.Color.DarkGrey }, t(Translation_1.Translation.Step.Confirmation.AlreadyHaveAccount)),
                            ' ',
                            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Label, color: Color_1.Color.DarkGrey },
                                react_1.default.createElement(BEButton_1.default, { isText: true, size: "tiny", textColor: modalPrimaryColor, onClick: handleSignInClick }, t(Translation_1.Translation.Navigation.SignIn.SignIn)))))),
                    viewingScreen === EViewingScreen.SignIn && (react_1.default.createElement(react_1.default.Fragment, null,
                        react_1.default.createElement("div", { className: "u-marg-bottom" },
                            react_1.default.createElement(BEButton_1.default, { isText: true, size: "small", icon: Icon_1.IconType.ArrowLeft, iconPosition: "left", onClick: handleBackToSignUp }, "Back to sign up")),
                        react_1.default.createElement(SignInForm_1.SignInForm, { onSignedIn: handleSignInSuccess, onForgotPassword: handleForgotPasswordClick, onNotVerified: handleBackToSignUp }))),
                    viewingScreen === EViewingScreen.ForgotPassword && (react_1.default.createElement(react_1.default.Fragment, null,
                        react_1.default.createElement("div", { className: "u-marg-bottom" },
                            react_1.default.createElement(BEButton_1.default, { isText: true, size: "small", icon: Icon_1.IconType.ArrowLeft, iconPosition: "left", onClick: handleBackToSignIn }, "Back to sign in")),
                        react_1.default.createElement(ForgotPasswordForm_1.ForgotPasswordForm, { onPasswordReset: handlePasswordResetSuccess, onCancelled: handleBackToSignIn, onDone: handlePasswordResetSuccess }))))))));
}
//# sourceMappingURL=MemberPortalSignInModal.js.map