import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useMemo } from 'react';
import { useFileUploadStore } from "../../../hooks/useFileUploadStore.js";
import { block } from "../../../utils/cn.js";
import { FileIcon } from "../../atoms/FileIcon/index.js";
import { AttachmentPicker } from "../../organisms/AttachmentPicker/index.js";
import { InputContext } from "./InputContext.js";
import { i18n } from "./i18n/index.js";
import "./InputContext.css";
const b = block('input-context');
function fileToBase64(file) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result);
        reader.onerror = reject;
    });
}
/**
 * Provides prompt input attachment state: file queue, header context chips, and `AttachmentPicker`.
 */
export function InputContextProvider({ children, fileUpload, fileDialogTitle, }) {
    const { entries, addFiles, removeFile, reset, uploadedMetas } = useFileUploadStore(fileUpload);
    const prepareFilesForSend = useCallback(async (inputAttachments) => {
        const storeFiles = entries.map((e) => e.file);
        const allFiles = [...storeFiles, ...(inputAttachments !== null && inputAttachments !== void 0 ? inputAttachments : [])];
        const imageFiles = allFiles.filter((f) => f.type.startsWith('image/'));
        const otherFiles = allFiles.filter((f) => !f.type.startsWith('image/'));
        const base64Images = await Promise.all(imageFiles.map(fileToBase64));
        const metaByName = new Map(uploadedMetas.map((m) => [m.name, m]));
        const fileAttachments = otherFiles.map((f) => {
            var _a, _b, _c;
            const meta = metaByName.get(f.name);
            return {
                id: (_a = meta === null || meta === void 0 ? void 0 : meta.id) !== null && _a !== void 0 ? _a : f.name,
                name: (_b = meta === null || meta === void 0 ? void 0 : meta.name) !== null && _b !== void 0 ? _b : f.name,
                mimeType: (_c = meta === null || meta === void 0 ? void 0 : meta.mimeType) !== null && _c !== void 0 ? _c : (f.type || undefined),
            };
        });
        const fileIds = fileAttachments.map((f) => f.id);
        const perSendFileParams = Object.assign({ fileAttachments, fileNames: allFiles.map((f) => f.name) }, (fileIds.length > 0 ? { fileIds } : {}));
        return {
            base64Images,
            perSendFileParams,
        };
    }, [entries, uploadedMetas]);
    const attachmentContent = useMemo(() => (_jsx(AttachmentPicker, { uploadOnly: true, fileDialogProps: {
            title: fileDialogTitle !== null && fileDialogTitle !== void 0 ? fileDialogTitle : i18n('dialog-title'),
            multiple: true,
            onCancel: reset,
            onAdd: addFiles,
            files: entries.map((entry) => ({
                id: entry.id,
                name: entry.file.name,
                size: entry.file.size,
                mimeType: entry.file.type || undefined,
                status: (() => {
                    if (entry.status === 'uploading')
                        return 'loading';
                    if (entry.status === 'done')
                        return 'success';
                    if (entry.status === 'error')
                        return 'error';
                    return undefined;
                })(),
                onRemove: () => removeFile(entry.id),
            })),
        } })), [addFiles, entries, fileDialogTitle, removeFile, reset]);
    const contextItems = useMemo(() => entries.map((entry) => ({
        id: entry.id,
        content: (_jsxs("span", { className: b('chip'), children: [_jsx(FileIcon, { fileName: entry.file.name, mimeType: entry.file.type || undefined, size: "s" }), entry.file.name] })),
        onRemove: () => removeFile(entry.id),
    })), [entries, removeFile]);
    const value = useMemo(() => ({
        contextItems,
        attachmentContent,
        prepareFilesForSend,
        reset,
    }), [attachmentContent, contextItems, prepareFilesForSend, reset]);
    return _jsx(InputContext.Provider, { value: value, children: children });
}
//# sourceMappingURL=InputContextProvider.js.map
