import axios, { AxiosInstance, AxiosResponse } from 'axios';

const API = ({ headers = {}, params = {} } = {}): AxiosInstance => {
  const user = JSON.parse(localStorage.getItem('user') as string) ?? {};
  const BASE_URL = import.meta.env.VITE_APP_TAGSAMURAI_API;

  const instance = axios.create({
    baseURL: `${BASE_URL}/fam/iot/v2`,
    headers: {
      'Content-type': 'application/json',
      'Authorization': `Bearer ${user.token}`,
      ...headers,
    },
    params,
  });

  return instance;
};

const DeviceIOTServices = {
  getReader: (serialNumber: string[]): Promise<AxiosResponse> => {
    return API({
      params: {
        serialNumber: JSON.stringify(serialNumber),
      },
    }).get('/reader');
  },

  validateSerialNumber: (serialNumber: string[]): Promise<AxiosResponse> => {
    return API({
      params: {
        serialNumber: JSON.stringify(serialNumber),
      },
    }).get('/reader/validate-serial-number');
  },
};

export default DeviceIOTServices;
