// formSubmitValidation will be deprecated. use "validateControls" instead.
export const formSubmitValidation = (result, ref) => {
    var _a;
    (_a = result.fieldErrors) === null || _a === void 0 ? void 0 : _a.forEach(element => {
        const key = element.key;
        const message = element.message;
        const hasError = element.hasError;
        const reference = ref[`${key}El`];
        if (reference && hasError && message.trim()) {
            reference.validationStatus = "error";
            reference.validationMessage = message;
        }
        else if (reference && !hasError) {
            reference.validationStatus = "indeterminate";
            reference.validationMessage = null;
        }
    });
};
// TODO: Improve the way we mark components with error. We must not use error classes.
const formElementsErrorClassesDictionary = {
    "ch-checkbox": "checkbox-error",
    "ch-combo-box-render": "combo-box-error",
    "ch-edit": "input-error"
};
const toggleErrorClass = (hasError, element) => {
    const elementTagName = element.tagName.toLowerCase();
    const errorClass = formElementsErrorClassesDictionary[elementTagName];
    if (hasError) {
        element.classList.add(errorClass);
    }
    else {
        element.classList.remove(errorClass);
    }
};
export const validateControls = (formSubmitResult, validatableControls) => {
    const validatableControlsUpdated = new Map(validatableControls);
    formSubmitResult.fieldErrors.forEach(fieldResult => {
        const fieldKey = fieldResult.key;
        const fieldHasError = fieldResult.hasError;
        const controlMapRecord = validatableControlsUpdated.get(fieldKey);
        if (controlMapRecord === null || controlMapRecord === void 0 ? void 0 : controlMapRecord.reference) {
            // toggle class to add or remove error style
            toggleErrorClass(fieldHasError, controlMapRecord.reference);
            validatableControlsUpdated.set(fieldKey, Object.assign(Object.assign({}, controlMapRecord), { hasError: fieldHasError, message: fieldResult.message }));
        }
    });
    // return validatableControls updated.
    return validatableControlsUpdated;
};
export const clearControlError = (validatableControls, controlId) => {
    const control = validatableControls.get(controlId);
    if (control === null || control === void 0 ? void 0 : control.reference) {
        toggleErrorClass(false, control.reference);
        control.hasError = false;
        control.message = "";
        validatableControls.set(controlId, control);
    }
};
//# sourceMappingURL=form-validation.js.map
