import { FormikForm } from "@shared/nej-react-utils/Form/FormikForm";
import { NejAccountingApiClient, CompanyResponse, CancelablePromise } from "@services/accounting-api";
import { useApiClient, useCompany } from "@utils/NejManager/NejProvider";
import useMethod from "../../../utils/useMethod.macro";
import useQuery from "../../../utils/useQuery.macro";

type FormProps<T> = {
    refresh: () => 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: NejAccountingApiClient, company: CompanyResponse, props: P) => (body: T) => CancelablePromise<any>, submitText: string) {
    return function GameFormWrapper({ refresh, initialData, ...props }: P) {
        const client = useApiClient();
        const company = useCompany();

        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)}
                submitText={submitText}
            >
                <WrappedForm />
            </FormikForm>
        );
    };
}
