import {actionPromise, gql} from "./index";


let toRegexp = (str) => {
    str = str.replace(/ +/g, " ").trim();
    str = "/" + str.split(" ").join("|") + "/";
    return str;
  };
  
  let toQuery = (str, fields = ["login", "nick", "_id"]) => {
    str = toRegexp(str);
    let arr = fields.map((s) => {
        console.log(s, typeof s);
        return { [s]: str };
    });
    return { $or: arr };
  };

  function  actionSearchUser(queryString) {

    return actionPromise("FoundUser", gql(
        `query userOne($query: String) {
            UserFind(query: $query){
              login
              _id
              createdAt
                  avatar {_id url}
              acl
              nick
            }
          }`, {query: JSON.stringify([toQuery(queryString)])}
    ))
      
  }

  export default actionSearchUser;