import type {RangeValue} from '@components';
import type {Dayjs} from 'dayjs';
import {useState} from 'react';

export function useRangeDate(
  startTime: Dayjs | null = null,
  endTime: Dayjs | null = null,
  options?: {
    enableTime?: boolean;
  },
) {
  const [dates, setDates] = useState<RangeValue<Dayjs>>([startTime, endTime]);

  const format = options?.enableTime ? 'YYYY-MM-DD HH:mm' : 'YYYY-MM-DD';

  const start = dates?.[0]?.format(format) ?? '',
    end = dates?.[1]?.format(format) ?? '';

  return [{dates, start, end}, setDates] as const;
}
