import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import classNames from 'classnames';
import { checkEmpty } from '../../utils/checkEmpty/checkEmpty';
import Button from '../Button';
import Icon from '../Icon';
import Select from '../Select';
import Typography from '../Typography';
import './SequentialConnectingBranch.scss';
import { useEffect, useRef, useState } from 'react';
import ConnectingBranches from './components/ConnectingBranches/ConnectingBranches';
import Tooltip from '../Tooltip';
import DataSetTooltip from './components/DatasetTooltip/DataSetTooltip';
import { EnvironmentVariableMapsContext } from './context/EnvironmentVariableMapsContext';
const SequentialConnectingBranch = ({ machineInstances = [], machineColumnWidth = 180, machineColumnCount = 1, selectedMachine = {}, machineOptionsList = [], onHandleSelect = () => { }, onAddBrowserInstance = () => { }, onAddRunBrowserInstance = () => { }, onDeleteBrowserInstance = () => { }, onUpdateDataSetList = () => { }, onDeleteMachineInstance = () => { }, dataSetValues = {
    testDataSetId: '',
    globalVariableSetId: '',
    peVariableSetId: '',
    globalVariableSetName: '',
    peVariableSetName: '',
    testDataSetName: '',
}, onUpdateAddBrowserInstance = () => { }, addInstanceLabel = 'Environment', placeholder = 'Choose Machine', scriptType = 'Automation', projectType = 'Web', readOnly = false, zIndex = 99999, maxRunCount = 35, environmentVariableMaps, isRequired = false, }) => {
    const [selectedMachineInstance, setSelectedMachineInstance] = useState({});
    const selectButtonRef = useRef(null);
    const defaultMachineColumnCount = Math.max(machineColumnCount, 1);
    const defaultMachineColumnWidth = Math.max(machineColumnWidth, 180);
    const [machineBranchInstances, setMachineBranchInstances] = useState([]);
    const isMachineInstances = checkEmpty(machineInstances);
    let isMachineSelected = checkEmpty(selectedMachine.value?.clientId);
    if (scriptType === 'Manual') {
        isMachineSelected = checkEmpty(selectedMachine.value);
    }
    const getMachineStatusColor = (machineStatus = '') => {
        if (machineStatus.toLowerCase().includes('inactive'))
            return 'var(--ff-machine-inactive-status-color)';
        if (machineStatus.toLowerCase().includes('running'))
            return 'var(--ff-machine-running-status-color)';
        if (machineStatus.toLowerCase().includes('active'))
            return 'var(--ff-machine-active-status-color)';
        return 'var(--ff-machine-active-status-color))';
    };
    useEffect(() => {
        setSelectedMachineInstance(selectedMachine);
    }, [selectedMachine]);
    useEffect(() => {
        if (readOnly) {
            setMachineBranchInstances(machineInstances);
        }
        else {
            setMachineBranchInstances([...machineInstances, {}]);
        }
    }, [machineInstances]);
    const hideDataSet = scriptType.toLowerCase() !== 'manual';
    const removeKeysContainingSubstring = (obj, substring) => {
        return Object.fromEntries(Object.entries(obj).filter(([key]) => !key.includes(substring)));
    };
    const filteredDataSetValues = removeKeysContainingSubstring(dataSetValues, 'Id');
    const showBranchPoint = (readOnly && !isMachineInstances) ||
        (!readOnly && isMachineInstances) ||
        (!readOnly && !isMachineInstances);
    return (_jsx(EnvironmentVariableMapsContext.Provider, { value: environmentVariableMaps, children: _jsxs("div", { className: "ff-sequential-connecting-branch-wrapper", children: [_jsxs("div", { className: "ff-connecting-select-branch-wrapper", children: [_jsxs("div", { className: "ff-select-branch-wrapper", children: [_jsx(Select, { onChange: onHandleSelect, optionsList: machineOptionsList, label: placeholder, required: isRequired, showLabel: true, className: "ff-sequential-select-branch", width: '240px', selectedOption: selectedMachineInstance, valueAccessor: 'name', optionsRequired: !readOnly, disableInput: readOnly, optionZIndex: zIndex }), !isMachineSelected && (_jsxs("div", { className: classNames({
                                        'ff-select-branch-arrow': isMachineInstances,
                                        'ff-select-branch-arrow-down': !isMachineInstances,
                                    }), children: [showBranchPoint && (_jsx("div", { className: "ff-select-branch-point" })), _jsxs("div", { className: "ff-select-branch-arrow", children: [!readOnly && (_jsx("div", { className: "ff-branch-arrow-wrapper", children: isMachineInstances && (_jsx("div", { className: "ff-branch-arrow" })) })), isMachineInstances && !readOnly && (_jsx(_Fragment, { children: _jsx(Button, { variant: "tertiary", label: scriptType === 'Manual'
                                                            ? 'Machine'
                                                            : 'Environment', iconName: 'plus_icon', iconPosition: 'left', size: "small", className: "branch-button", ref: selectButtonRef, onClick: () => onAddBrowserInstance('ff-sequential-branch-button'), id: "ff-sequential-branch-button" }) }))] })] }))] }), !isMachineSelected && (_jsxs("div", { className: "ff-select-scope-datalist", children: [_jsx("div", { className: "ff-scope-wrapper", children: _jsx(Typography, { as: "span", color: getMachineStatusColor(selectedMachine.value?.status), className: "ff-scope-text", children: selectedMachine.value?.status }) }), _jsxs("div", { className: "ff-datalist-wrapper", children: [hideDataSet && (_jsx("div", { id: "ff-sequential-machine-datalist", onClick: () => {
                                                if (readOnly)
                                                    return;
                                                onUpdateDataSetList('ff-sequential-machine-datalist', dataSetValues);
                                            }, children: _jsxs(Tooltip, { placement: "bottom", title: _jsx("div", { children: _jsx(DataSetTooltip, { datSetToolTip: filteredDataSetValues }) }), style: { display: 'flex', alignItems: 'center' }, children: [_jsx(Icon, { name: "datalist_icon", className: "ff-dataset-icon", hoverEffect: true }), _jsx(Typography, { as: "span", color: "var(--ff-connecting-branch-color)", className: "ff-datalist-text", children: "Data Set List" })] }) })), !readOnly && (_jsx(Tooltip, { title: "Delete", placement: "bottom", children: _jsx(Icon, { name: "delete", className: "ff-run-delete-icon", color: "var(--ff-connecting-branch-delete-color)", onClick: onDeleteMachineInstance, hoverEffect: true }) }))] })] }))] }), _jsx("div", { className: "ff-sequential-branches-wrapper", children: !isMachineInstances && (_jsx(ConnectingBranches, { machineBranchInstances: machineBranchInstances, machineColumnCount: defaultMachineColumnCount, machineColumnWidth: defaultMachineColumnWidth, onAddBrowser: onAddBrowserInstance, onDeleteBrowser: onDeleteBrowserInstance, onAddRunBrowser: onAddRunBrowserInstance, onUpdateDataSetList: onUpdateDataSetList, onUpdateAddBrowserInstance: onUpdateAddBrowserInstance, addInstanceLabel: addInstanceLabel, scriptType: scriptType, projectType: projectType, readOnly: readOnly, maxRunCount: maxRunCount })) })] }) }));
};
export default SequentialConnectingBranch;
//# sourceMappingURL=SequentialConnectingBranch.js.map