import { FormikForm } from "@shared/nej-react-utils/Form/FormikForm";
import { useApiClient, useProject } from "@utils/NejManager/NejProvider";
import useMethod from "../macros/useMethod.macro";
import useQuery from "../macros/useQuery.macro";
import { CancelablePromise, PortalApiClient, ProjectResponse } from "@services/portal-api";

type FormProps<T> = {
    refresh: () => void;
    afterSubmit?: (values: T) => void;
    initialData?: T;
};
export type WithFormProps<T, P> = P & FormProps<T>;
export function withForm<T extends object, P extends FormProps<T>>(WrappedForm: React.ComponentType, method: (client: PortalApiClient, project: ProjectResponse, props: P) => (body: T) => CancelablePromise<any>, submitText: string) {
    return function GameFormWrapper({ refresh, afterSubmit, initialData, ...props }: P) {
        const client = useApiClient();
        const company = useProject();

        const fnc = method(client, company, { ...props, initialData } as P);
        console.log(fnc);

        const [submit, { }] = useMethod(
            fnc,
            () => refresh()
        );

        return (
            <FormikForm
                data={initialData || {} as T}
                onSubmit={(values) => {submit(values); afterSubmit && afterSubmit(values)}}
                submitText={submitText}
            >
                <WrappedForm />
            </FormikForm>
        );
    };
}
