import { withFormik } from "formik";
import * as yup from "yup";
import InnerRegistrationCenterForm from "./InnerRegistrationCenterForm";
import { CenterItem, IdentityFormValues } from "@/core/types";

export interface RegistrationCenterFormValues {
  selectedCenter: CenterItem | null;
}

export interface WizardFormData {
  registrationCenter: {
    selectedCenter: CenterItem | null;
  };
  identity: IdentityFormValues;
}

export interface RegistrationCenterFormProps {
  step: number;
  setStep: React.Dispatch<React.SetStateAction<number>>;
  data: WizardFormData;
  update: (patch: Partial<WizardFormData>) => void;
}

const EMPTY_VALUES: RegistrationCenterFormValues = {
  selectedCenter: null,
};

const RegistrationCenterFormValidationSchema = yup.object({
  selectedCenter: yup
    .mixed<CenterItem | null>()
    .nullable()
    .required("لطفاً یک مرکز را انتخاب کنید"),
});

const RegistrationCenterForm = withFormik<
  RegistrationCenterFormProps,
  RegistrationCenterFormValues
>({
  enableReinitialize: true,

  mapPropsToValues: (props) => ({
    selectedCenter: props.data.registrationCenter.selectedCenter ?? null,
  }),

  validationSchema: RegistrationCenterFormValidationSchema,

  handleSubmit: (values, { props }) => {
    props.update({
      registrationCenter: values,
    });

    props.setStep((prev) => prev + 1);
  },
})(InnerRegistrationCenterForm);

export default RegistrationCenterForm;
