function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import dayjs from 'dayjs';

/**
 * 根据日程配置生成日历数据
 * @param values
 * @param others
 * @param type
 */
export var calcCalendarDataBySchedule = function calcCalendarDataBySchedule(values, others, type) {
  if (type === 'time-slots') {
    return calcCalendarDataByTimeSlots(values, others);
  }
  if (type === 'designation') {
    return calcCalendarDataByDesignation(values, others);
  }
  return calcCalendarDataByStandard(values, others);
};

/**
 * 获取开始时间和结束时间之间的所有日期
 * @param startDate
 * @param endDate
 */
export var getDaysByStartEnd = function getDaysByStartEnd(startDate, endDate) {
  var start = dayjs(startDate);
  var end = dayjs(endDate);
  var daysArray = [];
  while (start.isBefore(end, 'day') || start.isSame(end, 'day')) {
    daysArray.push(start.format('YYYY-MM-DD'));
    start = start.add(1, 'day');
  }
  return daysArray;
};

/**
 * 合并日期和时间
 * @param date
 * @param time
 */
var mergeDateTime = function mergeDateTime(date, time) {
  var dateStr = "".concat(dayjs(date).format('YYYY-MM-DD')).concat(time ? dayjs(time).format(' HH:MM') : '');
  return dayjs(dateStr);
};
var getDataRange = function getDataRange(_ref) {
  var start = _ref.start,
    end = _ref.end,
    type = _ref.type,
    time_slot = _ref.time_slot;
  if (type === 'time-slots') {
    var dateRange = [];
    var dateStr = getDaysByStartEnd(start, end);
    dateStr.forEach(function (str) {
      time_slot === null || time_slot === void 0 || time_slot.forEach(function (time) {
        dateRange.push({
          start: "".concat(str, " ").concat(time.start_time),
          end: "".concat(str, " ").concat(time.end_time)
        });
      });
    });
    return dateRange;
  }
  return [{
    start: start.format('YYYY-MM-DD HH:mm:ss'),
    end: end.format('YYYY-MM-DD HH:mm:ss')
  }];
};

/**
 * 按天重复配置获取日期
 * @param params
 */
var getDaysByRepeatDay = function getDaysByRepeatDay(params, isGetRange) {
  var start = params.start,
    end = params.end,
    deadline = params.deadline,
    frequency = params.frequency,
    type = params.type,
    time_slot = params.time_slot,
    excludedDays = params.excludedDays;
  var scheduleDiff = end.diff(start, 'second');
  var excludedDaysMap = new Map(excludedDays.map(function (day) {
    return [day, true];
  }));
  var startTmp = getMaxStart(start);
  var daysArr = [];
  while (startTmp.isBefore(deadline, 'day') || startTmp.isSame(deadline, 'day')) {
    var e = startTmp.add(scheduleDiff, 'second');
    if (isGetRange) {
      if (excludedDaysMap.has(startTmp.format('YYYY-MM-DD'))) {
        startTmp = startTmp.add(frequency, 'day');
        continue;
      }
      var arr = getDataRange({
        start: startTmp,
        end: e,
        type: type,
        time_slot: time_slot
      });
      daysArr.push.apply(daysArr, _toConsumableArray(arr));
    } else {
      daysArr.push.apply(daysArr, _toConsumableArray(getDaysByStartEnd(startTmp, e)));
    }
    startTmp = startTmp.add(frequency, 'day');
  }
  return Array.from(new Set(daysArr));
};

/**
 * 按周重复配置获取日期
 * @param params
 */
