import axios from 'axios';
import Utils from '#/app/Utils';

const $ax = axios.create({
    baseURL: '/api/',
    transformRequest: [
        (data, headers)=>{
            if (data && !(data instanceof FormData)){
                data = Utils.toSafeJSON(data);
                headers['Content-Type'] = 'application/json;charset=utf-8';
            }
            return data;
        }
        , ...axios.defaults.transformRequest
    ]
})

const api = {
    gameObject:{
        async save(data){
            return await $ax.put('/game-object', data);
        },
        async load(id){
            return await $ax.get(`/game-object/${id}`);
        },
        async search(query){
            return await $ax.post('/game-object', query);
        },
        async remove(id){
            return await $ax.delete(`/game-object/${id}`)
        },
        async getTags(q){
            return await $ax.post('/game-object/tags', {q});
        }
    },
    scenario:{
        async save(data){
            return await $ax.put('/scenario', data);
        },
        async load(id){
            return await $ax.get(`/scenario/${id}`);
        },
        async search(query){
            return await $ax.post('/scenario', query);
        },
        async remove(id){
            return await $ax.delete(`/scenario/${id}`)
        }
    },
    game:{
        async save(data){
            return await $ax.put('/game', data);
        },
        async load(id){
            return await $ax.get(`/game/${id}`);
        },
        async search(query){
            return await $ax.post('/game', query);
        },
        async remove(id){
            return await $ax.delete(`/game/${id}`)
        },
        async setHeader(id, data){
            return await $ax.post(`/game/${id}/header`, data);
        }
    },
    user:{
        async tm(action, object, data){
            return await $ax.post('/user/tm', {action, object, data});
        },
        async signin(data){
            return await $ax.post('/user/signin', data);
        },
        async signup(data){
            return await $ax.post('/user/signup', data);
        },
        async signout(){
            return await $ax.get('/user/signout');
        },
        async load(){
            return await $ax.get('/user/info');
        },
        async update(data){
            return await $ax.post('/user/update', data);
        },
        async addGameData(data){
            return await $ax.put('/user/gameData', data);
        },
        async getGameData(){
            return await $ax.get('/user/gameData');
        }
    }
}

export { api };