"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFakeUsers = void 0;
const faker_1 = require("@ui/dev/faker");
const axios_1 = __importDefault(require("axios"));
const axios_mock_adapter_1 = __importDefault(require("axios-mock-adapter"));
const getRandomDelay = () => Math.random() * 100 + 100; // 200-500ms
const axiosInstance = axios_1.default.create();
const mock = new axios_mock_adapter_1.default(axiosInstance, {
    delayResponse: getRandomDelay(),
});
const generateFakeUsers = (count) => {
    return Array.from({ length: count - 1 }).map(() => ({
        id: faker_1.faker.string.uuid(),
        name: faker_1.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];
});
const getFakeUsers = async (search, options) => {
    const response = await axiosInstance.get('api/users', {
        ...options,
        params: { ...options?.params, name_like: search },
    });
    return response.data;
};
exports.getFakeUsers = getFakeUsers;
