import { gql } from "./gql";


export const userFind = (_id) => {
    return gql(`query userOne($query:String) {
            UserFindOne(query:$query){
                _id 
                avatar{
                    url
                }
            }
        }`,
        { query: JSON.stringify([{ _id }]) }
    );
};

export const reg = async (login, password) => {
    let query = `mutation reg($l:String! , $p:String!) {
        createUser(login:$l,password:$p){
            _id login
        }
    }`;
    let qVariables = {
        l: login,
        p: password
    };
    let result = await gql(query, qVariables);
    return result;
};

export const log = async (login, password) => {
    let query = ` query log($l:String!,$p:String!) {
        login(login:$l,password:$p)
      }`;
    let qVariables = {
      l: login,
      p: password
    };
    let result = await gql(query, qVariables);
    return result;
};

export const search = async (string) => {
    return gql(`query snippetFind($query:String){
        SnippetFind(query:$query){
          owner {
            _id login
          }
          title description _id files {
              type text name
          }
        }
    }`,
      { query: JSON.stringify([
        {
            $or: [{title: `/${string.trim().split(" ").join('|')}/`},{description: `/${string.trim().split(" ").join('|')}/`}] 
        },
        {
          sort: [{title: 1}]}]) }
    );
};

export const snippetByOwner = async (id) => {
    let query = `query snippetFind($query:String){
        SnippetFind(query:$query){
                          owner{
                            _id 
                          }
              title description _id files {
               type text name
             }
          }
  }`;
  let variables = {
      query: JSON.stringify([{___owner: id } , {sort:[{_id: -1}]}])
  };
  let res = gql(query, variables);
  return res;
}

export const imgFind = async () => {
    return await gql(`query fhbg{
      ImageFind(query:"[{}]"){
        url owner{
          nick
        }
      }
    }`);
};

export const ava = async (idUser, id) => {
    let query = `mutation setAvatar($idUser:String , $idAvatar:ID){ 
      UserUpsert(user:{_id: $idUser, avatar: {_id: $idAvatar}}){
          _id, avatar{
              url
          }
      }
  }`;
    let qVariables = { 
        idUser: idUser, 
        idAvatar: id 
    };
    let res = await gql(query, qVariables);
    return res;
};

export const snippetAdd = async (title, description, files , idSnippet) => {
    let query = `mutation newSnippet($snippet:SnippetInput) {
      SnippetUpsert(snippet:$snippet){
        _id
      }
    }`;
    let qVariables = { 
        snippet:{ 
            title, 
            description, 
            files, 
            _id: idSnippet 
        }  
    };
    let res = await gql(query, qVariables);
    return res;
};

export const changePass = async (login, password, newPassword) => {
    let query = `mutation change($login:String!, $password:String!, $newPassword:String!){
                      changePassword(login: $login,
                                    password: $password, 
                                    newPassword: $newPassword){
                          _id login
                      }
                }`;
    let qVariables = {
        l:login,
        p:password,
        n:newPassword
    };
    let result = await gql(query, qVariables);
    return result;
};

export const snippetById = async(id) => {
    let query = `query snippetFind($query:String){
        SnippetFind(query:$query){
            owner{
                _id 
            }
            title
            description
            _id
            files {
                type text name
            }
        }
    }`;
    let variables = {
        query: JSON.stringify([{_id: id }])
    };
    let res = gql(query, variables);
    return res;
}



  