"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFieldErrorMsg = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const formik_1 = require("formik");
// With getRichTextValidation we can receive an array of strings so it needs to be converted to string
const adaptErrorInvalidFormat = (error) => {
    if (Array.isArray(error)) {
        return error.join('. ');
    }
    return error;
};
const useFieldErrorMsg = ({ name, inputError, validate }) => {
    /*
      for empty values - show an error only if at a certain point it was none empty, to avoid new items in a field array shown as invalid right away
      for none empty values - always show the error if there is one
    */
    const [field, { error }] = (0, formik_1.useField)({
        name,
        validate,
    });
    const { submitCount } = (0, formik_1.useFormikContext)();
    const [hadValue, setHadValue] = React.useState(false);
    React.useEffect(() => {
        if (field.value) {
            setHadValue(!!field.value);
        }
    }, [field.value, setHadValue]);
    const showError = !!field.value || (!field.value && hadValue) || submitCount > 0;
    const errorMessage = adaptErrorInvalidFormat((showError ? error : '') || inputError);
    return errorMessage;
};
exports.useFieldErrorMsg = useFieldErrorMsg;
exports.default = exports.useFieldErrorMsg;
//# sourceMappingURL=useFieldErrorMsg.js.map