import * as React from 'react'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import Slider from '@/app/components/base/slider'
import Tooltip from '@/app/components/base/tooltip'
import {
  NumberField,
  NumberFieldControls,
  NumberFieldDecrement,
  NumberFieldGroup,
  NumberFieldIncrement,
  NumberFieldInput,
} from '@/app/components/base/ui/number-field'

const MIN_KEYWORD_NUMBER = 0
const MAX_KEYWORD_NUMBER = 50

type KeyWordNumberProps = {
  keywordNumber: number
  onKeywordNumberChange: (value: number) => void
}

const KeyWordNumber = ({
  keywordNumber,
  onKeywordNumberChange,
}: KeyWordNumberProps) => {
  const { t } = useTranslation()

  const handleInputChange = useCallback((value: number | null) => {
    onKeywordNumberChange(value ?? MIN_KEYWORD_NUMBER)
  }, [onKeywordNumberChange])

  return (
    <div className="flex items-center gap-x-1">
      <div className="flex grow items-center gap-x-0.5">
        <div className="truncate text-text-secondary system-xs-medium">
          {t('form.numberOfKeywords', { ns: 'datasetSettings' })}
        </div>
        <Tooltip
          popupContent={t('form.numberOfKeywords', { ns: 'datasetSettings' })}
        >
          <span className="i-ri-question-line h-3.5 w-3.5 text-text-quaternary" />
        </Tooltip>
      </div>
      <Slider
        className="mr-3 w-[206px] shrink-0"
        value={keywordNumber}
        min={MIN_KEYWORD_NUMBER}
        max={MAX_KEYWORD_NUMBER}
        onChange={onKeywordNumberChange}
      />
      <NumberField
        className="w-12 shrink-0"
        min={MIN_KEYWORD_NUMBER}
        max={MAX_KEYWORD_NUMBER}
        value={keywordNumber}
        onValueChange={handleInputChange}
      >
        <NumberFieldGroup size="regular">
          <NumberFieldInput size="regular" />
          <NumberFieldControls>
            <NumberFieldIncrement size="regular" />
            <NumberFieldDecrement size="regular" />
          </NumberFieldControls>
        </NumberFieldGroup>
      </NumberField>
    </div>
  )
}

export default React.memo(KeyWordNumber)
