var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import { useEffect, useState } from 'react';
import { useAppDispatch } from 'src/app/redux/store';
import { isEqual } from 'src/lib/utils';
export var useForm = function (options) {
    var dispatch = (options === null || options === void 0 ? void 0 : options.onChangeDispatch) ? useAppDispatch() : undefined;
    var _a = __read(useState(__assign({}, options === null || options === void 0 ? void 0 : options.initialValues)), 2), formData = _a[0], setFormData = _a[1];
    var data = (options === null || options === void 0 ? void 0 : options.customData) || formData;
    var _b = __read(useState({}), 2), errors = _b[0], setErrors = _b[1];
    var checkErrorsInData = function (formData, customValidations) {
        var _a;
        if (formData === void 0) { formData = data; }
        var errorOnInit = options === null || options === void 0 ? void 0 : options.errorOnInit;
        var validations = __assign(__assign({}, options === null || options === void 0 ? void 0 : options.validations), customValidations);
        if (!errorOnInit || !validations) {
            return {};
        }
        var newErrors = {};
        for (var key in validations) {
            var value = formData[key];
            var validation = validations[key];
            if (((_a = validation === null || validation === void 0 ? void 0 : validation.required) === null || _a === void 0 ? void 0 : _a.value) && !value) {
                newErrors[key] = validation.required.message;
            }
            var pattern = validation === null || validation === void 0 ? void 0 : validation.pattern;
            if ((pattern === null || pattern === void 0 ? void 0 : pattern.value) && !RegExp(pattern.value).test(value)) {
                newErrors[key] = pattern.message;
            }
            var custom = validation === null || validation === void 0 ? void 0 : validation.custom;
            if ((custom === null || custom === void 0 ? void 0 : custom.isValid) && !custom.isValid(value)) {
                newErrors[key] = custom.message;
            }
        }
        return newErrors;
    };
    useEffect(function () {
        var newErrors = checkErrorsInData(data);
        if (!isEqual(errors, newErrors))
            setErrors(newErrors);
    }, [data]);
    var handleChange = function (key, extraKeyValue) { return function (e) {
        var _a;
        var _b;
        if (e.target.pattern && !e.target.validity.valid)
            return;
        var isCheckbox = e.target.type === 'checkbox';
        var booleanValue = e.target.checked;
        var value = e.target.value;
        var newValue = isCheckbox ? booleanValue : value;
        if (dispatch && (options === null || options === void 0 ? void 0 : options.onChangeDispatch)) {
            var getNewFilters = function () {
                var _a, _b;
                if (extraKeyValue)
                    return _a = {},
                        _a[key] = newValue,
                        _a[extraKeyValue.key] = extraKeyValue.value,
                        _a;
                return _b = {}, _b[key] = newValue, _b;
            };
            dispatch(options.onChangeDispatch(getNewFilters()));
            if (((_b = options.apiTriggers) === null || _b === void 0 ? void 0 : _b.includes(key)) && options.onSubmit) {
                options.onSubmit(__assign({}, getNewFilters()));
            }
        }
        else {
            setFormData(__assign(__assign({}, formData), (_a = {}, _a[key] = newValue, _a)));
        }
    }; };
    var handleKeyDown = function (key, e) {
        var _a;
        if (((_a = options === null || options === void 0 ? void 0 : options.apiTriggersOnEnter) === null || _a === void 0 ? void 0 : _a.includes(key)) && options.onSubmit) {
            if (e.key === 'Enter') {
                options.onSubmit();
            }
        }
    };
    var handleFilterApply = function (key, mainValue, extraKeyValue, extras) {
        var _a;
        var _b;
        if ((extras === null || extras === void 0 ? void 0 : extras.pattern) && !extras.isValid)
            return;
        if (dispatch && (options === null || options === void 0 ? void 0 : options.onChangeDispatch)) {
            var getNewFilters = function () {
                var _a, _b;
                if (extraKeyValue) {
                    var obj = extraKeyValue.reduce(function (obj, item) {
                        var _a;
                        return Object.assign(obj, (_a = {}, _a[item.key] = item.value, _a));
                    }, {});
                    return __assign((_a = {}, _a[key] = mainValue, _a), obj);
                }
                return _b = {}, _b[key] = mainValue, _b;
            };
            dispatch(options.onChangeDispatch(getNewFilters()));
            if (((_b = options.apiTriggers) === null || _b === void 0 ? void 0 : _b.includes(key)) && options.onSubmit) {
                options.onSubmit(__assign({}, getNewFilters()));
            }
        }
        else {
            setFormData(__assign(__assign({}, formData), (_a = {}, _a[key] = mainValue, _a)));
        }
    };
    var handleSubmit = function (e) {
        e.preventDefault();
        if (Object.keys(errors).length > 0)
            return;
        if (options === null || options === void 0 ? void 0 : options.onSubmit) {
            options.onSubmit();
        }
    };
    var resetForm = function () {
        if (options === null || options === void 0 ? void 0 : options.emptyData) {
            if (dispatch && options.onChangeDispatch) {
                dispatch(options.onChangeDispatch(options.emptyData));
                setErrors(checkErrorsInData(__assign(__assign({}, data), options.emptyData)));
            }
        }
        else {
            var empty = {};
            var resetData = (options === null || options === void 0 ? void 0 : options.initialValues) || empty;
            setErrors(checkErrorsInData(resetData));
            setFormData(resetData);
        }
    };
    return {
        data: formData,
        handleChange: handleChange,
        handleFilterApply: handleFilterApply,
        handleSubmit: handleSubmit,
        handleKeyDown: handleKeyDown,
        checkErrors: function (formData, customValidations) {
            if (formData === void 0) { formData = data; }
            return setErrors(checkErrorsInData(formData, customValidations));
        },
        resetForm: resetForm,
        errors: errors,
    };
};
