"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const formik_1 = require("formik");
const react_core_1 = require("@patternfly/react-core");
const utils_1 = require("./utils");
const useFieldErrorMsg_1 = tslib_1.__importDefault(require("../../../hooks/useFieldErrorMsg"));
const exclamation_circle_icon_1 = tslib_1.__importDefault(require("@patternfly/react-icons/dist/js/icons/exclamation-circle-icon"));
const InputField = React.forwardRef((_a, ref) => {
    var { label, labelIcon, helperText, isRequired, onChange, validate, idPostfix, children, noDefaultOnChange, description, labelInfo, showErrorMessage = true, type = 'text' } = _a, props = tslib_1.__rest(_a, ["label", "labelIcon", "helperText", "isRequired", "onChange", "validate", "idPostfix", "children", "noDefaultOnChange", "description", "labelInfo", "showErrorMessage", "type"]);
    const [field, { error, touched }] = (0, formik_1.useField)({
        name: props.name,
        validate,
    });
    const fieldId = (0, utils_1.getFieldId)(props.name, 'input', idPostfix);
    const errorMessage = (0, useFieldErrorMsg_1.default)({ name: props.name, inputError: error, validate });
    const isValid = !(touched && error);
    return (React.createElement(react_core_1.FormGroup, { id: `form-control__${fieldId}`, fieldId: fieldId, label: label, isRequired: isRequired, labelHelp: labelIcon, labelInfo: labelInfo },
        description && (React.createElement(react_core_1.HelperText, null,
            React.createElement(react_core_1.HelperTextItem, { variant: "indeterminate", "data-testid": " input-field-description-${fieldId}-helper-text" }, description))),
        React.createElement(react_core_1.Split, null,
            React.createElement(react_core_1.SplitItem, { isFilled: true },
                React.createElement(react_core_1.TextInput, Object.assign({}, field, props, { ref: ref, id: fieldId, validated: isValid ? 'default' : 'error', isRequired: isRequired, "aria-describedby": `${fieldId}-helper`, onChange: (event) => {
                        if (!props.isDisabled) {
                            !noDefaultOnChange && field.onChange(event);
                            onChange && onChange(event);
                        }
                    }, type: type }))),
            React.createElement(react_core_1.SplitItem, null, children)),
        ((showErrorMessage && !isValid) || helperText) && (React.createElement(react_core_1.FormHelperText, null,
            React.createElement(react_core_1.HelperText, null,
                showErrorMessage && !isValid && (React.createElement(react_core_1.HelperTextItem, { icon: React.createElement(exclamation_circle_icon_1.default, null), variant: 'error', id: `${fieldId}-helper-error` }, errorMessage)),
                helperText && (React.createElement(react_core_1.HelperTextItem, { variant: 'default', id: `${fieldId}-helper` }, helperText)))))));
});
exports.default = InputField;
//# sourceMappingURL=InputField.js.map