import { jsx, jsxs } from 'react/jsx-runtime';
import React__default, { useRef, useState, useEffect } from 'react';
import { r as reactArcherExports } from '../../_virtual/react-archer.js';
import MachineInstances from './BranchComponents/MachineInstances.js';
import Typography from '../Typography/Typography.js';
import { checkEmpty } from '../../utils/checkEmpty/checkEmpty.js';

const ConnectingBranch = ({ data: machineData, noOfScripts, getChildNodeComponent, getParentNodeComponent, getParentNodeComponentActionItems, childNodeCurveHeight = 1.28, isReadOnlyMode = false, hideNodeAtLast = true, }) => {
    const datasetRef = useRef(null);
    const [datasetHeight, setDatasetHeight] = useState(0);
    const [curveHeights, setCurveHeights] = useState([]);
    const itemRefs = useRef([]);
    const calculateVerticalLineHeight = (machineInstances, datasetHeight, gap) => {
        if (machineInstances.length === 1 && (isReadOnlyMode || hideNodeAtLast)) {
            return 0;
        }
        const totalDataSets = machineInstances.reduce((acc, instance, index) => acc +
            (index !== machineInstances.length - 1 ||
                !(isReadOnlyMode || hideNodeAtLast)
                ? instance.runLevelExecutionDataSets?.length || 0
                : 0), 0);
        return totalDataSets * (datasetHeight + gap);
    };
    useEffect(() => {
        if (datasetRef.current) {
            setDatasetHeight(datasetRef.current.offsetHeight);
        }
    }, [machineData]);
    const calculateCurveHeight = () => {
        const heights = itemRefs.current.map((_, index) => {
            if (index === 0)
                return 0;
            return datasetHeight * childNodeCurveHeight;
        });
        setCurveHeights(heights);
    };
    useEffect(() => {
        calculateCurveHeight();
    }, [datasetHeight, machineData]);
    const calculateRowSpan = (machine) => {
        const baseRows = 1;
        const childrenRows = machine.machineInstances?.length + 1 || 0;
        return baseRows + childrenRows;
    };
    let currentRow = 1;
    const calculateRowSpanOnlyView = (parentNodeData) => {
        if (!parentNodeData?.machineInstances ||
            checkEmpty(parentNodeData.machineInstances)) {
            return 1;
        }
        return parentNodeData.machineInstances.length;
    };
    const totalRows = machineData?.machines?.selectedMachines.reduce((sum, parentNodeData) => {
        if (!isReadOnlyMode || !hideNodeAtLast) {
            return sum + calculateRowSpan(parentNodeData);
        }
        else {
            return sum + calculateRowSpanOnlyView(parentNodeData);
        }
    }, 0);
    const centerRow = Math.round(totalRows / 2);
    let dynamicTop = (centerRow === 1 && isReadOnlyMode && totalRows === 2) ||
        (centerRow === 1 && !isReadOnlyMode && totalRows === 2);
    return (jsx("div", { className: "connecting-branch-container", children: jsx(reactArcherExports.ArcherContainer, { strokeColor: "var(--brand-color)", strokeWidth: 1, startMarker: true, endMarker: false, noCurves: true, children: jsxs("div", { className: "ff-connecting-branch-grid-container", children: [jsx(reactArcherExports.ArcherElement, { id: "rootNode", children: jsx("div", { className: "ff-grid-item ff-dynamic-item", style: {
                                gridRow: centerRow,
                                top: dynamicTop ? '44px' : '',
                            }, children: jsx("div", { className: "ff-script-count-container", children: jsxs("div", { className: "ff-script-count-box", children: [jsx(Typography, { children: noOfScripts }), jsx(Typography, { children: "Scripts" })] }) }) }) }), machineData?.machines?.selectedMachines.map((parentNodeData, parentNodeIndex) => {
                        const rowStart = currentRow;
                        const rowSpan = calculateRowSpan(parentNodeData);
                        currentRow += rowSpan;
                        const gap = 16;
                        const verticalLineHeight = calculateVerticalLineHeight(parentNodeData?.machineInstances, datasetHeight, gap);
                        return (jsx(React__default.Fragment, { children: jsxs("div", { className: "ff-selectedMachines", style: {
                                    gridRow: `${rowStart} / span ${rowSpan}`,
                                }, children: [jsx("div", { className: "ff-selectedMachines-input", children: jsxs("div", { className: `ff-input-box-container ${!parentNodeData?.machineInstances.length
                                                ? 'ff-input-box-aligned'
                                                : ''}`, children: [jsx(reactArcherExports.ArcherElement, { id: `parentnode${parentNodeIndex}`, relations: [
                                                        {
                                                            targetId: 'rootNode',
                                                            targetAnchor: 'left',
                                                            sourceAnchor: 'left',
                                                        },
                                                    ], children: getParentNodeComponent({
                                                        isMultiSelect: false,
                                                        parentNodeData,
                                                        parentNodeIndex,
                                                    }) }), getParentNodeComponentActionItems({
                                                    parentNodeData,
                                                    parentNodeIndex,
                                                })] }) }), jsx(MachineInstances, { parentNodeIndex: parentNodeIndex, parentNodeData: parentNodeData, verticalLineHeight: verticalLineHeight, datasetHeight: datasetHeight, curveHeights: curveHeights, itemRefs: itemRefs, datasetRef: datasetRef, getChildNodeComponent: getChildNodeComponent, isReadOnlyMode: isReadOnlyMode, hideNodeAtLast: hideNodeAtLast })] }) }, parentNodeIndex));
                    }), !isReadOnlyMode && (jsx("div", { className: "ff-select-machine-input", style: {
                            gridRow: `${currentRow}`,
                        }, children: jsx(reactArcherExports.ArcherElement, { id: "lastNode", relations: [
                                {
                                    targetId: 'rootNode',
                                    targetAnchor: 'left',
                                    sourceAnchor: 'left',
                                },
                            ], children: jsx("div", { children: getParentNodeComponent({ isMultiSelect: true }) }) }) }))] }) }) }));
};

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