import { trackSplit } from 'ripple';
import type { HTMLProps, MaybeTracked, PolymorphicProps } from '../../types';
import { ark } from '../factory';
import { listboxAnatomy } from './listbox.anatomy';
import { useListboxContext } from './use-listbox-context';

const parts = listboxAnatomy.build();

export interface ListboxEmptyBaseProps extends PolymorphicProps<'div'> {}
export interface ListboxEmptyProps extends HTMLProps<'div'>, ListboxEmptyBaseProps {}

export component ListboxEmpty(props: MaybeTracked<ListboxEmptyProps>) {
  const [children, localProps] = trackSplit(props, ['children']);
  const listbox = useListboxContext();

  if (@listbox.collection.size === 0) {
    <ark.div {...parts.empty.attrs} {...@localProps} role="presentation">
      <@children />
    </ark.div>
  }
}
