import { mergeProps } from '@zag-js/solid'
import { createSplitProps } from '../../utils/create-split-props.ts'
import { type HTMLProps, type PolymorphicProps, ark } from '../factory.tsx'
import { parts } from './segment-group.anatomy.ts'
import type { UseSegmentGroupReturn } from './use-segment-group.ts'
import { SegmentGroupProvider } from './use-segment-group-context.ts'

interface RootProviderProps {
  value: UseSegmentGroupReturn
}

export interface SegmentGroupRootProviderBaseProps extends PolymorphicProps<'div'> {}
export interface SegmentGroupRootProviderProps
  extends HTMLProps<'div'>, RootProviderProps, SegmentGroupRootProviderBaseProps {}

export const SegmentGroupRootProvider = (props: SegmentGroupRootProviderProps) => {
  const [{ value: segmentGroup }, localProps] = createSplitProps<RootProviderProps>()(props, ['value'])
  const mergedProps = mergeProps(() => segmentGroup().getRootProps(), parts.root.attrs, localProps)

  return (
    <SegmentGroupProvider value={segmentGroup}>
      <ark.div {...mergedProps} />
    </SegmentGroupProvider>
  )
}
