import { mergeProps } from 'zag-ripple';
import { trackSplit, track } from 'ripple';
import { CheckboxApiContext } from './use-checkbox-context';
import type { UseCheckboxReturn } from './use-checkbox.ripple';
import { ark } from '../factory';
import type { HTMLProps, MaybeTracked, PolymorphicProps } from '../../types';

export interface CheckboxRootProviderBaseProps extends PolymorphicProps<'label'> {
  value: UseCheckboxReturn;
}
export interface CheckboxRootProviderProps extends HTMLProps<
  'label'
>, CheckboxRootProviderBaseProps {}

export component CheckboxRootProvider(props: MaybeTracked<CheckboxRootProviderProps>) {
  const [checkbox, children, rest] = trackSplit(props, ['value', 'children']);

  CheckboxApiContext.set(checkbox);

  let mergedProps = track(() => mergeProps(@checkbox.getRootProps(), @rest));

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