var getDaysByRepeatWeek = function getDaysByRepeatWeek(params, isGetRange) {
  var start = params.start,
    end = params.end,
    deadline = params.deadline,
    frequency = params.frequency,
    frequency_date = params.frequency_date,
    type = params.type,
    time_slot = params.time_slot,
    excludedDays = params.excludedDays;
  var scheduleDiff = end.diff(start, 'second');
  var excludedDaysMap = new Map(excludedDays.map(function (day) {
    return [day, true];
  }));
  var startTmp = getMaxStart(start);
  var daysArr = [];
  while (startTmp.isBefore(deadline, 'day') || startTmp.isSame(deadline, 'day')) {
    for (var i = 0; i < frequency_date.length; i++) {
      var item = frequency_date[i];
      var _start = startTmp.day(item);

      // 如果计算出的日期早于startTmp，则跳到下一周的相同星期几
      if (_start.isBefore(startTmp, 'day')) {
        _start = _start.add(1, 'week');
      }

      // 如果周结束的那一天还超过了deadline，则需要把_end置为deadline
      var _end = _start.add(scheduleDiff, 'second');
      if (_end.isAfter(deadline, 'day')) {
        _end = deadline;
      }
      if (isGetRange) {
        if (excludedDaysMap.has(_start.format('YYYY-MM-DD'))) {
          startTmp = startTmp.day(1).add(frequency, 'day');
          continue;
        }
        var arr = getDataRange({
          start: _start,
          end: _end,
          type: type,
          time_slot: time_slot
        });
        daysArr.push.apply(daysArr, _toConsumableArray(arr));
      } else {
        daysArr.push.apply(daysArr, _toConsumableArray(getDaysByStartEnd(_start, _end)));
      }
    }
    startTmp = startTmp.add(frequency, 'week');
  }
  return Array.from(new Set(daysArr));
};

/**
 * 日期和排除日期格式化日历所需数据格式
 * @param days
 * @param color
 * @param excludedDays
 */
var formatDayColor = function formatDayColor(_ref2) {
  var days = _ref2.days,
    color = _ref2.color,
    excludedDays = _ref2.excludedDays;
  var excludedDaysSet = new Set(excludedDays);
  return days.map(function (item) {
    return {
      date: item,
      color: [color],
      isExcluded: excludedDaysSet.has(item)
    };
  });
};

/**
 * 获取开始结束时间数组的所有日期
 * @param arr
 */
var getDaysByStartEndArr = function getDaysByStartEndArr() {
  var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var daysArr = [];
  arr.forEach(function (item) {
    if (item && item.start && item.end) {
      daysArr.push.apply(daysArr, _toConsumableArray(getDaysByStartEnd(item.start, item.end)));
    }
  });
  return Array.from(new Set(daysArr));
};

/**
 * 过滤排除日期
 * @param daysArr
 * @param excludedArr
 */
var filterExcludedDays = function filterExcludedDays(daysArr, excludedArr) {
  var excludeSet = new Set(excludedArr);
  if (!excludedArr.length) return daysArr;
  return daysArr.filter(function (item) {
    return !excludeSet.has(item);
  });
};

/**
 * 处理日期重复逻辑
 * @param repeat_type
 * @param start
 * @param end
 * @param color
 * @param endRadio
 * @param end_date
 * @param excluded_date
 * @param included_date
 * @param frequency
 * @param excludedStatus
 * @param includeStatus
 * @param frequency_date
 */
var processDateRange = function processDateRange(_ref3, isGetRange) {
  var repeat_type = _ref3.repeat_type,
    start = _ref3.start,
    end = _ref3.end,
    color = _ref3.color,
    endRadio = _ref3.endRadio,
    end_date = _ref3.end_date,
    excluded_date = _ref3.excluded_date,
    included_date = _ref3.included_date,
    frequency = _ref3.frequency,
    excludedStatus = _ref3.excludedStatus,
    includeStatus = _ref3.includeStatus,
    frequency_date = _ref3.frequency_date,
    type = _ref3.type,
    time_slot = _ref3.time_slot;
  /** 时间范围或天 */
  var days = [];
  var excludedDays = getDaysByStartEndArr(excluded_date);
  var includedDays = getDaysByStartEndArr(included_date);

  // 不重复
  if (repeat_type === '0') {
    if (isGetRange) {
      if (type === 'time-slots') {
        days = (time_slot === null || time_slot === void 0 ? void 0 : time_slot.map(function (item) {
          return {
            start: start.format("YYYY-MM-DD ".concat(item.start_time)),
            end: end.format("YYYY-MM-DD ".concat(item.end_time))
          };
        })) || [];
      } else {
        days = [{
          start: start.format('YYYY-MM-DD HH:mm:ss'),
          end: end.format('YYYY-MM-DD HH:mm:ss')
        }];
      }
    } else {
      days = getDaysByStartEnd(start, end);
    }
  } else {
    var endDate = dayjs(end_date);

    // 截止时间 如果永不截止取18个月截止
    if (endRadio === 1) {
      endDate = isGetRange ? dayjs().add(12, 'month') : dayjs().add(18, 'month');
    }

    // 按天重复
    if (repeat_type === '1') {
      days = getDaysByRepeatDay({
        start: start,
        end: end,
        deadline: endDate,
        frequency: frequency,
        type: type,
        time_slot: time_slot,
        excludedDays: excludedDays
      }, isGetRange);
      // 按周重复
    } else if (repeat_type === '2') {
      days = getDaysByRepeatWeek({
        start: start,
        end: end,
        deadline: endDate,
        frequency: frequency,
        frequency_date: frequency_date,
        type: type,
        time_slot: time_slot,
        excludedDays: excludedDays
      }, isGetRange);
    }
  }
  if (isGetRange) {
    // 如果有包含的天再把包含的天的时间段插入
    if (includedDays.length) {
      for (var i = 0; i < includedDays.length; i++) {
        var _days;
        var includedDay = includedDays[i];
        (_days = days).push.apply(_days, _toConsumableArray(getDataRange({
          start: dayjs(includedDay),
          end: dayjs(includedDay),
          type: type,
          time_slot: time_slot
        })));
      }
    }
    return days;
  }

  // 开启了排除日期
  // if (excludedStatus) {
  //   days = filterExcludedDays(days, excludedDays);
  // }

  // 开启了包含日期
  if (includeStatus) {
    days = days.concat(includedDays);
  }
  return formatDayColor({
    days: days,
    color: color,
    excludedDays: excludedStatus ? excludedDays : []
  });
};

