// @ts-ignore
// import { useState, useEffect } from 'react';
import axios, { AxiosResponse } from 'axios';
import { allowedToTrack } from 'helper';

export function useTracker(): {
  increaseVisits: (unique: boolean) => Promise<AxiosResponse<any, any>>
  updatePlayerTrackerData: (brawlhalla_id: number | string | string[], name: string | string[]) => Promise<AxiosResponse<any, any>>,
  updateClanTrackerData: (clan_id: number | string | string[], name: string | string[]) => Promise<AxiosResponse<any, any>>,
  getVisits: () => Promise<AxiosResponse<any, any>>
  getPlayerTrackerData: (brawlhalla_id: number | string | string[], name: string | string[]) => Promise<AxiosResponse<any, any>>,
  getClanTrackerData: (clan_id: number | string | string[], name: string | string[]) => Promise<AxiosResponse<any, any>>

} {
  let URL = process.env.DOMAIN + "/api/tracker/";
  async function increaseVisits(unqiue: boolean): Promise<AxiosResponse<any, any>> {
    if(!allowedToTrack()) return
    
    let res = await axios.post<AxiosResponse>(URL + "views", {
      "uniqueVisit": unqiue
    })
    return res.data
  }
  async function getVisits(): Promise<AxiosResponse<any, any>> {
    let res = await axios.get<AxiosResponse>(URL + "views")
    return res.data
  }
  async function updatePlayerTrackerData(brawlhalla_id: number | string | string[], name: string | string[]): Promise<AxiosResponse<any, any>> {
    if(!allowedToTrack()) return
    let res = await axios.post<AxiosResponse>(URL + "player/" + brawlhalla_id?.toString(), {
      name: name,
      get: false,
    })
    return res.data
  }

  async function getPlayerTrackerData(brawlhalla_id: number | string | string[], name: string | string[]): Promise<AxiosResponse<any, any>> {
    let res = await axios.post<AxiosResponse>(URL + "player/" + brawlhalla_id?.toString(), {
      name: name,
      get: true
    })
    return res.data
  }
  async function updateClanTrackerData(clan_id: number | string | string[], name: string | string[]): Promise<AxiosResponse<any, any>> {
    if(!allowedToTrack()) return
    let res = await axios.post<AxiosResponse>(URL + "clan/" + clan_id?.toString(), {
      name: name,
      get: false,
    })
    return res.data
  }

  async function getClanTrackerData(clan_id: number | string | string[], name: string | string[]): Promise<AxiosResponse<any, any>> {
    let res = await axios.post<AxiosResponse>(URL + "clan/" + clan_id?.toString(), {
      name: name,
      get: true
    })
    return res.data
  }
  return {
    increaseVisits, updatePlayerTrackerData, getVisits, getPlayerTrackerData,
    getClanTrackerData, updateClanTrackerData
  };
}