import axios from 'axios';

import { addAuthIntercepter } from '@/utils/api/intercepters/request/authIntercepter';

const MIN_DELAY = 0; //700;
const MAX_DELAY = 0; //1700;

const api = axios.create({
  baseURL: `/service/ervu-subpoena-registry`
});

addAuthIntercepter(api);

function responseData(requestPromise) {
  return requestPromise.then(response => response.data);
}

function simulateDelay(promise) {
  return new Promise((resolve) => setTimeout(resolve, MIN_DELAY + Math.random() * (MAX_DELAY - MIN_DELAY) | 0)).then(_ => {
    return promise;
  });
}

export const subpoenas = {
  get(id) {
    return responseData(
      simulateDelay(
        api.get(`/subpoena/${encodeURIComponent(id)}`)
      )
    );
  },
  search(params) {
    return responseData(
      simulateDelay(
        api.post(`/subpoena/search`, params)
      )
    );
  },
}
