import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React from 'react';
import { prepareData } from '../../../utils/TableCell/TableCell';
import Checkbox from '../../Checkbox';
import RadioButton from '../../RadioButton';
import { checkEmpty } from '../../../utils/checkEmpty/checkEmpty';
import AddModule from '../../EditLabel';
import Arrow from '../../../assets/icons/arrows_down_icon.svg?react';
import Spinner from '../../../assets/icons/spinner.svg?react';
import Tooltip from '../../Tooltip';
import { formatCellData } from '../Utils/formatDataCell';
const renderSpaces = (level, parentSiblings = [], isLast, isContainer) => {
    let siblingsArray = parentSiblings;
    let isLastNode = isLast;
    if (checkEmpty(parentSiblings)) {
        if (!isNaN(level)) {
            siblingsArray = Array(level).fill(true);
        }
    }
    return (_jsx("div", { className: "tree-table-space-container", children: siblingsArray?.map((line, i) => (_jsx("span", { className: `tree-table-space-block tree-table-space-block-${i} ${!line ? 'no-lines' : ''} ${isLastNode && i === level - 1 ? 'last-node' : ''} ${isContainer ? 'folder' : ''}` }, i))) }));
};
const TableCell = React.memo(({ treeRowRef, col, node, selected, select, onCheckBoxChange, onToggleExpand, onAddConfirm, onAddCancel, handleEditFieldError, isExpanding, columnTextColor, hideOnDisable, scriptLengthTruncate, addModuleInputWidth, addModuleSelectWidth, disableEditLabelConfirmIcon, }) => {
    //Todo uncomment the following code when we are highlighting the tree table nodes on hover
    // useEffect(() => {
    //   const handleHover = (event: MouseEvent, isHovering: boolean) => {
    //     const target = event.target as HTMLElement;
    //     const levelClass = Array.from(target.classList).find((cls) =>
    //       cls.startsWith('tree-table-space-block-')
    //     );
    //     if (levelClass) {
    //       const level = levelClass.split('-').pop();
    //       // Check if level is valid before selecting elements
    //       if (level) {
    //         const sameLevelBlocks = document.querySelectorAll(
    //           `.tree-table-space-block-${level}`
    //         ) as NodeListOf<HTMLElement>;
    //         sameLevelBlocks.forEach((block) => {
    //           if (isHovering) {
    //             block.classList.add('tree-table-hovered');
    //           } else {
    //             block.classList.remove('tree-table-hovered');
    //           }
    //         });
    //       }
    //     }
    //   };
    //   const spaceBlocks = document.querySelectorAll(
    //     '.tree-table-space-block'
    //   ) as NodeListOf<HTMLElement>;
    //   spaceBlocks.forEach((block) => {
    //     block.addEventListener('mouseenter', (e) => handleHover(e, true));
    //     block.addEventListener('mouseleave', (e) => handleHover(e, false));
    //   });
    //   return () => {
    //     spaceBlocks.forEach((block) => {
    //       block.removeEventListener('mouseenter', (e) => handleHover(e, true));
    //       block.removeEventListener('mouseleave', (e) => handleHover(e, false));
    //     });
    //   };
    // }, []);
    return (_jsxs("td", { className: `ff-table-tree-td ${col.isTree && node.container ? 'folder' : ''}`, style: {
            color: `${columnTextColor || 'var(--ff-primary-text-color)'}`,
        }, children: [col.isTree &&
                renderSpaces(node.hierarchy, node.parentSiblings, node.lastChild, node.container), _jsxs("div", { className: "tree-title-container", children: [col.isTree && (_jsx("span", { className: `tree-table-space-block  last-block last-block tree-table-space-block-${node.hierarchy} ${node?.expanded ? 'tree-row-expanded' : 'tree-row-collapsed'} ${node.container && node.expandable ? '' : 'no-folder'}`, children: node.container && node.expandable && (_jsxs("span", { onClick: () => onToggleExpand(node), children: [' ', isExpanding ? _jsx(Spinner, {}) : _jsx(Arrow, {})] })) })), _jsxs("span", { className: `tree-table-td-content ${col.isTree && node.container ? 'folder' : ''}`, children: [!node?.isNewNode && (_jsxs(_Fragment, { children: [col.isTree &&
                                        select === 'checkbox' &&
                                        !(hideOnDisable && node.unselectable) && (_jsx("span", { className: "tree-table-td-content-select", children: node.selectedStatus === 'none' ? (_jsx(Tooltip, { title: !!node.machine
                                                ? 'This script is assigned to another environment. Selecting it will reassign it to the current environment.'
                                                : undefined, children: _jsx(Checkbox, { checked: node.selectedStatus === 'completely', partial: node.selectedStatus === 'partially', onChange: (e) => onCheckBoxChange(e, node), disabled: node.unselectable ||
                                                    node.state === 'REVIEW' ||
                                                    node?.isDisable === true, isDefaultHover: node.selectedStatus === 'none' && !!node.machine }) })) : (_jsx(Checkbox, { checked: node.selectedStatus === 'completely', partial: node.selectedStatus === 'partially', onChange: (e) => onCheckBoxChange(e, node), disabled: node.unselectable ||
                                                node.state === 'REVIEW' ||
                                                node?.isDisable === true })) })), col.isTree &&
                                        select === 'radio' &&
                                        !(hideOnDisable && node.unselectable) && (_jsx("span", { className: "tree-table-td-content-select", children: _jsx(RadioButton, { name: node.key, checked: selected.includes(node.key), value: node.key, onChange: (e) => onCheckBoxChange(e, node), disabled: node.unselectable ||
                                                node.state === 'REVIEW' ||
                                                node?.isDisable === true }) }))] })), node.isNewNode && col.isTree ? (_jsx(AddModule, { onCancel: onAddCancel, onConfirm: onAddConfirm, value: node.value, label: node?.label, optionsList: node.options, selectedOption: node.selectedOption, withDropdown: node.type === 'inputWithDropdown', handleCustomError: handleEditFieldError, inputFieldWidth: addModuleInputWidth, selectFieldWidth: addModuleSelectWidth, confirmIconTooltip: node.confirmIconTooltip, cancelIconTooltip: node.cancelIconTooltip, isOnBlurTrue: true, isDisable: { confirm: disableEditLabelConfirmIcon } })) : (_jsx("span", { className: "tree-table-td-content-text", children: formatCellData(prepareData(node, col), scriptLengthTruncate) }))] })] }), col.actions && !node?.isNewNode && (_jsx("div", { className: `table-tree-row-action`, children: (() => {
                    return col.actions(node, treeRowRef);
                })() }))] }));
});
export default TableCell;
//# sourceMappingURL=TableCell.js.map