/**
 * 根据标准日程计算日历数据
 * @param values
 * @param others
 */
export var calcCalendarDataByStandard = function calcCalendarDataByStandard(values, others, isGetRange) {
  var color = values.color,
    excluded_date = values.excluded_date,
    included_date = values.included_date,
    end_date = values.end_date,
    designation = values.designation,
    repeat_type = values.repeat_type,
    _values$frequency = values.frequency,
    frequency = _values$frequency === void 0 ? 1 : _values$frequency,
    _values$frequency_dat = values.frequency_date,
    frequency_date = _values$frequency_dat === void 0 ? [] : _values$frequency_dat,
    time_slot = values.time_slot,
    start_time = values.start_time,
    end_time = values.end_time;
  var endRadio = others.endRadio,
    isAllDay = others.isAllDay,
    includeStatus = others.includeStatus,
    excludedStatus = others.excludedStatus;
  if (Array.isArray(designation)) {
    return [];
  }

  // 日程开始时间
  var start = designation ? mergeDateTime(designation.start_date, isAllDay ? designation.start_time : '') : dayjs(start_time);

  // 日程结束时间
  var end = designation ? mergeDateTime(designation.end_date, isAllDay ? designation.end_time : '') : dayjs(end_time);

  // // 不重复
  // if (repeat_type === "0") {
  //   return formatDayColor(getDaysByStartEnd(start, end), color);
  // }
  // // 截止时间 如果永不截止取15个月截止
  // const endDate = endRadio === 2 ? dayjs(end_date) : dayjs().add(15, "month");
  //
  // const excludedDays = getDaysByStartEndArr(excluded_date);
  //
  // const includedDays = getDaysByStartEndArr(included_date);
  //
  // // 按天重复
  // if (repeat_type === "1") {
  //   let days = getDaysByRepeatDay({
  //     start,
  //     end,
  //     deadline: endDate,
  //     frequency,
  //   });
  //
  //   // 开启了排除日期
  //   if (excludedStatus) {
  //     days = filterExcludedDays(days, excludedDays);
  //   }
  //
  //   // 开启了包含日期
  //   if (includeStatus) {
  //     days = days.concat(includedDays);
  //   }
  //
  //   return formatDayColor(days, color);
  // }
  //
  // // 按周重复
  // if (repeat_type === "2") {
  //   let days = getDaysByRepeatWeek({
  //     start,
  //     end,
  //     deadline: endDate,
  //     frequency,
  //     frequency_date,
  //   });
  // }
  //
  // return [];

  return processDateRange({
    repeat_type: repeat_type,
    start: start,
    end: end,
    color: color,
    endRadio: endRadio,
    end_date: end_date,
    excluded_date: excluded_date,
    excludedStatus: excludedStatus,
    frequency: frequency,
    frequency_date: frequency_date,
    included_date: included_date,
    includeStatus: includeStatus,
    time_slot: time_slot,
    type: 'standard'
  }, isGetRange);
};
var getMaxStart = function getMaxStart(start_time) {
  var valueStart = dayjs(start_time);
  var currentYearStart = dayjs().startOf('year').set('hour', valueStart.get('hour')).set('minute', valueStart.get('minutes')).set('second', valueStart.get('second'));
  var start = valueStart.isBefore(currentYearStart) ? currentYearStart : valueStart;
  return start;
};

