import { actionPromise, store } from "../../../store/store";
// import history from '../../history';

// store.dispatch({
//   type: "PROMISE",
//   name: "login",
//   status: "RESOLVED",
//   payload: "hi",
// });

// store.dispatch({
//   type: "PROMISE",
//   name: "chatList",
//   status: "REJECTED",
//   error: "bye",
// });

// function actionToken(token) {
//   return {
//     type: "TOKEN",
//     token,
//   };
// }

// function actionError(error) {
//   return {
//     type: "ERROR",
//     error,
//   };
// }

// const handleClick = () => history.push("/my_profile")

const getGQL = (url, headers = {}) => (query = "") =>
  fetch(url, {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
      ...headers,
    },
    body: JSON.stringify({ query }),
  })
  // .then((res) => console.log(res))
    .then((res) => res.json())
    // .then((a) => console.log(a));

const actionChatGroupPromise = () => {
  var promise = getGQL("http://localhost:3330/graphql")(
    `query allChatGroup{
      getAllChatGroups {
        id, inId {
          id, login
        }, outId {
          id, login
        }
      }
    }`
  );
  return actionPromise("chatGroup", promise);
};

const actionChatGroup = () => {
  return async (dispatch) => {
    var token = await dispatch(actionChatGroupPromise());
    // console.log(token);
    // if (token === undefined) await dispatch(actionLoginPromise1(login, password))
    // if(token) history.push("/my_profile")
  };
};

const actionMessagePromise = () => {
  var promise = getGQL("http://localhost:3330/graphql")(
    `query allMessage{
      getAllMessages {
        id, message, inId {
          id, login
        }
      }
    }`
  );
  return actionPromise("message", promise);
};

const actionMessage = () => {
  return async (dispatch) => {
    var token = await dispatch(actionMessagePromise());
    // console.log(token);
  };
};

export { actionChatGroup, actionMessage };
