import { defineStore } from 'pinia'
import { AxiosWrapper } from '@/utils/axios-wrapper'
import { get } from 'lodash-es'
const url = '/competition'
const axios = new AxiosWrapper()

export const CompetitionStore = defineStore('competition', () => {
  const query = async ({ skip = 0, limit = undefined, ...info } = {}) => {
    let cond = {}
    if (skip) cond.skip = skip
    if (limit) cond.limit = limit
    cond = { ...cond, ...info }
    const res = await axios.$get(`${url}`, cond)
    return res
  }
  const fetch = async (payload) => {
    const res = await axios.$get(`${url}/${payload}`)
    return res
  }
  const create = async (payload) => {
    const res = await axios.$post(`${url}`, payload)
    return res
  }
  const update = async (payload) => {
    const id = get(payload, 'id', get(payload, '_id'))
    const res = await axios.$post(`${url}/${id}`, payload)
    return res
  }
  const del = async (payload) => {
    const res = await axios.$delete(`${url}/${payload}`)
    return res
  }
  return {
    query,
    fetch,
    create,
    update,
    del
  }
})
