import { DatePicker } from 'antd'
import type { DatePickerProps } from 'antd'
import type { RangePickerProps } from 'antd/es/date-picker'

import {
  ADMIN_DATE_FORMAT,
  ADMIN_DATE_RANGE_PLACEHOLDER,
  ADMIN_DATE_TIME_FORMAT,
  ADMIN_DATE_TIME_RANGE_PLACEHOLDER,
} from '@/lib/dayjs'

type AdminDatePickerProps = DatePickerProps

type AdminRangePickerProps = RangePickerProps

/**
 * 后台统一日期选择：中文 locale（依赖全局 dayjs zh-cn + ConfigProvider zh_CN）。
 */
export function AdminDatePicker({
  format,
  placeholder = '请选择日期',
  ...rest
}: AdminDatePickerProps) {
  const resolvedFormat = format ?? (rest.showTime ? ADMIN_DATE_TIME_FORMAT : ADMIN_DATE_FORMAT)

  return <DatePicker format={resolvedFormat} placeholder={placeholder} {...rest} />
}

/**
 * 后台统一日期范围：默认 YYYY-MM-DD；showTime 时 YYYY-MM-DD HH:mm:ss。
 */
export function AdminRangePicker({ format, placeholder, ...rest }: AdminRangePickerProps) {
  const withTime = Boolean(rest.showTime)
  const resolvedFormat = format ?? (withTime ? ADMIN_DATE_TIME_FORMAT : ADMIN_DATE_FORMAT)
  const resolvedPlaceholder =
    placeholder ?? (withTime ? ADMIN_DATE_TIME_RANGE_PLACEHOLDER : ADMIN_DATE_RANGE_PLACEHOLDER)

  return (
    <DatePicker.RangePicker format={resolvedFormat} placeholder={resolvedPlaceholder} {...rest} />
  )
}

export default AdminDatePicker
