<script setup lang="ts">
import type { Artist } from '~/types'

import { reactive, ref, watch } from 'vue'
import { useStore } from '~/store'

import axios from 'axios'

import ArtistCard from '~/components/audio/artist/Card.vue'

import useErrorHandler from '~/composables/useErrorHandler'

interface Props {
  filters: Record<string, string | boolean>
  search?: boolean
  header?: boolean
  limit?: number
}

const props = withDefaults(defineProps<Props>(), {
  search: false,
  header: true,
  limit: 12
})

const store = useStore()

const query = ref('')
const artists = reactive([] as Artist[])
const count = ref(0)
const nextPage = ref()

const isLoading = ref(false)
const fetchData = async (url = 'artists/') => {
  isLoading.value = true

  try {
    const params = {
      q: query.value,
      ...props.filters,
      page_size: props.limit
    }

    const response = await axios.get(url, { params })
    nextPage.value = response.data.next
    count.value = response.data.count
    artists.push(...response.data.results)
  } catch (error) {
    useErrorHandler(error as Error)
  }

  isLoading.value = false
}

const performSearch = () => {
  artists.length = 0
  fetchData()
}

watch(
  () => store.state.moderation.lastUpdate,
  () => fetchData(),
  { immediate: true }
)
</script>

<template>
  <div class="wrapper">
    <h3
      v-if="header"
      class="ui header"
    >
      <slot name="title" />
      <span class="ui tiny circular label">{{ count }}</span>
    </h3>
    <inline-search-bar
      v-if="search"
      v-model="query"
      @search="performSearch"
    />
    <div class="ui hidden divider" />
    <div class="ui five app-cards cards">
      <div
        v-if="isLoading"
        class="ui inverted active dimmer"
      >
        <div class="ui loader" />
      </div>
      <artist-card
        v-for="artist in artists"
        :key="artist.id"
        :artist="artist"
      />
    </div>
    <slot
      v-if="!isLoading && artists.length === 0"
      name="empty-state"
    >
      <empty-state
        :refresh="true"
        @refresh="fetchData"
      />
    </slot>
    <template v-if="nextPage">
      <div class="ui hidden divider" />
      <button
        v-if="nextPage"
        :class="['ui', 'basic', 'button']"
        @click="fetchData(nextPage)"
      >
        {{ $t('components.audio.artist.Widget.button.more') }}
      </button>
    </template>
  </div>
</template>
