import { createElement as _createElement } from "react";
import { Fragment as _Fragment, jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { forwardRef } from 'react';
import styled from 'styled-components';
import { useTestIds } from '../../hooks';
import { withTestIds } from '../../utils';
import { getTreeTestIds } from './Tree.test-ids';
export const StyledTreeListItem = styled.li ``;
export const StyledTreeList = styled.ul `
  &,
  & & {
    list-style: none;
    position: relative;
  }

  ${StyledTreeListItem} {
    position: relative;
  }
`;
const DefaultNodeRenderer = ({ data, subTree }) => {
    return (_jsxs(_Fragment, { children: [data, subTree] }));
};
let TreeList;
const TreeItem = ({ listItemProps, depth, nodes, nodeRenderer: NodeRenderer, ...restProps }) => {
    return (_jsx(StyledTreeListItem, { ...listItemProps, children: _jsx(NodeRenderer, { ...restProps, depth: depth, nodes: nodes, subTree: nodes && _jsx(TreeList, { depth: depth + 1, nodes: nodes, nodeRenderer: NodeRenderer }) }) }));
};
TreeList = forwardRef(function TreeListFunction({ nodes, depth, nodeRenderer, ...restProps }, ref) {
    const hasParentSibling = nodes.some(node => !!node.nodes);
    return nodes.length > 0 ? (_jsx(StyledTreeList, { ...restProps, ref: ref, role: depth === 0 ? 'tree' : 'group', children: nodes.map(node => (_createElement(TreeItem, { ...node, key: node.id, depth: depth, nodeRenderer: nodeRenderer, hasParentSibling: hasParentSibling }))) })) : null;
});
const Tree = forwardRef(function Tree({ testId, nodeRenderer = DefaultNodeRenderer, ...restProps }, ref) {
    const testIds = useTestIds(testId, getTreeTestIds);
    return (_jsx(TreeList, { "data-testid": testIds.root, ...restProps, ref: ref, depth: 0, nodeRenderer: nodeRenderer }));
});
export default withTestIds(Tree, getTreeTestIds);
//# sourceMappingURL=Tree.js.map