import { Flex, Input } from 'antd'
import SelectFieldButtonModal from '@/components/data/SelectFieldButtonModal'

export default function FunctionInput({ fieldType, ...props }: any) {
  const handleFieldSelect = (fieldName: string) => {
    const value = (props.value || '') as string
    let updatedValue = ''

    if (value.endsWith('()')) {
      updatedValue = value.replace('()', `(${fieldName})`)
    } else {
      updatedValue = value.includes('(') ? value.replace(')', `, ${fieldName})`) : `${value}, ${fieldName}`
    }

    // 调用 props.onChange 通知父组件更新 value
    props.onChange?.(updatedValue)
  }

  return (
    <Flex gap={10} align="center">
      <Input.TextArea rows={6} {...props} />
      <SelectFieldButtonModal onChange={handleFieldSelect} fieldType={fieldType}></SelectFieldButtonModal>
    </Flex>
  )
}
