import { useAppDispatch as R } from "./index-use-app-dispatch.js";
import { setFieldValidity as S, validInput as x } from "./index-valid-input.js";
import { useRef as h, useEffect as A } from "react";
import { useSelector as g } from "react-redux";
function w(a) {
  const o = a.inputs, F = a.reducer;
  a.validateOn = a.validateOn ?? "change";
  const I = a.slice, i = h({}), c = a.reducer ? g(a.reducer) : {}, N = a.slice ?? {}, m = a.stateErrorsConfig ?? {
    errorsStateFieldName: "errors"
  }, k = R();
  A(() => {
    const n = m.errorsStateFieldName, s = c[n];
    s && s.length > 0 && V();
  }, [c[m == null ? void 0 : m.errorsStateFieldName]]);
  for (const n in o) {
    const s = n;
    o[n] = {
      //@ts-ignore
      ...o[s],
      _autocompleteRef: h(null),
      _inputRef: h(null),
      stateFieldName: s,
      reducer: F ? g(F) : null,
      slice: I,
      stateValue: null,
      setStateValue: null,
      setStateValueArray: null,
      validateOn: a.validateOn,
      getValue: function() {
        var e, r, t, f, l, d, p, u, y, v;
        return this.fieldType == "InputNumber" ? parseInt((r = (e = this._inputRef) == null ? void 0 : e.current) == null ? void 0 : r.value) : this.fieldType == "Checkbox" ? (f = (t = this._inputRef) == null ? void 0 : t.current) == null ? void 0 : f.checked : this.fieldType == "Autocomplete" ? (d = (l = this._inputRef) == null ? void 0 : l.current) == null ? void 0 : d.value : this.fieldType == "ColorPicker" ? (u = (p = this._inputRef) == null ? void 0 : p.current) == null ? void 0 : u.value : ((v = (y = this._inputRef) == null ? void 0 : y.current) == null ? void 0 : v.value) ?? void 0;
      },
      initialValue: c.initialState ? c.initialState[s] : null,
      getInitialValue: function() {
        return this.initialValue;
      },
      //
      errors: null,
      setErrors: null,
      hasError: null,
      setHasError: null,
      isDirty: null,
      setIsDirty: null
    }, o._addInternalInput = (e) => {
      if (e.stateFieldName) {
        let r = "";
        e.arrayPosition && (r = `${e.arrayPosition}`), i.current[e.stateFieldName + r] = e, o[e.stateFieldName + r] = e;
      }
      o._removeInternalInput = (r) => {
        if (r.stateFieldName) {
          let t = "";
          r.arrayPosition && (t = `${r.arrayPosition}`), delete i.current[r.stateFieldName + t], delete o[r.stateFieldName + t];
        }
      }, o._internalInputs = i;
    };
  }
  const _ = () => {
    let n = !0;
    for (const s in i.current) {
      const e = x(i.current[s]);
      n = n && e;
    }
    return n;
  }, O = () => {
    var s, e, r;
    const n = {};
    for (const t in i.current)
      i.current[t] && ((s = i.current[t]) != null && s.getValue) && (n[t] = (r = (e = i.current[t]) == null ? void 0 : e.getValue) == null ? void 0 : r.call(e));
    return n;
  }, P = () => {
    var s, e, r;
    const n = {};
    for (const t in i.current)
      (s = i.current[t]) != null && s.isDirty && (n[t] = (r = (e = i.current[t]) == null ? void 0 : e.getValue) == null ? void 0 : r.call(e));
    return n;
  }, b = () => {
    const n = N.resetState;
    k(n());
  };
  function D(n, s) {
    const e = s.split(".");
    let r = n;
    for (const t of e)
      if (r.hasOwnProperty(t))
        r = r[t];
      else
        return;
    return r;
  }
  const V = () => {
    const n = m.errorsStateFieldName;
    if (c.hasOwnProperty(n)) {
      const s = c[n];
      a.debugDisplayBeErrors;
      for (const e in i.current) {
        const r = i.current[e];
        let t = "";
        for (const f of s) {
          let l = r.errorMatching ?? [];
          Array.isArray(l) || (l = [l]);
          for (const d of l)
            for (const p in d)
              if (d.hasOwnProperty(p)) {
                const u = d[p], y = D(f, p);
                if (y !== void 0) {
                  if (typeof u == "boolean" && u === !0) {
                    t = f.errorMessage;
                    break;
                  } else if (typeof u == "string" && u === y) {
                    t = f.errorMessage;
                    break;
                  }
                }
              }
        }
        t ? S(o[e], !1, t) : S(o[e], !0);
      }
    }
  };
  return {
    formConfig: o,
    isValid: _,
    getData: O,
    getChangedData: P,
    resetForm: b,
    validateFromState: V
  };
}
export {
  w as useFormConfig
};
