const getGQL = url => {
    return function(query, variables) {
        return fetch(url, 
        {
            method: "POST",
            headers: {"Content-Type": "application/json"},
            body: JSON.stringify({query, variables})
        }).then(resp => resp.json()).then(data => {
            if ("errors" in data) {
                throw new Error('ашипка, угадывай што не так')
            }
            else {
                return data.data[Object.keys(variables)[0]]
            }
        })
    }
}

let gql = getGQL('http://shop-roles.asmer.fs.a-level.com.ua/graphql')

//запросики
//categories
async function getCategoriesCount() {
    let result = await gql(`
    query ($query:String){
        CategoryCount(query: $query)
      }`, {CategoryCount: '', query: "[{}]"})
    return result
}

async function findAllCategories() {
    let result = await gql(`
    query ($query:String){
        CategoryFind(query: $query) {
          _id 
          name 
          image {
            _id text url 
          }
          goods {
            _id name description price images {
              _id text url
            }
          }
        }
      }`, {CategoryFind: '', query: "[{}]"})
    return result
}

async function findCategoryById(categoryId) {
    let id = `[{"_id":"${categoryId}"}]`
    let result = await gql(`
    query ($id:String){
        CategoryFindOne(query: $id){
          name 
          image {
            _id text url 
          }
          goods {
            _id name description price images {
              _id text url
            }
          }
        }
    }`, { CategoryFindOne: '', id })
    return result
}

async function findSubCategories(categoryId) {
    let id = `[{"_id":"${categoryId}"}]`
    let result = await gql(`
    query($id:String){
        CategoryFindOne(query: $id){
          subCategories {
            _id 
            name 
            image {
              _id text url originalFileName
            }
            goods {
                _id name description price images {
                  _id text url
                }
              }
          }
        }
    }`, { CategoryFindOne: '', id })
    return result
}

//goods
async function getGoodsCount() {
    let result = await gql(`
    query ($query:String){
        GoodCount(query: $query)
      }`, {GoodCount: '', query: "[{}]"})
    return result
}

async function findAllGoods() {
    let result = await gql(`
    query ($query:String){
        GoodFind(query: $query) {
          _id name description price images {
            _id text url
          }
          categories {
            _id name
          }
        }
      }`, {GoodFind: '', query: "[{}]"})
    return result
}

async function findGoodById(goodId) {
    let id = `[{"_id":"${goodId}"}]`
    let result = await gql(`
    query good($id:String){
        GoodFindOne(query: $id) {
          name description price images {
            _id text url
          }
          categories {
            _id name
          }
        }
    }`, { GoodFindOne: '', id })
    return result
}

//users 
async function getUsersCount() {
    let result = await gql(`
    query ($query:String){
        UserCount(query: $query)
      }`, {UserCount: '', query: "[{}]"})
    return result
}

async function findAllUsers() {
    let result = await gql(`
    query ($query:String){
        UserFind(query: $query){
          _id createdAt login nick acl avatar {
            _id text url
          }
        }
      }`, {UserFind: '', query: "[{}]"})
    return result
}

async function findUserById(userId) {
    let id = `[{"_id":"${userId}"}]`
    let result = await gql(`
    query ($id:String){
        UserFindOne(query: $id){
          _id createdAt login nick acl avatar {
            _id text url
          }
        }
    }`, { UserFindOne: '', id })
    return result
}

//images
async function getImagesCount() {
    let result = await gql(`
    query ($query:String){
        ImageCount(query: $query)
      }`, {ImageCount: '', query: "[{}]"})
    return result
}

async function findAllImages() {
    let result = await gql(`
    query ($query:String){
        ImageFind(query: $query) {
          _id createdAt text url originalFileName 
        }
      }`, {ImageFind: '', query: "[{}]"})
    return result
}

//orders
async function findAllOrders() {
    let result = await gql(`
    query ($query:String) {
        OrderFind(query: $query){
          _id 
        createdAt 
        total 
        orderGoods {
          _id count price good {
            _id name
          }
        }
        owner {
            _id login acl
          }
        }
      }`, {OrderFind: '', query: "[{}]"})
    return result
}

async function findOrderById(orderId) {
    let id = `[{"_id":"${orderId}"}]`
    let result = await gql(`
    query ($id:String){
        OrderFindOne(query: $id){
            _id 
          createdAt 
          total 
          orderGoods {
            _id count price good {
              _id name
            }
          }
          owner {
              _id login acl
            }
          }
    }`, { OrderFindOne: '', id })
    return result
}

//reg
async function registerUser(login, password) {
  let result = await gql(`
  mutation ($login:String, $password:String) {
    UserUpsert(user:{
      login:$login, password:$password
    }) {
      _id
      login
    }
  }`, {UserUpsert: '', login, password})
  return result
}

//всё, хватит..........