import { faker } from '@ui/dev/faker';
import axios from 'axios';
import AxiosMockAdapter from 'axios-mock-adapter';
const getRandomDelay = () => Math.random() * 100 + 100; // 200-500ms
const axiosInstance = axios.create();
const mock = new AxiosMockAdapter(axiosInstance, {
    delayResponse: getRandomDelay(),
});
const generateFakeUsers = (count) => {
    return Array.from({ length: count - 1 }).map(() => ({
        id: faker.string.uuid(),
        name: faker.person.fullName(),
    }));
};
const FAKE_USERS = [
    {
        id: '1',
        name: 'Иван Третьяков',
    },
    ...generateFakeUsers(30),
];
mock.onGet('/api/users').reply((config) => {
    const search = new URLSearchParams(config.params).get('name_like') || '';
    const users = FAKE_USERS.filter((user) => user.name.toLowerCase().includes(search.toLowerCase()));
    return [200, users];
});
mock.onGet('/api/users/id').reply((config) => {
    const userId = new URLSearchParams(config.params).get('id') || '';
    const user = FAKE_USERS.find((fakeUser) => fakeUser.id === userId);
    return [200, user];
});
export const getFakeUser = async (id) => {
    const response = await axiosInstance.get('api/users/id', {
        params: { id },
    });
    return response.data;
};
export const getFakeUsers = async (search, options) => {
    const response = await axiosInstance.get('api/users', {
        ...options,
        params: { ...options?.params, name_like: search },
    });
    return response.data;
};
