import axios from 'axios'

const OKI_API = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337'
const AWTIS_POU_CHAK_PAJ = process.env.NEXT_PUBLIC_AWTIS_POU_CHAK_PAJ || 6

export async function jwennTeksEpiSlug(slug) {
  const response = await axios.get(`${OKI_API}/teks?slug=${slug}&_where[published_at_null]=false`)
  return response.data[0]
}

export async function jwennAwtisKantite() {
  const response = await axios.get(`${OKI_API}/awtis/count?_where[published_at_null]=false`)
  return response.data
}

export async function jwennTeksKantite() {
  const response = await axios.get(`${OKI_API}/teks/count?_where[published_at_null]=false`)
  return response.data
}

export async function jwennAwtisPajinasyon(paj) {
  const start = AWTIS_POU_CHAK_PAJ * (paj - 1)
  const query = `_sort=alias:ASC&_start=${start}&_limit=${AWTIS_POU_CHAK_PAJ}&_where[published_at_null]=false`

  const response = await axios.get(`${OKI_API}/awtis?${query}`)
  return response.data
}

export async function jwennTeks() {
  const query = '_sort=tit:ASC&_where[published_at_null]=false'

  const response = await axios.get(`${OKI_API}/teks?${query}`)
  return response.data
}

export async function jwennTeksEpiUserId(userId) {
  const query = `_sort=tit:ASC&_where[user.id]=${userId}`

  const response = await axios.get(`${OKI_API}/teks?${query}`)
  return response.data
}

export async function jwennSlugs() {
  const response = await axios.get(`${OKI_API}/slugs`)
  return response.data
}

export async function jwennKomanteEpiTeksId(teksId) {
  const query = `_sort=published_at:DESC&_where[teks]=${teksId}&_where[published_at_null]=false`
  const response = await axios.get(`${OKI_API}/komante?${query}`)
  return response.data
}
