import { client } from '@things-factory/shell'
import gql from 'graphql-tag'

export async function fetchLocationSortingRule(sortingRule) {
  const response = await client.query({
    query: gql`
      query setting($name: String!) {
        setting(name: $name) {
          value
        }
      }
    `,
    variables: { name: sortingRule }
  })

  if (!response.errors) {
    let filters = []

    if (response.data.setting?.value) {
      const sortingRule = JSON.parse(response.data.setting.value)
      filters = Object.keys(sortingRule).map(field => {
        return { name: field, desc: sortingRule[field] === 'DESC' }
      })
    }

    return filters
  }
}