/**
 * 根据多时间段计算日历数据
 * @param values
 * @param others
 */
export var calcCalendarDataByTimeSlots = function calcCalendarDataByTimeSlots(values, others, isGetRange) {
  var color = values.color,
    excluded_date = values.excluded_date,
    included_date = values.included_date,
    end_date = values.end_date,
    repeat_type = values.repeat_type,
    _values$frequency2 = values.frequency,
    frequency = _values$frequency2 === void 0 ? 1 : _values$frequency2,
    _values$frequency_dat2 = values.frequency_date,
    frequency_date = _values$frequency_dat2 === void 0 ? [] : _values$frequency_dat2,
    start_time = values.start_time,
    time_slot = values.time_slot;
  var endRadio = others.endRadio,
    excludedStatus = others.excludedStatus,
    includeStatus = others.includeStatus;
  if (!start_time) return [];
  var start = dayjs(start_time);
  var end = dayjs(start_time);
  return processDateRange({
    repeat_type: repeat_type,
    start: start,
    end: end,
    color: color,
    endRadio: endRadio,
    end_date: end_date,
    excluded_date: excluded_date,
    excludedStatus: excludedStatus,
    frequency: frequency,
    frequency_date: frequency_date,
    included_date: included_date,
    includeStatus: includeStatus,
    time_slot: time_slot,
    type: 'time-slots'
  }, isGetRange);
};

/**
 * 根据指定日期计算日历数据
 * @param values
 * @param others
 */
export var calcCalendarDataByDesignation = function calcCalendarDataByDesignation(values, others, isGetRange) {
  var designation = values.designation,
    color = values.color;
  var days = [];
  if (Array.isArray(designation)) {
    designation.forEach(function (item) {
      // 日程开始时间
      var start = mergeDateTime(item.start_date, item.start_time);

      // 日程结束时间
      var end = mergeDateTime(item.end_date, item.end_time);
      if (isGetRange) {
        days.push({
          start: "".concat(item.start_date, " ").concat(item.start_time),
          end: "".concat(item.end_date, " ").concat(item.end_time)
        });
      } else {
        days.push.apply(days, _toConsumableArray(getDaysByStartEnd(start, end)));
      }
    });
  }
  if (isGetRange) {
    return days;
  }
  return formatDayColor({
    days: days,
    color: color,
    excludedDays: []
  });
};
export var isAllDay = function isAllDay(value) {
  return dayjs(value.start_time).format('HH:mm:ss') === '00:00:00' && dayjs(value.end_time).format('HH:mm:ss') === '23:59:59';
};
var repeatTypeMap = {
  none: '0',
  daily: '1',
  weekly: '2'
};
var endTypeMap = {
  never: 1,
  date: 2
};

/**
 * 根据接口返回的Schedule计算日历数据
 * @param schedule
 */
export var calcCalendarDataByScheduleResult = function calcCalendarDataByScheduleResult(schedule) {
  var _formatScheduleResult = formatScheduleResult(schedule),
    scheduleFormData = _formatScheduleResult.scheduleFormData,
    scheduleFormOtherValue = _formatScheduleResult.scheduleFormOtherValue;
  return calcCalendarDataBySchedule(scheduleFormData, scheduleFormOtherValue, schedule.type);
};

/**
 * 接口返回schedule转换格式
 * @param schedule
 */
