import React, { createRef, isValidElement, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import { isEmpty } from 'lodash';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import matchSorter from 'match-sorter';
import Wrapper from './Wrapper';
import List from './List';
import Search from './Search';
import LeftMenuLink from '../LeftMenuLink';
import LeftMenuSubList from '../LeftMenuSubList';
function LeftMenuList({ customLink, links, title }) {
const [search, setSearch] = useState('');
const [showSearch, setShowSearch] = useState(false);
const ref = createRef();
useEffect(() => {
if (showSearch && ref.current) {
ref.current.focus();
}
}, [ref, showSearch]);
const { Component, componentProps } = customLink || {
Component: null,
componentProps: {},
};
const toggleSearch = () => setShowSearch(!showSearch);
const handleClose = () => {
clearSearch();
toggleSearch();
};
const clearSearch = () => {
setSearch('');
};
const handleChange = ({ target: { value } }) => {
setSearch(value);
};
const hasChildObject = () => links.some(link => !isEmpty(link.links));
const getCount = () => {
if (hasChildObject()) {
return links.reduce((acc, current) => {
return acc + current.links.length;
}, 0);
}
return links.length;
};
const getList = () => {
if (hasChildObject()) {
return links.map(link => {
return {
...link,
links: matchSorter(link.links, search, { keys: ['title'] }),
};
});
}
return matchSorter(links, search, { keys: ['title'] });
};
const getTitle = () =>
getCount() > 1 ? `${title.id}plural` : `${title.id}singular`;
const renderCompo = (link, i) => {
const { links, name, title, ...rest } = link;
if (links) {
const isSearching = !isEmpty(search);
return (