import {gql} from "./gql";

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}]}]) }
    );
  };