"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 = PromoCodeInput;
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const BookingWizardPromoCode_module_scss_1 = __importDefault(require("./BookingWizard/BookingWizardPromoCode/BookingWizardPromoCode.module.scss"));
const Icon_1 = __importStar(require("./Icon/Icon"));
const SmallSpinner_1 = __importDefault(require("./loader/SmallSpinner"));
const Text_1 = __importStar(require("./Text"));
const Tooltip_1 = __importDefault(require("./Tooltip/Tooltip"));
const Color_1 = require("../../util/Color");
const TextBox_1 = __importDefault(require("./TextBox"));
function PromoCodeInput(props) {
    var _a;
    const { t } = (0, react_i18next_1.useTranslation)();
    const [promoCode, setPromoCode] = (0, react_1.useState)((_a = props.value) !== null && _a !== void 0 ? _a : '');
    const [showPromoSave, setShowPromoSave] = (0, react_1.useState)(false);
    const [showPromoRemove, setShowPromoRemove] = (0, react_1.useState)(false);
    const [valueChangedBeforeSubmit, setValueChangedBeforeSubmit] = (0, react_1.useState)(false);
    const [pressedChange, setPressedChange] = (0, react_1.useState)(false);
    const firstUpdate = (0, react_1.useRef)(null);
    const promoCodeStatus = props.valid || props.valid === false ? (props.valid ? 'success' : 'error') : undefined;
    (0, react_1.useEffect)(() => {
        if (props.valid || props.valid === false) {
            setShowPromoRemove(true);
        }
    }, [props.valid]);
    (0, react_1.useEffect)(() => {
        if (showPromoSave || showPromoRemove) {
            setPressedChange(false);
        }
    }, [showPromoSave, showPromoRemove]);
    (0, react_1.useEffect)(() => {
        setValueChangedBeforeSubmit(true);
        if (firstUpdate) {
            if (promoCode) {
                if (promoCode !== props.value) {
                    if (props.direct) {
                        updatePromoCode(promoCode, false);
                    }
                    else {
                        setShowPromoSave(true);
                    }
                }
                else {
                    setShowPromoRemove(true);
                }
            }
            else {
                setShowPromoSave(false);
                setShowPromoRemove(false);
                if (promoCode !== '') {
                    updatePromoCode('', false);
                }
            }
        }
        else {
            if (promoCode) {
                setShowPromoRemove(true);
            }
        }
    }, [promoCode]);
    const updatePromoCode = (code, propagate = true) => {
        if (code) {
            if (props.direct) {
                setShowPromoRemove(true);
            }
            else {
                setPressedChange(true);
                setShowPromoSave(false);
            }
        }
        if (propagate) {
            setPromoCode(code);
        }
        props.onChange(code);
    };
    const handleKeyPress = (event) => {
        if (event.key === 'Enter' && valueChangedBeforeSubmit) {
            setValueChangedBeforeSubmit(false);
            updatePromoCode(promoCode);
        }
    };
    const clearPromoCode = () => {
        return updatePromoCode('');
    };
    const setUpdatedPromoCode = () => {
        return updatePromoCode(promoCode);
    };
    const setPromoCodeOnChange = (val) => {
        setPromoCode(val.target.value);
    };
    return (react_1.default.createElement("div", { ref: firstUpdate, className: props.className },
        react_1.default.createElement("div", { className: "u-pos-rel" },
            props.variant === 'text' ? (react_1.default.createElement("input", { className: (0, classnames_1.default)(BookingWizardPromoCode_module_scss_1.default.container, { [BookingWizardPromoCode_module_scss_1.default['--error']]: promoCodeStatus === 'error', [BookingWizardPromoCode_module_scss_1.default['--success']]: promoCodeStatus === 'success' }), type: "text", value: (props.direct ? props.value : promoCode).toUpperCase(), placeholder: t(Translation_1.Translation.Step.Date.PromoCodePlaceHolder), onChange: setPromoCodeOnChange, onKeyPress: handleKeyPress })) : (react_1.default.createElement(TextBox_1.default, { value: (props.direct ? props.value : promoCode).toUpperCase(), placeholder: t(Translation_1.Translation.Step.Date.PromoCode), wide: true, validationStatus: promoCodeStatus, onChange: setPromoCodeOnChange, onKeyPress: handleKeyPress })),
            showPromoRemove && (react_1.default.createElement(Tooltip_1.default, { title: "Remove Promo code" },
                react_1.default.createElement("div", { className: "people-picker--promo-icon", onClick: clearPromoCode },
                    react_1.default.createElement(Icon_1.default, { size: "24px", icon: Icon_1.IconType.Close, color: Color_1.Color.Grey })))),
            showPromoSave && (react_1.default.createElement(Tooltip_1.default, { title: "Apply Code" },
                react_1.default.createElement("div", { className: "people-picker--promo-icon --success", onClick: setUpdatedPromoCode },
                    react_1.default.createElement(Icon_1.default, { size: "24px", icon: Icon_1.IconType.ArrowRight2, color: Color_1.Color.Success })))),
            pressedChange && (react_1.default.createElement(Tooltip_1.default, { title: "Promo code loading" },
                react_1.default.createElement("div", { className: "people-picker--promo-icon" },
                    react_1.default.createElement(SmallSpinner_1.default, null))))),
        !props.hideValidText && (react_1.default.createElement(react_1.default.Fragment, null,
            props.valid === true && (react_1.default.createElement("div", { className: "u-marg-top" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Success }, t(Translation_1.Translation.Step.Date.PromoCodeValidation.Valid, {
                    value: props.value,
                })))),
            props.valid === false && (react_1.default.createElement("div", { className: "u-marg-top" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Alert }, props.row.getPromoCodeWarningMessage())))))));
}
//# sourceMappingURL=PromoCodeInput.js.map