import * as _ from 'lodash';

export function findMenuItemByPath(menu: any, targetTo: any) {
  let result;
  _.forEach(menu, (item): any => {
    if (item.label && item.label.props && item.label.props.to === targetTo) {
      result = item;
      return false; // exit the loop
    }

    if (item.children) {
      const nestedResult = findMenuItemByPath(item.children, targetTo);
      if (nestedResult) {
        result = nestedResult;
        return false; // exit the loop
      }
    }
  });

  return result || undefined; // return result or undefined explicitly
}
