import axios from "axios";
import { Language } from "./models/Types";
import { CatchError } from "./common";

// const controllerUrl = 'https://localhost:7057/Dictionaries/Language/';
const controllerUrl = 'http://localhost:5198/Dictionaries/Language/';

export async function GetLanguageList() {
    try {
        // 👇️ const data: GetUsersResponse
        // const { data, status } = await axios.get<Array<Language>>(`${controllerUrl}List`,
        const { data} = await axios.get<Array<Language>>(`${controllerUrl}List`,
        {
            headers: {
            Accept: 'text/plain',
            },
        },
        );

        // console.log(JSON.stringify(data, null, 4));

        // 👇️ "response status is: 200"
        // console.log('response status is: ', status);

        return data;
    }
    catch (error)
    {
        CatchError(error);
    }
}

export async function CreateLanguage(codeIso3: string, name: string) {
    try {
        // 👇️ const data: GetUsersResponse
        // const { data, status } = await axios.post<Array<Genre>>(`${controllerUrl}CreateGenre?genreName=${value}`,
        await axios.post(`${controllerUrl}Create?codeIso3=${codeIso3}&name=${name}`,
            {
                headers: {
                // Accept: 'text/plain',
                },
                // body: {}
            },
        );

        // console.log(JSON.stringify(data, null, 4));

        // 👇️ "response status is: 200"
        // console.log('response status is: ', status);

        // return data;
    } 
    catch (error)
    {
        CatchError(error);
    }
}

export async function EditLanguage(id : string, codeIso3: string, name: string) {
    try {
        // 👇️ const data: GetUsersResponse
        // const { status } = await axios.post(`${controllerUrl}Edit?id=${id}&codeIso3=${codeIso3}&name=${name}`,
        await axios.post(`${controllerUrl}Edit?id=${id}&codeIso3=${codeIso3}&name=${name}`,
        {
            headers: {
            // Accept: 'text/plain',
            },
            // body: {}
        },
        );

        //console.log(JSON.stringify(data, null, 4));

        // 👇️ "response status is: 200"
        // console.log('response status is: ', status);

        // return Promise.resolve();
        // Promise.resolve();
    }
    catch (error)
    {
        CatchError(error);
    }
}

export async function DeleteLanguage(id : string) {
    try {
        // 👇️ const data: GetUsersResponse
        const { status } = await axios.post(`${controllerUrl}Delete?id=${id}`,
        {
            headers: {
            // Accept: 'text/plain',
            },
            // body: {}
        },
        );

        //console.log(JSON.stringify(data, null, 4));

        // 👇️ "response status is: 200"
        console.log('response status is: ', status);

        // return Promise.resolve();
        // Promise.resolve();
    }
    catch (error)
    {
        CatchError(error);
    }
}