"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cssClass = void 0;
var classnames = require("classnames");
var React = require("react");
var react_1 = require("react");
var FontAwesome = require("react-fontawesome");
var Button_1 = require("../Button/Button");
var Forms_1 = require("../utils/Forms");
require("./TextInput2.less");
exports.cssClass = {
    CONTAINER: "TextInput2--container",
    INFO_ROW: "TextInput2--infoRow",
    INFO_ROW_LABEL_HIDDEN: "TextInput2--infoRow--labelHidden",
    INFO_REQUIREMENT: "TextInput2--infoRequirement",
    LABEL: "TextInput2--label",
    LABEL_HIDDEN: "TextInput2--label--hidden",
    LEADING_ICON: "TextInput2--leadingIcon",
    INPUT_CONTAINER: "TextInput2--inputContainer",
    INPUT_CONTAINER_FOCUSED: "TextInput2--inputContainer--focused",
    INPUT_CONTAINER_ERROR: "TextInput2--inputContainer--error",
    INPUT_CONTAINER_DISABLED: "TextInput2--inputContainer--disabled",
    INPUT: "TextInput2--input",
    FOOTER_TEXT: "TextInput2--footerText",
    ERROR_MESSAGE: "TextInput2--footerText--error",
    ERROR_ICON: "TextInput2--errorIcon",
    HIDE_SHOW: "TextInput2--hideShowButton",
};
/*
 * text input
 */
var TextInput2 = function (_a) {
    var className = _a.className, name = _a.name, label = _a.label, hideLabel = _a.hideLabel, placeholder = _a.placeholder, helpText = _a.helpText, icon = _a.icon, requirement = _a.requirement, errorOnEmpty = _a.errorOnEmpty, errorOnFocus = _a.errorOnFocus, errorValidation = _a.errorValidation, obscurable = _a.obscurable, value = _a.value, onChange = _a.onChange, onFocus = _a.onFocus, onBlur = _a.onBlur, size = _a.size, autoFocus = _a.autoFocus;
    var id = name;
    var _b = react_1.useState(false), isFocused = _b[0], setIsFocused = _b[1];
    var _c = react_1.useState(true), isObscured = _c[0], setIsObscured = _c[1];
    var inputType = obscurable && isObscured ? "password" : "text";
    // NOTE: setting errorMessage to an empty string displays an error state with no message (e.g. required)
    var _d = react_1.useState(null), errorMessage = _d[0], setErrorMessage = _d[1];
    react_1.useEffect(function () {
        // if value is empty, either:
        // 1) display empty error if errorOnEmpty is true and input isn't in focus
        // 2) remove error
        if (!value) {
            setErrorMessage(errorOnEmpty && !isFocused ? "" : null);
            return;
        }
        var newErrorMessage = errorValidation(value);
        // show error if input is out of focus
        if (newErrorMessage && !isFocused) {
            setErrorMessage(newErrorMessage);
        }
        // show error if input is in focus
        else if (newErrorMessage && isFocused && errorOnFocus) {
            setErrorMessage(newErrorMessage);
            return;
        }
        // remove error if it has been corrected
        if (!newErrorMessage)
            setErrorMessage(null);
    }, [value, isFocused, errorOnEmpty]);
    return (React.createElement("div", { className: classnames(exports.cssClass.CONTAINER, Forms_1.formElementSizeClassName(size), className) },
        React.createElement("div", { className: classnames(exports.cssClass.INFO_ROW, hideLabel && exports.cssClass.INFO_ROW_LABEL_HIDDEN) },
            React.createElement("label", { className: classnames(exports.cssClass.LABEL, hideLabel && exports.cssClass.LABEL_HIDDEN), htmlFor: id }, label),
            requirement && (React.createElement("label", { className: exports.cssClass.INFO_REQUIREMENT, "aria-live": "polite", htmlFor: id }, requirement))),
        React.createElement("div", { className: classnames(exports.cssClass.INPUT_CONTAINER, isFocused && exports.cssClass.INPUT_CONTAINER_FOCUSED, errorMessage != null && exports.cssClass.INPUT_CONTAINER_ERROR, requirement === Forms_1.FormElementRequirement.DISABLED && exports.cssClass.INPUT_CONTAINER_DISABLED) },
            icon && React.createElement("i", { className: exports.cssClass.LEADING_ICON }, icon),
            React.createElement("input", { id: id, name: id, className: exports.cssClass.INPUT, role: "textbox", type: inputType, value: value, placeholder: placeholder, disabled: requirement === Forms_1.FormElementRequirement.DISABLED, onChange: onChange, onFocus: function (e) {
                    setIsFocused(true);
                    if (onFocus)
                        onFocus(e);
                }, onBlur: function (e) {
                    setIsFocused(false);
                    if (onBlur)
                        onBlur(e);
                }, autoFocus: autoFocus }),
            errorMessage != null && (React.createElement(FontAwesome, { className: exports.cssClass.ERROR_ICON, name: "exclamation-circle" })),
            obscurable && (React.createElement(Button_1.Button, { className: exports.cssClass.HIDE_SHOW, type: "linkPlain", onClick: function (e) { return setIsObscured(!isObscured); }, value: isObscured ? "Show" : "Hide" }))),
        (errorMessage || helpText) && (React.createElement("div", { className: classnames(exports.cssClass.FOOTER_TEXT, errorMessage && exports.cssClass.ERROR_MESSAGE) }, errorMessage || helpText))));
};
TextInput2.defaultProps = {
    errorOnEmpty: false,
    errorValidation: function () { return null; },
    size: Forms_1.FormElementSize.FULL_WIDTH,
};
exports.default = TextInput2;
