text: /*eslint-disable*/
  /* eslint-disable react/destructuring-assignment */
  /* eslint-disable react/no-string-refs */
  import React from 'react';
  import { connect } from 'react-redux';
  import { Route, Link, Redirect } from 'react-router-dom';
  import classnames from 'classnames';
  import SideNavigation from '../common/side-navigation/side-navigation';
  import HeaderView from '../common/header-view/header-view';
  import VulnerabilityStatsView from '../common/top-stats-panel-view/vulnerability-stats-view';
  import { ADMIN_SIDE_NAV_MENU_COLLECTION, USER_SIDE_NAV_MENU_COLLECTION } from '../../constants/menu-collection';
  import { getUserRole } from '../../helpers/auth-helper';
  import VulnerabilityScanView from './scan-index';
  import TopExploitableVulnerabilities from './top-exploitable-vulnerabilities';
  import  RuntimeBom  from './runtime-bom';
  import {
    getCVESeverityReportAction,
    breadcrumbChange,
  } from '../../actions/app-actions';
  import pollable from '../common/header-view/pollable';
  
  const menu = [{
    id: 'vulnerabilities',
    displayName: 'Most Exploitable Vulnerabilities',
    component: TopExploitableVulnerabilities,
  }, {
    id: 'scans',
    displayName: 'Vulnerability Scans',
    component: VulnerabilityScanView,
  }, {
    id: 'bom',
    displayName: 'Runtime BOM',
    component: RuntimeBom,
  }];
  
  function getActiveRouteId(routePath) {
    return menu.find(item => {
      return routePath?.toLowerCase?.()?.endsWith?.(item.id);
    })?.id ?? menu[0].id;
  }
  
  class VulnerabilityHome extends React.Component {
    constructor(props) {
      super(props);
      this.sideNavMenuCollection = (getUserRole() === 'admin') ? ADMIN_SIDE_NAV_MENU_COLLECTION : USER_SIDE_NAV_MENU_COLLECTION;
      this.state = {
        activeMenu: this.sideNavMenuCollection[0],
      };
    }
  
    componentDidMount() {
      const { registerPolling, startPolling } = this.props;
      registerPolling(this.getReport);
      startPolling();
      this.props.dispatch(breadcrumbChange([{ name: 'Vulnerability' }]));
    }
  
    UNSAFE_componentWillReceiveProps(newProps) {
      if ((newProps.isLicenseActive && !newProps.isLicenseExpired)
        && (newProps.licenseResponse.license_status === 'expired' || newProps.licenseResponse.license_status === 'hosts_exceeded')) {
        this.setState({
          licenseResponse: newProps.licenseResponse,
          isLicenseExpiryModalVisible: true
        });
      } else {
        this.setState({
          isLicenseExpiryModalVisible: false
        });
      }
    }
  
    componentWillUnmount() {
      const { stopPolling } = this.props;
      stopPolling();
    }
  
    getReport(pollParams) {
      const {
        globalSearchQuery,
        alertPanelHistoryBound = this.props.alertPanelHistoryBound || {},
        initiatedByPollable,
      } = pollParams;
      const { dispatch, nodeId, checkType } = this.props;
      const params = {
        nodeId,
        checkType,
        lucene_query: globalSearchQuery,
        // Conditionally adding number and time_unit fields
        ...(alertPanelHistoryBound.value
          ? { number: alertPanelHistoryBound.value.number } : {}),
        ...(alertPanelHistoryBound.value
          ? { time_unit: alertPanelHistoryBound.value.time_unit } : {}),
        initiatedByPollable,
      };
      return dispatch(getCVESeverityReportAction(params));
    }
  
  
    render() {
      const {
        isSideNavCollapsed,
        isFiltersViewVisible,
        match,
        location
      } = this.props;
      const { isLicenseExpiryModalVisible } = this.state;
      const divClassName = classnames({
        'collapse-side-nav': isSideNavCollapsed,
        'expand-side-nav': !isSideNavCollapsed,
        'show-filters-view': isFiltersViewVisible,
        'hide-filters-view': !isFiltersViewVisible,
      });
      const contentClassName = classnames(
        'summary',
      );
      return (
        <div className="cve-summary-view">
          <SideNavigation
            navMenuCollection={this.sideNavMenuCollection}
            activeMenu={this.state.activeMenu}
          />
          <div ref="vulnerabilityResizeRef" style={{ overflow: 'hidden' }}>
            <HeaderView />
            <div className={divClassName}>
              <VulnerabilityStatsView activeTab={getActiveRouteId(location.pathname)}/>
            </div>
          </div>
          <div className={contentClassName}>
            <div className="tab-links">
              <div className="df-tabs">
                <div className="tabheading">
                  <ul>
                    {menu.map(menuItem => (
                      <Route
                        key={menuItem.id}
                        path={`${match.path}/${menuItem.id}`}
                        children={({ match: linkMatch }) => (
                          <li key={menuItem.id} className={classnames('tab', { active: linkMatch })}>
                            <Link
                              to={`${match.url}/${menuItem.id}`}
                            >
                              {menuItem.displayName}
                            </Link>
                          </li>
                        )}
                      />
                    ))}
                  </ul>
                </div>
                {menu.map(menuItem => (
                  <Route
                    key={menuItem.id}
                    exact
                    path={`${match.path}/${menuItem.id}`}
                    render={(props) => <menuItem.component {...props} />}
                  />
                ))}
                <Route
                  exact
                  path={match.path}
                  render={() => (
                    <Redirect
                      to={`${match.url}/${menu[0].id}`}
                    />
                  )}
                />
              </div>
            </div>
          </div>
        </div>
      );
    }
  }
  
  function mapStateToProps(state) {
    return {
      isSideNavCollapsed: state.get('isSideNavCollapsed'),
      isFiltersViewVisible: state.get('isFiltersViewVisible'),
    };
  }
  export default connect(mapStateToProps)(pollable({
    pollingIntervalInSec: 5,
  })(VulnerabilityHome));
  