import axios from 'axios';
import AxiosMockAdapter from 'axios-mock-adapter';
let globalId = 0;
const generateId = () => {
    globalId += 1;
    return globalId;
};
const progressSteps = Array.from({ length: 10 }, (_, i) => (i + 1) / 10);
const getRandomDelay = () => Math.random() * 300 + 200; // 200-500ms
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
const mock = new AxiosMockAdapter(axios);
mock.onPost('/api/upload').reply(async (config) => {
    const formData = config.data;
    const file = formData.get('file');
    const total = file.size;
    for (const progress of progressSteps) {
        await sleep(getRandomDelay());
        if (config.onUploadProgress) {
            config.onUploadProgress({
                loaded: Math.floor(total * progress),
                total,
                lengthComputable: true,
                bytes: Math.floor(total * progress),
            });
        }
    }
    const response = {
        id: `upl-${generateId()}`,
        type: file.type,
    };
    return [200, response];
});
export const fakePreviewFileUploadQuery = (formData, options) => axios.post('/api/upload', formData, {
    headers: {
        'Content-Type': 'multipart/form-data',
    },
    ...(options || {}),
});
