import { NestedTableNodeState } from './NestedTableNodeState';
import { FiltersMap, KeyedItem } from '@wix/bex-core';
import { NestedTableLeafNodeState } from './NestedTableLeafNodeState';
import { NestedTableNodeStatePublicAPI } from './NestedTableNodeStatePublicAPI';

export function getNodeOrLeaf<C extends string, T, F extends FiltersMap>(
  parentNode: NestedTableNodeState<C, T, F>,
  keyedItem: KeyedItem<T>,
): NestedTableNodeStatePublicAPI<C, any, any> {
  const node = parentNode.parentKeyToChildrenMap.get(keyedItem.key);

  return (
    node ??
    new NestedTableLeafNodeState({
      parentNode,
      parent: keyedItem,
    })
  );
}
