import moment, { DurationInputArg1, Moment, unitOfTime } from 'moment'

/**
 * 获取两个时间范围内的所有时间
 * @param t1 时间1
 * @param t2 时间2
 * @param format 时间格式化类型
 * @param unit 时间单位
 * @param amount 时间区间间隔数
 * @returns
 */
export function getRangeTime(t1: string | Moment | Date, t2: string | Moment | Date, format = 'YYYYMMDD', unit: unitOfTime.DurationConstructor = 'd', amount: DurationInputArg1 = 1): string[] {
    const fn = (val: Moment) => val.format(format);
    // 处理开始时间和结束时间
    let startTime = moment(t1);
    let endTime = moment(t2);

    const result: string[] = [];
    const isSame = () => startTime.isSame(endTime, unit);

    if (isSame()) {
        return [fn(startTime), fn(startTime)];
    } else {
        if (startTime.isAfter(endTime)) {
            [startTime, endTime] = [endTime, startTime]
        }
        while (!isSame()) {
            result.push(fn(startTime));
            startTime = startTime.add(amount, unit);
        }
        result.push(fn(endTime));
        return result;
    }
}