import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
import Icon from '../Icon/Icon.js';
import Tooltip from '../Tooltip/Tooltip.js';
import Typography from '../Typography/Typography.js';
import { useRef, useState, useEffect } from 'react';
import { truncateText } from '../../utils/truncateText/truncateText.js';

const RadioFilePreview = ({ selectedFile, onFileRemoveClick, onFileReplaceClick, selectedRadioOption, setShowDrawer, setFileContent, }) => {
    const fileInputRef = useRef(null);
    const wrapperRef = useRef(null);
    const [maxTextWidth, setMaxTextWidth] = useState();
    useEffect(() => {
        if (wrapperRef.current) {
            const fullWidth = wrapperRef.current.offsetWidth;
            setMaxTextWidth(fullWidth);
        }
    }, []);
    const getFileExtension = () => {
        if (!selectedFile)
            return '';
        const lastDotIndex = selectedFile.lastIndexOf('.');
        if (lastDotIndex === -1)
            return '';
        return selectedFile.substring(lastDotIndex);
    };
    const handleReplaceClick = () => {
        if (selectedRadioOption?.value === 'Test Data') {
            setShowDrawer?.(true);
            return;
        }
        fileInputRef.current?.click();
    };
    const handleFileChange = (event) => {
        const file = event.target.files?.[0];
        const readerFileData = new FileReader();
        if (file) {
            readerFileData.readAsText(file);
            readerFileData.onload = async () => {
                const fileContent = await readerFileData.result;
                if (setFileContent) {
                    setFileContent(fileContent);
                }
            };
            onFileReplaceClick?.(file);
        }
    };
    return (jsxs("div", { className: "ff-webservice-file-wrapper", ref: wrapperRef, children: [jsx("div", { className: "ff-file-info", children: jsx(Tooltip, { title: selectedFile, children: jsx(Typography, { lineHeight: "18px", fontWeight: "semi-bold", color: 'var(--text-color)', className: "ff-webservice-file-name", children: truncateText(selectedFile, maxTextWidth, 'pixel') }) }) }), jsx(Fragment, { children: jsx(Tooltip, { title: "Remove", children: jsx(Icon, { name: "close", height: 12, width: 12, hoverEffect: true, onClick: onFileRemoveClick }) }) }), jsxs(Fragment, { children: [jsx(Tooltip, { title: "Replace", children: jsx(Icon, { name: "replace_file", color: 'var(--icons-default-color)', height: 16, width: 16, hoverEffect: true, onClick: handleReplaceClick }) }), jsx("input", { type: "file", ref: fileInputRef, className: "ff-input-ref", onChange: handleFileChange, accept: getFileExtension() })] })] }));
};

export { RadioFilePreview as default };
//# sourceMappingURL=RadioFilePreview.js.map