export var formatScheduleResult = function formatScheduleResult(schedule) {
  var color = schedule.color,
    name = schedule.name,
    repeat_type = schedule.repeat_type,
    designation = schedule.designation,
    repeat_rule = schedule.repeat_rule,
    start_time = schedule.start_time,
    end_time = schedule.end_time,
    is_all = schedule.is_all,
    type = schedule.type,
    time_slot = schedule.time_slot;
  var _ref4 = repeat_rule || {},
    frequency = _ref4.frequency,
    end = _ref4.end,
    frequency_date = _ref4.frequency_date,
    included_date = _ref4.included_date,
    excluded_date = _ref4.excluded_date;
  return {
    scheduleFormData: {
      color: color,
      name: name,
      repeat_type: repeatTypeMap[repeat_type],
      designation: designation,
      frequency: frequency,
      frequency_date: frequency_date,
      end_time: end_time,
      start_time: start_time,
      excluded_date: excluded_date,
      included_date: included_date,
      time_slot: time_slot,
      end_date: end === null || end === void 0 ? void 0 : end.end_date
    },
    scheduleFormOtherValue: {
      endRadio: endTypeMap[(end === null || end === void 0 ? void 0 : end.type) || 'never'],
      isAllDay: !!is_all,
      type: type,
      excludedStatus: !!(excluded_date !== null && excluded_date !== void 0 && excluded_date.length),
      includeStatus: !!(included_date !== null && included_date !== void 0 && included_date.length)
    }
  };
};
export var calcScheduleDateRange = function calcScheduleDateRange(schedule) {
  var _formatScheduleResult2 = formatScheduleResult(schedule),
    scheduleFormData = _formatScheduleResult2.scheduleFormData,
    scheduleFormOtherValue = _formatScheduleResult2.scheduleFormOtherValue;
  if (schedule.type === 'time-slots') {
    return calcCalendarDataByTimeSlots(scheduleFormData, scheduleFormOtherValue, true);
  }
  if (schedule.type === 'designation') {
    return calcCalendarDataByDesignation(scheduleFormData, scheduleFormOtherValue, true);
  }
  return calcCalendarDataByStandard(scheduleFormData, scheduleFormOtherValue, true);
};
export var calcCalendarDataBySchedules = function calcCalendarDataBySchedules(schedules) {
  var allSchedules = [];
  schedules.forEach(function (item) {
    allSchedules.push.apply(allSchedules, _toConsumableArray(calcCalendarDataByScheduleResult(item)));
  });
  var dateMap = allSchedules.reduce(function (pre, cur) {
    var _pre$cur$date, _pre$cur$date2;
    return _objectSpread(_objectSpread({}, pre), {}, _defineProperty({}, cur.date, _objectSpread(_objectSpread({}, cur), {}, {
      color: [].concat(_toConsumableArray(((_pre$cur$date = pre[cur.date]) === null || _pre$cur$date === void 0 ? void 0 : _pre$cur$date.color) || []), _toConsumableArray(cur.color)),
      isExcluded: ((_pre$cur$date2 = pre[cur.date]) === null || _pre$cur$date2 === void 0 ? void 0 : _pre$cur$date2.isExcluded) || cur.isExcluded
    })));
  }, {});
  return Object.values(dateMap);
};
export var calcMinTimeMaxTimeBySchedules = function calcMinTimeMaxTimeBySchedules(schedules, scheduleAllMap, selectDate) {
  return schedules.reduce(function (pre, cur) {
    var _scheduleAllMap$cur$i, _minTime, _maxTime;
    var dateRange = (scheduleAllMap === null || scheduleAllMap === void 0 || (_scheduleAllMap$cur$i = scheduleAllMap[cur.id]) === null || _scheduleAllMap$cur$i === void 0 ? void 0 : _scheduleAllMap$cur$i.dateRange) || calcScheduleDateRange(cur);
    var dateRangeFormat = [];
    if (selectDate) {
      var dateRangeTmp = [];
      var dateRangeFormatTmp = [];
      for (var i = 0; i < dateRange.length; i++) {
        var range = dateRange[i];
        var start = dayjs(range.start);
        var end = dayjs(range.end);
        // includes 比 dayjs isSame方法会快一些
        var isSameStart = range.start.includes(selectDate);
        var isSameEnd = range.end.includes(selectDate);
        if (isSameStart || isSameEnd) {
          dateRangeTmp.push(range);
          if (isSameStart && !isSameEnd) {
            dateRangeFormatTmp.push({
              start: range.start,
              end: end.format('YYYY-MM-DD 23:59:59')
            });
          } else if (!isSameStart && isSameEnd) {
            dateRangeFormatTmp.push({
              start: start.format('YYYY-MM-DD 00:00:00'),
              end: range.end
            });
          } else {
            dateRangeFormatTmp.push(range);
          }
        }
      }
      dateRange = dateRangeTmp;
      dateRangeFormat = dateRangeFormatTmp;
    }
    var minTime = null;
    var maxTime = null;
    if (dateRange.length) {
      // ({ minTime, maxTime } = getMinTimeRange(dateRange));
      minTime = dayjs(dateRange[0].start);
      maxTime = dayjs(dateRange[dateRange.length - 1].end);
    }
    return _objectSpread(_objectSpread({}, pre), {}, _defineProperty({}, cur.id, {
      minTime: minTime,
      maxTime: maxTime,
      minTimeStr: ((_minTime = minTime) === null || _minTime === void 0 ? void 0 : _minTime.format('YYYY-MM-DD HH:mm:ss')) || null,
      maxTimeStr: ((_maxTime = maxTime) === null || _maxTime === void 0 ? void 0 : _maxTime.format('YYYY-MM-DD HH:mm:ss')) || null,
      dateRange: dateRange,
      dateRangeFormat: dateRangeFormat
    }));
  }, {});
};

