"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentProcessor = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const Card_1 = require("../common/Card");
const DocumentProcessor = ({ acceptedTypes = ['.pdf', '.txt', '.docx', '.doc', '.csv', '.json'], maxFileSize = 10 * 1024 * 1024, // 10MB
processingOptions = ['summarize', 'extract-entities', 'analyze-sentiment', 'extract-topics'], onProcess, showPreview = true, allowBatch = true, maxFiles = 5, }) => {
    const [documents, setDocuments] = (0, react_1.useState)([]);
    const [selectedOptions, setSelectedOptions] = (0, react_1.useState)(['summarize']);
    const [isDragging, setIsDragging] = (0, react_1.useState)(false);
    const handleDragOver = (0, react_1.useCallback)((e) => {
        e.preventDefault();
        setIsDragging(true);
    }, []);
    const handleDragLeave = (0, react_1.useCallback)((e) => {
        e.preventDefault();
        setIsDragging(false);
    }, []);
    const processFile = async (file) => {
        const doc = {
            id: Date.now().toString(),
            filename: file.name,
            fileSize: file.size,
            mimeType: file.type,
            status: 'pending',
            progress: 0,
            uploadedAt: new Date(),
        };
        setDocuments(prev => [...prev, doc]);
        // Update to processing
        setTimeout(() => {
            setDocuments(prev => prev.map(d => d.id === doc.id ? { ...d, status: 'processing', progress: 30 } : d));
        }, 500);
        try {
            if (onProcess) {
                const result = await onProcess(file, selectedOptions);
                setDocuments(prev => prev.map(d => d.id === doc.id ? result : d));
            }
            else {
                // Mock processing
                await new Promise(resolve => setTimeout(resolve, 3000));
                const mockResult = {
                    ...doc,
                    status: 'completed',
                    progress: 100,
                    processedAt: new Date(),
                    result: {
                        summary: `This document "${file.name}" contains important information about AI and machine learning concepts. It discusses various approaches to natural language processing and their applications in modern software development.`,
                        keyPoints: [
                            'Introduction to transformer architecture',
                            'Benefits of large language models',
                            'Best practices for prompt engineering',
                            'Ethical considerations in AI development',
                        ],
                        entities: [
                            { type: 'Technology', value: 'GPT-4' },
                            { type: 'Company', value: 'OpenAI' },
                            { type: 'Concept', value: 'Machine Learning' },
                        ],
                        sentiment: 'positive',
                        wordCount: 2456,
                        language: 'English',
                        topics: ['AI', 'Machine Learning', 'NLP', 'Software Development'],
                    },
                };
                setDocuments(prev => prev.map(d => d.id === doc.id ? mockResult : d));
            }
        }
        catch (error) {
            setDocuments(prev => prev.map(d => d.id === doc.id ? {
                ...d,
                status: 'failed',
                error: error instanceof Error ? error.message : 'Processing failed',
            } : d));
        }
    };
    const handleDrop = (0, react_1.useCallback)(async (e) => {
        e.preventDefault();
        setIsDragging(false);
        const files = Array.from(e.dataTransfer.files);
        const validFiles = files
            .filter(file => {
            const extension = '.' + file.name.split('.').pop();
            return acceptedTypes.includes(extension) && file.size <= maxFileSize;
        })
            .slice(0, allowBatch ? maxFiles : 1);
        for (const file of validFiles) {
            await processFile(file);
        }
    }, [acceptedTypes, maxFileSize, allowBatch, maxFiles]);
    const handleFileSelect = async (e) => {
        const files = Array.from(e.target.files || []);
        const validFiles = files
            .filter(file => {
            const extension = '.' + file.name.split('.').pop();
            return acceptedTypes.includes(extension) && file.size <= maxFileSize;
        })
            .slice(0, allowBatch ? maxFiles : 1);
        for (const file of validFiles) {
            await processFile(file);
        }
    };
    const handleOptionToggle = (option) => {
        setSelectedOptions(prev => prev.includes(option)
            ? prev.filter(o => o !== option)
            : [...prev, option]);
    };
    const formatFileSize = (bytes) => {
        if (bytes < 1024)
            return bytes + ' B';
        if (bytes < 1024 * 1024)
            return (bytes / 1024).toFixed(1) + ' KB';
        return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
    };
    const getStatusColor = (status) => {
        switch (status) {
            case 'completed':
                return 'text-green-600';
            case 'processing':
                return 'text-blue-600';
            case 'failed':
                return 'text-red-600';
            default:
                return 'text-gray-600';
        }
    };
    const getStatusIcon = (status) => {
        switch (status) {
            case 'completed':
                return '✓';
            case 'processing':
                return '⟳';
            case 'failed':
                return '✗';
            default:
                return '⏳';
        }
    };
    return ((0, jsx_runtime_1.jsx)(Card_1.Card, { className: "document-processor", children: (0, jsx_runtime_1.jsxs)("div", { className: "p-6", children: [(0, jsx_runtime_1.jsx)("h3", { className: "text-lg font-semibold mb-6", children: "Document Processor" }), (0, jsx_runtime_1.jsxs)("div", { className: "mb-6", children: [(0, jsx_runtime_1.jsx)("label", { className: "block text-sm font-medium text-gray-700 mb-3", children: "Processing Options" }), (0, jsx_runtime_1.jsx)("div", { className: "flex flex-wrap gap-2", children: processingOptions.map((option) => ((0, jsx_runtime_1.jsxs)("label", { className: `flex items-center px-3 py-2 rounded-lg border cursor-pointer transition-colors ${selectedOptions.includes(option)
                                    ? 'border-blue-500 bg-blue-50 text-blue-700'
                                    : 'border-gray-200 hover:border-gray-300'}`, children: [(0, jsx_runtime_1.jsx)("input", { type: "checkbox", checked: selectedOptions.includes(option), onChange: () => handleOptionToggle(option), className: "mr-2" }), (0, jsx_runtime_1.jsx)("span", { className: "text-sm capitalize", children: option.replace(/-/g, ' ') })] }, option))) })] }), (0, jsx_runtime_1.jsxs)("div", { onDragOver: handleDragOver, onDragLeave: handleDragLeave, onDrop: handleDrop, className: `border-2 border-dashed rounded-lg p-8 text-center transition-colors ${isDragging
                        ? 'border-blue-500 bg-blue-50'
                        : 'border-gray-300 hover:border-gray-400'}`, children: [(0, jsx_runtime_1.jsx)("div", { className: "text-4xl mb-4", children: "\uD83D\uDCC4" }), (0, jsx_runtime_1.jsxs)("p", { className: "text-gray-600 mb-4", children: ["Drag and drop ", allowBatch ? `up to ${maxFiles} files` : 'a file', " here, or"] }), (0, jsx_runtime_1.jsxs)("label", { htmlFor: "file-upload", className: "cursor-pointer", children: [(0, jsx_runtime_1.jsx)("span", { className: "inline-block px-4 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 cursor-pointer", children: "Browse Files" }), (0, jsx_runtime_1.jsx)("input", { id: "file-upload", type: "file", accept: acceptedTypes.join(','), onChange: handleFileSelect, multiple: allowBatch, className: "hidden" })] }), (0, jsx_runtime_1.jsxs)("p", { className: "text-xs text-gray-500 mt-4", children: ["Accepted: ", acceptedTypes.join(', '), " (Max ", formatFileSize(maxFileSize), ")"] })] }), documents.length > 0 && ((0, jsx_runtime_1.jsxs)("div", { className: "mt-6 space-y-4", children: [(0, jsx_runtime_1.jsx)("h4", { className: "font-medium text-gray-900", children: "Processed Documents" }), documents.map((doc) => ((0, jsx_runtime_1.jsxs)("div", { className: "border rounded-lg p-4", children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex justify-between items-start mb-3", children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex items-center", children: [(0, jsx_runtime_1.jsx)("span", { className: `text-2xl mr-3 ${getStatusColor(doc.status)}`, children: getStatusIcon(doc.status) }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("h5", { className: "font-medium text-gray-900", children: doc.filename }), (0, jsx_runtime_1.jsxs)("p", { className: "text-sm text-gray-500", children: [formatFileSize(doc.fileSize), " \u2022 ", doc.uploadedAt.toLocaleTimeString()] })] })] }), (0, jsx_runtime_1.jsx)("span", { className: `text-sm font-medium ${getStatusColor(doc.status)}`, children: doc.status.charAt(0).toUpperCase() + doc.status.slice(1) })] }), doc.status === 'processing' && ((0, jsx_runtime_1.jsx)("div", { className: "mb-3", children: (0, jsx_runtime_1.jsx)("div", { className: "w-full bg-gray-200 rounded-full h-2", children: (0, jsx_runtime_1.jsx)("div", { className: "bg-blue-500 h-2 rounded-full transition-all", style: { width: `${doc.progress}%` } }) }) })), doc.status === 'completed' && doc.result && showPreview && ((0, jsx_runtime_1.jsxs)("div", { className: "bg-gray-50 rounded p-4 space-y-3", children: [doc.result.summary && ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("h6", { className: "text-sm font-medium text-gray-700 mb-1", children: "Summary" }), (0, jsx_runtime_1.jsx)("p", { className: "text-sm text-gray-600", children: doc.result.summary })] })), doc.result.keyPoints && doc.result.keyPoints.length > 0 && ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("h6", { className: "text-sm font-medium text-gray-700 mb-1", children: "Key Points" }), (0, jsx_runtime_1.jsx)("ul", { className: "list-disc list-inside text-sm text-gray-600", children: doc.result.keyPoints.map((point, index) => ((0, jsx_runtime_1.jsx)("li", { children: point }, index))) })] })), doc.result.topics && doc.result.topics.length > 0 && ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("h6", { className: "text-sm font-medium text-gray-700 mb-1", children: "Topics" }), (0, jsx_runtime_1.jsx)("div", { className: "flex flex-wrap gap-2", children: doc.result.topics.map((topic) => ((0, jsx_runtime_1.jsx)("span", { className: "px-2 py-1 bg-blue-100 text-blue-800 text-xs rounded", children: topic }, topic))) })] })), (0, jsx_runtime_1.jsxs)("div", { className: "flex justify-between text-xs text-gray-500 mt-3", children: [doc.result.wordCount && (0, jsx_runtime_1.jsxs)("span", { children: [doc.result.wordCount, " words"] }), doc.result.language && (0, jsx_runtime_1.jsx)("span", { children: doc.result.language }), doc.result.sentiment && ((0, jsx_runtime_1.jsxs)("span", { className: "capitalize", children: ["Sentiment: ", doc.result.sentiment] }))] })] })), doc.status === 'failed' && doc.error && ((0, jsx_runtime_1.jsx)("div", { className: "bg-red-50 text-red-700 text-sm p-3 rounded", children: doc.error }))] }, doc.id)))] }))] }) }));
};
exports.DocumentProcessor = DocumentProcessor;
exports.default = exports.DocumentProcessor;
//# sourceMappingURL=DocumentProcessor.js.map