import { useCallback as u } from "react";
import { useStepper as w } from "../useStepper/index.js";
import { useFormContext as y } from "react-hook-form";
import { shallow as C } from "zustand/shallow";
import { useDynamicValidation as J } from "../useDynamicValidation/index.js";
import { focusFirstErrorField as T } from "../../helpers/focusFirstErrorField/index.js";
function B() {
  const { trigger: d, clearErrors: r, getValues: n, reset: S, setFocus: f, getFieldState: m } = y(), { activateFieldsValidation: F, clearAllValidation: a } = J(), {
    nextStep: v,
    prevStep: V,
    currentStep: s,
    steps: o,
    setStepValidationStatus: g,
    resetStepper: h
  } = w(
    (t) => ({
      nextStep: t.actions.nextStep,
      prevStep: t.actions.prevStep,
      currentStep: t.currentStep,
      steps: t.steps,
      setStepValidationStatus: t.actions.setStepValidationStatus,
      resetStepper: t.actions.resetStepper
    }),
    C
  ), D = u(() => {
    const t = n();
    V(t);
  }, [V, n]), A = u(async () => {
    const t = n(), x = o.slice(s + 1), c = [];
    x.forEach((i) => {
      i.validationFields && c.push(...i.validationFields);
    });
    const l = await v(async () => {
      const e = o[s]?.validationFields || [];
      if (e.length === 0)
        return !0;
      const p = await d(e);
      return p || (F(e), T(e, m, f)), p;
    }, t);
    return l && (o[s - 1]?.validationFields || []).length > 0 && a(), l && c.length > 0 && (r(c), setTimeout(() => {
      r(c), x.forEach((i, e) => {
        const p = s + 1 + e;
        g(p, !0);
      });
    }, 100)), l;
  }, [
    v,
    r,
    g,
    o,
    s,
    d,
    n,
    F,
    a,
    f,
    m
  ]), E = u(() => {
    S(), r(), a(), h();
  }, [S, r, a, h]);
  return { prevStepAction: D, nextStepAction: A, cancelAction: E };
}
export {
  B as useStepperActions
};
