"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
/**
 * Document Analyzer Component
 *
 * A component that analyzes documents using the native_aijs SDK integration
 */
const react_1 = require("react");
const useAI_js_1 = __importDefault(require("../../hooks/useAI.js"));
const DocumentAnalyzer = ({ className = '', onAnalysisComplete }) => {
    // State
    const [document, setDocument] = (0, react_1.useState)('');
    const [detailed, setDetailed] = (0, react_1.useState)(false);
    const [categories, setCategories] = (0, react_1.useState)([]);
    const [newCategory, setNewCategory] = (0, react_1.useState)('');
    const [results, setResults] = (0, react_1.useState)(null);
    // Get AI capabilities from our hook
    const { analyzeDocument, status, isLoading, isError, error } = (0, useAI_js_1.default)({
        initialConfig: {
            model: 'claude-3-sonnet-20240229',
            temperature: 0.3
        },
        onError: (err) => {
            console.error('Analysis Error:', err);
        }
    });
    // Handle document text change
    const handleDocumentChange = (e) => {
        setDocument(e.target.value);
    };
    // Handle detailed analysis toggle
    const handleDetailedChange = (e) => {
        setDetailed(e.target.checked);
    };
    // Add a new category
    const handleAddCategory = () => {
        if (newCategory.trim() && !categories.includes(newCategory.trim())) {
            setCategories([...categories, newCategory.trim()]);
            setNewCategory('');
        }
    };
    // Remove a category
    const handleRemoveCategory = (category) => {
        setCategories(categories.filter(c => c !== category));
    };
    // Handle analysis submission
    const handleAnalyze = async (e) => {
        e.preventDefault();
        if (!document.trim() || isLoading)
            return;
        try {
            // Perform analysis
            const analysisResult = await analyzeDocument(document, {
                detailed,
                categories: categories.length > 0 ? categories : undefined
            });
            // Store results
            setResults(analysisResult);
            // Call callback if provided
            if (onAnalysisComplete) {
                onAnalysisComplete(analysisResult);
            }
        }
        catch (err) {
            console.error('Analysis failed:', err);
        }
    };
    // Helper to render sentiment badge
    const renderSentimentBadge = (sentiment) => {
        const badgeClasses = {
            positive: 'bg-green-100 text-green-800',
            neutral: 'bg-gray-100 text-gray-800',
            negative: 'bg-red-100 text-red-800'
        };
        return ((0, jsx_runtime_1.jsx)("span", { className: `px-2 py-1 rounded-full text-sm ${badgeClasses[sentiment]}`, children: sentiment }));
    };
    return ((0, jsx_runtime_1.jsxs)("div", { className: `bg-white rounded-lg shadow-md ${className}`, children: [(0, jsx_runtime_1.jsxs)("div", { className: "p-4 border-b", children: [(0, jsx_runtime_1.jsx)("h2", { className: "text-xl font-semibold", children: "Document Analyzer" }), (0, jsx_runtime_1.jsx)("p", { className: "text-gray-500", children: "Analyze documents using native_aijs" })] }), (0, jsx_runtime_1.jsxs)("div", { className: "p-4", children: [(0, jsx_runtime_1.jsxs)("form", { onSubmit: handleAnalyze, children: [(0, jsx_runtime_1.jsxs)("div", { className: "mb-4", children: [(0, jsx_runtime_1.jsx)("label", { className: "block mb-2 font-medium", children: "Document Text" }), (0, jsx_runtime_1.jsx)("textarea", { value: document, onChange: handleDocumentChange, className: "w-full p-3 border rounded-md h-48", placeholder: "Paste your document text here...", disabled: isLoading })] }), (0, jsx_runtime_1.jsx)("div", { className: "mb-4", children: (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center mb-2", children: [(0, jsx_runtime_1.jsx)("input", { type: "checkbox", id: "detailed", checked: detailed, onChange: handleDetailedChange, className: "mr-2", disabled: isLoading }), (0, jsx_runtime_1.jsx)("label", { htmlFor: "detailed", children: "Detailed Analysis" })] }) }), (0, jsx_runtime_1.jsxs)("div", { className: "mb-4", children: [(0, jsx_runtime_1.jsx)("label", { className: "block mb-2 font-medium", children: "Categories (Optional)" }), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center", children: [(0, jsx_runtime_1.jsx)("input", { type: "text", value: newCategory, onChange: (e) => setNewCategory(e.target.value), className: "flex-grow p-2 border rounded-l-md", placeholder: "Add a category...", disabled: isLoading }), (0, jsx_runtime_1.jsx)("button", { type: "button", onClick: handleAddCategory, className: "px-4 py-2 bg-blue-500 text-white rounded-r-md hover:bg-blue-600 disabled:bg-blue-300", disabled: !newCategory.trim() || isLoading, children: "Add" })] }), categories.length > 0 && ((0, jsx_runtime_1.jsx)("div", { className: "mt-2 flex flex-wrap gap-2", children: categories.map(category => ((0, jsx_runtime_1.jsxs)("span", { className: "inline-flex items-center px-2 py-1 bg-gray-100 rounded-full", children: [category, (0, jsx_runtime_1.jsx)("button", { type: "button", onClick: () => handleRemoveCategory(category), className: "ml-1 text-gray-500 hover:text-red-500", disabled: isLoading, children: "\u00D7" })] }, category))) }))] }), (0, jsx_runtime_1.jsx)("button", { type: "submit", className: "w-full px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600 disabled:bg-blue-300", disabled: !document.trim() || isLoading, children: isLoading ? 'Analyzing...' : 'Analyze Document' })] }), isError && error && ((0, jsx_runtime_1.jsxs)("div", { className: "mt-4 bg-red-100 text-red-800 p-3 rounded-lg", children: [(0, jsx_runtime_1.jsxs)("p", { className: "font-semibold", children: ["Error: ", error.code] }), (0, jsx_runtime_1.jsx)("p", { children: error.message })] })), results && ((0, jsx_runtime_1.jsxs)("div", { className: "mt-6 border-t pt-4", children: [(0, jsx_runtime_1.jsx)("h3", { className: "text-lg font-semibold mb-4", children: "Analysis Results" }), (0, jsx_runtime_1.jsxs)("div", { className: "mb-4", children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex justify-between items-center mb-2", children: [(0, jsx_runtime_1.jsx)("h4", { className: "font-medium", children: "Summary" }), results.sentiment && renderSentimentBadge(results.sentiment)] }), (0, jsx_runtime_1.jsx)("div", { className: "p-3 bg-gray-50 rounded-md", children: results.summary })] }), results.keyPoints && results.keyPoints.length > 0 && ((0, jsx_runtime_1.jsxs)("div", { className: "mb-4", children: [(0, jsx_runtime_1.jsx)("h4", { className: "font-medium mb-2", children: "Key Points" }), (0, jsx_runtime_1.jsx)("ul", { className: "list-disc pl-5 space-y-1", children: results.keyPoints.map((point, index) => ((0, jsx_runtime_1.jsx)("li", { children: point }, index))) })] })), results.categories && Object.keys(results.categories).length > 0 && ((0, jsx_runtime_1.jsxs)("div", { className: "mb-4", children: [(0, jsx_runtime_1.jsx)("h4", { className: "font-medium mb-2", children: "Categories" }), (0, jsx_runtime_1.jsx)("div", { className: "grid grid-cols-2 gap-2", children: Object.entries(results.categories).map(([category, score]) => ((0, jsx_runtime_1.jsxs)("div", { className: "flex justify-between", children: [(0, jsx_runtime_1.jsx)("span", { children: category }), (0, jsx_runtime_1.jsxs)("span", { className: "font-medium", children: [(score * 100).toFixed(1), "%"] })] }, category))) })] }))] }))] })] }));
};
exports.default = DocumentAnalyzer;
//# sourceMappingURL=DocumentAnalyzer.js.map