"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fakeFileUploadQuery = void 0;
const axios_1 = __importDefault(require("axios"));
const axios_mock_adapter_1 = __importDefault(require("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 axios_mock_adapter_1.default(axios_1.default);
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];
});
const fakeFileUploadQuery = (formData, options) => axios_1.default.post('/api/upload', formData, {
    headers: {
        'Content-Type': 'multipart/form-data',
    },
    ...(options || {}),
});
exports.fakeFileUploadQuery = fakeFileUploadQuery;
