import { mergeProps } from '@zag-js/solid'
import { createSplitProps } from '../../utils/create-split-props.ts'
import { type HTMLProps, type PolymorphicProps, ark } from '../factory.tsx'
import { type UseTagsInputProps, useTagsInput } from './use-tags-input.ts'
import { TagsInputProvider } from './use-tags-input-context.ts'

export interface TagsInputRootBaseProps extends UseTagsInputProps, PolymorphicProps<'div'> {}
export interface TagsInputRootProps extends HTMLProps<'div'>, TagsInputRootBaseProps {}

export const TagsInputRoot = (props: TagsInputRootProps) => {
  const [useTagsInputProps, localProps] = createSplitProps<UseTagsInputProps>()(props, [
    'addOnPaste',
    'allowDuplicates',
    'allowOverflow',
    'autoFocus',
    'blurBehavior',
    'defaultInputValue',
    'defaultValue',
    'delimiter',
    'disabled',
    'editable',
    'form',
    'id',
    'ids',
    'inputValue',
    'invalid',
    'max',
    'maxLength',
    'name',
    'onFocusOutside',
    'onHighlightChange',
    'onInputValueChange',
    'onInteractOutside',
    'onPointerDownOutside',
    'onValueChange',
    'onValueInvalid',
    'placeholder',
    'readOnly',
    'required',
    'translations',
    'validate',
    'value',
    'sanitizeValue',
  ])

  const api = useTagsInput(useTagsInputProps)
  const mergedProps = mergeProps(() => api().getRootProps(), localProps)

  return (
    <TagsInputProvider value={api}>
      <ark.div {...mergedProps} />
    </TagsInputProvider>
  )
}
