import { mergeProps } from 'zag-ripple';
import { trackSplit, track } from 'ripple';
import { useTreeViewContext } from './use-tree-view-context';
import { useTreeViewNodePropsContext } from './use-tree-view-node-props-context';
import { ark } from '../factory';
import type { HTMLProps, MaybeTracked, PolymorphicProps } from '../../types';

export interface TreeViewBranchTextBaseProps extends PolymorphicProps<'span'> {}
export interface TreeViewBranchTextProps extends HTMLProps<'span'>, TreeViewBranchTextBaseProps {}

export component TreeViewBranchText(props: MaybeTracked<TreeViewBranchTextProps>) {
  const [children, localProps] = trackSplit(props, ['children']);
  const treeView = useTreeViewContext();
  const nodeProps = useTreeViewNodePropsContext();

  let mergedProps = track(() => mergeProps(@treeView.getBranchTextProps(@nodeProps), @localProps));

  <ark.span {...@mergedProps}>
    <@children />
  </ark.span>
}
