"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InputContextProvider = InputContextProvider;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const useFileUploadStore_1 = require("../../../hooks/useFileUploadStore.js");
const cn_1 = require("../../../utils/cn.js");
const FileIcon_1 = require("../../atoms/FileIcon/index.js");
const AttachmentPicker_1 = require("../../organisms/AttachmentPicker/index.js");
const InputContext_1 = require("./InputContext.js");
const i18n_1 = require("./i18n/index.js");
require("./InputContext.css");
const b = (0, cn_1.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`.
 */
function InputContextProvider({ children, fileUpload, fileDialogTitle, }) {
    const { entries, addFiles, removeFile, reset, uploadedMetas } = (0, useFileUploadStore_1.useFileUploadStore)(fileUpload);
    const prepareFilesForSend = (0, react_1.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 = (0, react_1.useMemo)(() => ((0, jsx_runtime_1.jsx)(AttachmentPicker_1.AttachmentPicker, { uploadOnly: true, fileDialogProps: {
            title: fileDialogTitle !== null && fileDialogTitle !== void 0 ? fileDialogTitle : (0, i18n_1.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 = (0, react_1.useMemo)(() => entries.map((entry) => ({
        id: entry.id,
        content: ((0, jsx_runtime_1.jsxs)("span", { className: b('chip'), children: [(0, jsx_runtime_1.jsx)(FileIcon_1.FileIcon, { fileName: entry.file.name, mimeType: entry.file.type || undefined, size: "s" }), entry.file.name] })),
        onRemove: () => removeFile(entry.id),
    })), [entries, removeFile]);
    const value = (0, react_1.useMemo)(() => ({
        contextItems,
        attachmentContent,
        prepareFilesForSend,
        reset,
    }), [attachmentContent, contextItems, prepareFilesForSend, reset]);
    return (0, jsx_runtime_1.jsx)(InputContext_1.InputContext.Provider, { value: value, children: children });
}
//# sourceMappingURL=InputContextProvider.js.map
