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

// 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 getGQL = (url, headers = {}) => (query = "", variables = {}) =>
  fetch(url, {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
      ...headers,
    },
    body: JSON.stringify({ query, variables }),
  }).then((res) => res.json());

const actionPutPasswordPromise = (email, password) => {
  let promise = getGQL("http://localhost:3330/graphql")(
    `mutation chanPass($email: String, $password:String){
      changePassword(email:$email, password:$password){id, email, login}
    }`,
    { email, password }
  );
    return actionPromise("putPassword", promise);
  // return actionPromise("login", promise);
};

const actionPutPassword = (email, password) => {
  return async (dispatch) => {
    let token = await dispatch(actionPutPasswordPromise(email, password));
    console.log(token);
    // if (!token.errors) {
    //   const a = dispatch(actionToken(token));
    //   console.log(a);
    // } else {
    //   const b = dispatch(actionError(token));
    //   console.log(b);
    // }
  };
};

export { actionPutPassword };
