import { createAsyncThunk } from "@reduxjs/toolkit";
import axios from 'axios';

const instance = axios.create({
  baseURL: 'https://a1.test.coord.graff.tech',
});

instance.defaults.headers.post['Content-Type'] = 'application/json';



export const fetchCards = createAsyncThunk(
  "cards/FetchAll",
  async (language: string, thunkAPI) => {
    try {
      const { data } = await instance.get('/title/get_for_language/', { params: { start: 0, count: 100, language: language } });
      return data;
    } catch (e: any) {
      const { response } = e
      return thunkAPI.rejectWithValue(response.data.message);
    }

  }
);


export const createSession = createAsyncThunk(
  "session/Create",
  async (title: string, thunkApi) => {
    try {
      const { data } = await instance.post('/session/create', { title });
      return data;
    } catch (e: any) {
      const { response } = e
      return thunkApi.rejectWithValue(response.data.message);
    }
  }
);

export const connectSession = createAsyncThunk(
  "session/Connect",
  async (code: string, thunkApi) => {
    try {
      const { data } = await instance.get('/session/connect', { params: { session_id: code } });
      return data;
    } catch (e: any) {
      const { response } = e
      return thunkApi.rejectWithValue(response.data.message);
    }
  }
);
