import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import './FileDropzone.scss';
import Icon from '../Icon';
import Tooltip from '../Tooltip';
import Typography from '../Typography';
import { useEffect, useRef, useState } from 'react';
import { truncateText } from '../../utils/truncateText/truncateText';
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 default RadioFilePreview;
//# sourceMappingURL=RadioFilePreview.js.map