import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useState } from "react";
import { FileUpload } from ".";
const simulateUploadWithErrors = (files, setUploadState) => {
    files.forEach((file, index) => {
        const fileName = file.name;
        setUploadState((prev) => (Object.assign(Object.assign({}, prev), { [fileName]: { progress: 0, error: false } })));
        const interval = setInterval(() => {
            setUploadState((prev) => {
                var _a;
                const currentProgress = ((_a = prev[fileName]) === null || _a === void 0 ? void 0 : _a.progress) || 0;
                // Simulate an error for every second file
                if (index % 2 === 1) {
                    clearInterval(interval);
                    return Object.assign(Object.assign({}, prev), { [fileName]: {
                            progress: 0,
                            error: true,
                            helperText: "Upload failed. Please try again."
                        } });
                }
                // Simulate successful upload for other files
                if (currentProgress >= 100) {
                    clearInterval(interval);
                    return Object.assign(Object.assign({}, prev), { [fileName]: {
                            progress: 100,
                            error: false
                        } });
                }
                return Object.assign(Object.assign({}, prev), { [fileName]: Object.assign(Object.assign({}, prev[fileName]), { progress: currentProgress + 10 }) });
            });
        }, 300);
    });
};
export const AllStateStory = () => {
    const [uploadState, setUploadState] = useState({});
    const handleUpload = (files) => {
        const fileArray = Array.isArray(files) ? files : [files];
        simulateUploadWithErrors(fileArray, setUploadState);
    };
    const retryFailedUpload = () => {
        const failedFiles = Object.entries(uploadState)
            .filter(([, state]) => state.error)
            .map(([fileName]) => new File([], fileName));
        simulateUploadWithErrors(failedFiles, setUploadState);
    };
    const resetUploadState = () => {
        setUploadState({});
    };
    return (_jsxs(_Fragment, { children: [_jsx("div", { className: "h-8 border border-secondary-200 w-fit bg-primary-50 rounded-lg flex items-center px-4 dark:text-white dark:bg-secondary-600 mb-4", children: _jsx("h1", { children: "Drag & drop files to see the different states" }) }), _jsx(FileUpload, { acceptedExtensions: [".json", ".csv", ".pdf"], handleUpload: handleUpload, multiple: true, dropAreaCustomMessage: "File upload with simulated errors", uploadState: uploadState, resetUploadState: resetUploadState, maxFileSize: 1 * 1024 * 1024, retryFailedUpload: retryFailedUpload })] }));
};
//# sourceMappingURL=storyUtil.js.map