"use client";

import { withFormik, type FormikBag } from "formik";
import type { SkillsFormProps, SkillsFormValues } from "./types";
import { INITIAL_SKILLS_VALUES } from "./constant";
import { SkillsValidationSchema } from "./validation";
import InnerSkillsForm from "./InnerSkillsForm";

const SkillsForm = withFormik<SkillsFormProps, SkillsFormValues>({
  displayName: "SkillsForm",

  enableReinitialize: true,

  mapPropsToValues: (props) => {
    return {
      computerSkill: props.data?.computerSkill || INITIAL_SKILLS_VALUES.computerSkill,
      languageSkill: props.data?.languageSkill || INITIAL_SKILLS_VALUES.languageSkill,
    };
  },

  validationSchema: SkillsValidationSchema,

  handleSubmit: async (
    values,
    bag: FormikBag<SkillsFormProps, SkillsFormValues>
  ) => {
    const { props, setSubmitting } = bag;

    // ثبت مقادیر در استیت اصلی Wizard کامپوننت مادر
    props.update({
      computerSkill: values.computerSkill,
      languageSkill: values.languageSkill,
    });

    // رفتن به گام بعدی
    props.setStep((prev) => prev + 1);
    setSubmitting(false);
  },
})(InnerSkillsForm);

export default SkillsForm;
