'use client'

import { mergeProps } from '@zag-js/react'
import { forwardRef } from 'react'
import { createSplitProps } from '../../utils/create-split-props.ts'
import {
  type RenderStrategyProps,
  RenderStrategyPropsProvider,
  splitRenderStrategyProps,
} from '../../utils/render-strategy.ts'
import { type HTMLProps, type PolymorphicProps, ark } from '../factory.ts'
import type { UseNavigationMenuReturn } from './use-navigation-menu.ts'
import { NavigationMenuProvider } from './use-navigation-menu-context.ts'

interface RootProviderProps {
  value: UseNavigationMenuReturn
}

export interface NavigationMenuRootProviderBaseProps extends RootProviderProps, RenderStrategyProps, PolymorphicProps {}
export interface NavigationMenuRootProviderProps extends HTMLProps<'nav'>, NavigationMenuRootProviderBaseProps {}

const splitRootProviderProps = createSplitProps<RootProviderProps>()

export const NavigationMenuRootProvider = forwardRef<HTMLElement, NavigationMenuRootProviderProps>((props, ref) => {
  const [renderStrategyProps, navigationMenuProps] = splitRenderStrategyProps(props)
  const [{ value: navigationMenu }, localProps] = splitRootProviderProps(navigationMenuProps, ['value'])
  const mergedProps = mergeProps(navigationMenu.getRootProps(), localProps)

  return (
    <NavigationMenuProvider value={navigationMenu}>
      <RenderStrategyPropsProvider value={renderStrategyProps}>
        <ark.nav {...mergedProps} ref={ref} />
      </RenderStrategyPropsProvider>
    </NavigationMenuProvider>
  )
})

NavigationMenuRootProvider.displayName = 'NavigationMenuRootProvider'
