import gql from 'graphql-tag'

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

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

  if (!response.errors) {
    if (response.data.setting?.value) {
      switch (response.data.setting.value.toLowerCase().trim()) {
        case 'true':
          return true
        case 'false':
          return false
        default:
          return response.data.setting.value.trim()
      }
    }

    return false
  }
}