/**
 * @title: 格式化日程数据
 * @description:
 * @param {Array} relation 资源列表
 * @param {string} table_type 关联表类型
 * @param {string} item_type 资源类型
 * @param {boolean} isExcluded 是否排除
 * @return {*}
 * @Author: WangHan
 * @Date: 2024-09-04 11:54
 */
export var formatScheduleData = function formatScheduleData(relation, table_type, item_type, isExcluded) {
  var _relation = relation || [];
  // 有关联表类型和资源类型并且排除
  if (table_type && item_type && isExcluded) {
    return _relation.find(function (item) {
      return (item === null || item === void 0 ? void 0 : item.relation_table_type) === table_type && (item === null || item === void 0 ? void 0 : item.item_type) !== item_type;
    });
  }

  // 有关联表类型和资源类型
  if (table_type && item_type) {
    return _relation.find(function (item) {
      return (item === null || item === void 0 ? void 0 : item.relation_table_type) === table_type && (item === null || item === void 0 ? void 0 : item.item_type) === item_type;
    });
  }

  // 有关联表类型
  if (table_type) {
    return _relation.some(function (item) {
      return (item === null || item === void 0 ? void 0 : item.relation_table_type) === table_type;
    });
  }

  // 有关联资源类型
  if (item_type) {
    return _relation.find(function (item) {
      return (item === null || item === void 0 ? void 0 : item.item_type) === item_type;
    });
  }
};
export function getAllSortedDateRanges(schedules) {
  // 收集所有 dateRange
  var allDateRanges = [];

  // 遍历所有对象，收集 dateRange
  Object.values(schedules).forEach(function (schedule) {
    if (schedule.dateRange && Array.isArray(schedule.dateRange)) {
      allDateRanges.push.apply(allDateRanges, _toConsumableArray(schedule.dateRange));
    }
  });

  // 如果没有找到任何 dateRange，返回空数组
  if (allDateRanges.length === 0) {
    console.log('No dateRanges found in schedules:', schedules);
    return [];
  }

  // 按照开始时间和时长排序
  var sortedDateRanges = allDateRanges.sort(function (a, b) {
    // 首先按开始时间排序
    var startTimeCompare = dayjs(a.start).valueOf() - dayjs(b.start).valueOf();
    if (startTimeCompare !== 0) return startTimeCompare;

    // 如果开始时间相同，按时长排序（结束时间 - 开始时间）
    var durationA = dayjs(a.end).valueOf() - dayjs(a.start).valueOf();
    var durationB = dayjs(b.end).valueOf() - dayjs(b.start).valueOf();
    return durationA - durationB;
  });

  // 排序完了，还需要做个去重，如果有开始时间结束时间完全一致的，则只保留一个
  // 需要使用 dayjs 的 isSame 方法来判断
  var uniqueDateRanges = sortedDateRanges.filter(function (range, index, self) {
    return index === self.findIndex(function (t) {
      return dayjs(t.start).isSame(dayjs(range.start)) && dayjs(t.end).isSame(dayjs(range.end));
    });
  });
  return uniqueDateRanges;
}