import { mergeProps } from 'zag-ripple';
import { trackSplit, track } from 'ripple';
import { useDrawerContext } from './use-drawer-context';
import { usePresenceContext } from '../presence/use-presence-context';
import { ark } from '../factory';
import type { HTMLProps, MaybeTracked, PolymorphicProps } from '../../types';

export interface DrawerContentBaseProps extends PolymorphicProps<'div'> {
  draggable?: boolean;
}
export interface DrawerContentProps extends HTMLProps<'div'>, DrawerContentBaseProps {}

export component DrawerContent(props: MaybeTracked<DrawerContentProps>) {
  const [children, draggable, localProps] = trackSplit(props, ['children', 'draggable']);
  const drawer = useDrawerContext();
  const presence = usePresenceContext();

  let mergedProps = track(
    () => mergeProps(
      @drawer.getContentProps({ draggable: @draggable ?? true }),
      @presence.getPresenceProps(),
      @localProps,
    ),
  );

  if (!@presence.unmounted) {
    <ark.div
      {...@mergedProps}
      {ref (el: HTMLDivElement) => {
        @presence.setNode(el);
      }}
    >
      <@children />
    </ark.div>
  }
}
