import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'

export const useAliasesStore = defineStore('aliases', () => {
  const api = useApi()
  const rows = ref([])
  const loading = ref(false)

  async function load() {
    loading.value = true
    try { rows.value = await api.get('/api/aliases') }
    finally { loading.value = false }
  }

  async function remove(aliasString, aliasCategory) {
    await api.delete(
      `/api/aliases/${encodeURIComponent(aliasString)}/${encodeURIComponent(aliasCategory)}`
    )
    rows.value = rows.value.filter(
      r => !(r.alias_string === aliasString && r.alias_category === aliasCategory)
    )
  }

  return { rows, loading, load, remove }
})
