"use client";
import {withFormik} from "formik";

import {toast} from "react-toastify";
import InnerConfigListForm from "./InnerConfigListForm";
import {AppRouterInstance} from "next/dist/shared/lib/app-router-context.shared-runtime";
import {UseMutateAsyncFunction} from "@tanstack/react-query";
import {ServerResponse} from "@/types";
export interface ConfigFormItem {
  key: string;
  value: string;
  description?: string;
}

export interface ConfigFormValues {
  configs: ConfigFormItem[];
}
export interface ConfigListFormProps {
  router: AppRouterInstance;
  initialData: ConfigFormItem[]; // 👈 دیتای ورودی تو
  onSubmit: UseMutateAsyncFunction<
    ServerResponse,
    Error,
    ConfigFormItem[],
    ServerResponse
  >;
  updatePending: boolean;
}

const ConfigListForm = withFormik<ConfigListFormProps, ConfigFormValues>({
  mapPropsToValues: (props) => ({
    configs: props.initialData, // 👈 اینجاست که فرم پر میشه
  }),

  handleSubmit: async (values, {props}) => {
    try {
      await props.onSubmit(values.configs);
      toast.success("تنظیمات ذخیره شد");
    } catch (error) {
      toast.error("خطایی رخ داده است");
    }
  },
})(InnerConfigListForm);

export default ConfigListForm;
