import { useQuery } from "react-query";
import { UserEditModalForm } from "./UserEditModalForm";
import { isNotEmpty, QUERIES } from "../../../../../_digifi/helpers";
import { useListView } from "../core/ListViewProvider";
import { getUserById, getApprovedUsers } from "../../core/_requests";

const UserEditModalFormWrapper = () => {
  const { itemIdForUpdate, setItemIdForUpdate } = useListView();
  const enabledQuery: boolean = isNotEmpty(itemIdForUpdate);
  const {
    isLoading,
    data: user,
    error,
  } = useQuery(
    `${QUERIES.READY_LIST}-user-${itemIdForUpdate}`,
    () => {
      // return getUserById(itemIdForUpdate);
      return getApprovedUsers('');
    },
    {
      cacheTime: 0,
      enabled: enabledQuery,
      onError: (err) => {
        setItemIdForUpdate(undefined);
        console.error(err);
      },
    }
  );

  if (!itemIdForUpdate) {
    return (
      <UserEditModalForm isUserLoading={isLoading} user={{ id: undefined }} />
    );
  }

  if (!isLoading && !error && user) {
    // return <UserEditModalForm isUserLoading={isLoading} user={user} />;
    // REMOVE LATER AND ALLOW UP ALONE
    let newUser:any = user?.records
    return <UserEditModalForm isUserLoading={isLoading} user={newUser} />;
  }

  return null;
};

export { UserEditModalFormWrapper };
