import Dayjs from 'dayjs';

/**
 * @title: 获取包括今天的后七天日期
 * @description:
 * @param {*}
 * @return {*}
 * @Author: 严文强
 * @Date: 2024-02-01 11:00
 */
export var getNextSevenDaysList = function getNextSevenDaysList(defaultDay) {
  // 获取当前日期
  var currentDate = Dayjs(defaultDay);
  // 创建一个数组来存储最近7天的日期
  var nextSevenDays = [];
  // 循环遍历最近7天的日期
  for (var i = 0; i < 7; i++) {
    // 获取当前日期的前7天
    var previousDay = currentDate.add(i, 'day');
    // 将日期添加到数组中
    nextSevenDays.push(previousDay.format('YYYY-MM-DD'));
  }
  return nextSevenDays;
};

/**
 * @title: 将年月日信息只展示为天
 * @description: 如果为今天 则返回ToDay
 * @param {*}
 * @return {*}
 * @Author: 严文强
 * @Date: 2024-02-01 11:00
 */
export var ShowDayOnly = function ShowDayOnly(date) {
  //
  var defaultDay = Dayjs().format('YYYY-MM-DD');
  if (date == defaultDay) {
    return 'ToDay';
  }
  return Dayjs(date).format('DD');
};

/**
 * @title: 日历星期多语言
 * @description:
 * @param {*}
 * @return {*}
 * @Author: 严文强
 * @Date: 2024-02-01 18:42
 */
export var weekListText = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];

/**
 * @title: 不同状态颜色值
 * @description:
 * @param {*}
 * @return {*}
 * @Author: xiangxiang.shao
 * @Date: 2024-02-19 11:35
 */
export var statusColor = {
  call_to_book: '#444CE7',
  // 请致电预订
  lots_of_space: '#079455',
  // 库存充足
  filling_up_fast: '#CA8504',
  // 库存紧张
  sold_out: '#D92D20' // 无库存
};

/**
 * @title: 根据预约量数据判断当前状态
 * @description:
 * @param {*}
 * @return {*}
 * @Author: xiangxiang.shao
 * @Date: 2024-02-19 11:35
 */
export var getStatus = function getStatus(dateLimit, currentDate, cutTime) {
  var status = '';
  var disabled = false;
  var dataItem = dateLimit[currentDate];
  var startTime = Dayjs(cutTime === null || cutTime === void 0 ? void 0 : cutTime.shop_time).format('YYYY-MM-DD HH:mm');
  var startDate = Dayjs(cutTime === null || cutTime === void 0 ? void 0 : cutTime.shop_time).format('YYYY-MM-DD');
  var endTime = Dayjs(startTime).add(cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit, cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type);
  var endDay = endTime.format('YYYY-MM-DD');
  try {
    // 没有当天数据, 禁止
    if (!dataItem) {
      return {
        status: status,
        disabled: true
      };
    }
  } catch (err) {
    return {
      status: status,
      disabled: true
    };
  }
  if (!Object.keys(dateLimit).length) {
    return {
      status: status,
      disabled: true
    };
  }
  var times = dataItem.times || [];
  if (!times.length) {
    disabled = true;
  } else {
    var useable = null;
    if (cutTime.type === 'ongoing' && currentDate === startDate) {
      var _cutTime$ongoing, _cutTime$ongoing2;
      // 活动结束前都可以购买
      if ((cutTime === null || cutTime === void 0 || (_cutTime$ongoing = cutTime['ongoing']) === null || _cutTime$ongoing === void 0 ? void 0 : _cutTime$ongoing.type) === 'before_end') {
        // 在活动时间范围内 或者 在活动还没开始前可选
        useable = times.find(function (d) {
          return startTime >= d.start_at && startTime < d.end_at || d.start_at >= startTime;
        });
      }
      if ((cutTime === null || cutTime === void 0 || (_cutTime$ongoing2 = cutTime['ongoing']) === null || _cutTime$ongoing2 === void 0 ? void 0 : _cutTime$ongoing2.type) === 'after_start') {
        // 在活动时间范围内 或者 在活动还没开始前可选
        useable = times.find(function (d) {
          var _cutTime$ongoing3;
          var _endAt = Dayjs(d.start_at).add(cutTime === null || cutTime === void 0 || (_cutTime$ongoing3 = cutTime['ongoing']) === null || _cutTime$ongoing3 === void 0 ? void 0 : _cutTime$ongoing3.unit, 'minutes');
          var _endTime = _endAt.format('YYYY-MM-DD HH:mm');
          return startTime >= d.start_at && startTime <= _endTime || d.start_at >= startTime;
        });
      }
    } else {
      useable = times.find(function (d) {
        return startTime < d.start_at;
      });
    }
    if (!useable) {
      return {
        status: status,
        disabled: true
      };
    }

    // 是否含有无限制数据
    var haveUnLimit = times.find(function (item) {
      return item.limit === 0;
    });
    if (haveUnLimit) {
      status = 'lots_of_space';
    } else {
      var allLimit = times.reduce(function (prev, next) {
        return prev + next.limit;
      }, 0);
      var allOrderCount = times.reduce(function (prev, next) {
        return prev + next.order_count;
      }, 0);
      var percent = (allLimit - allOrderCount) / allLimit;
      if (percent > 0.5) {
        // 库存充足
        status = 'lots_of_space';
      } else if (percent <= 0.5 && percent > 0) {
        // 库存紧张
        status = 'filling_up_fast';
      } else if (percent <= 0) {
        // 无库存
        status = 'sold_out';
      }
    }
  }
  return {
    status: status,
    disabled: disabled
  };
};

/**
 * @title: 根据当前日期以设定判断当前是否是call_to_book状态
 * @description:
 * @param {*}
 * @return {*}
 * @Author: xiangxiang.shao
 * @Date: 2024-02-21 10:10
 */
export var getCallToBookStatus = function getCallToBookStatus(currentDate, dateLimitData, cutTime) {
  var _dateLimitData$curren;
  var timeList = ((_dateLimitData$curren = dateLimitData[currentDate]) === null || _dateLimitData$curren === void 0 ? void 0 : _dateLimitData$curren.times) || [];
  var isShowCallToBook = false;
  if (!currentDate || !(cutTime !== null && cutTime !== void 0 && cutTime.shop_time) || !(cutTime !== null && cutTime !== void 0 && cutTime.unit) || !(cutTime !== null && cutTime !== void 0 && cutTime.unit_type)) {
    isShowCallToBook = false;
    return isShowCallToBook;
  }
  if ((cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type) === 'hours' || (cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type) === 'minutes' || (cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type) === 'days') {
    var startTime = Dayjs(cutTime.shop_time).format('YYYY-MM-DD HH:mm:ss');
    var endTime = Dayjs(startTime).add(cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit, cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type).format('YYYY-MM-DD HH:mm:ss');
    if (Dayjs(currentDate).isBefore(Dayjs(endTime.split(' ')[0]))) {
      isShowCallToBook = true;
    } else {
      // 如果日期相等，则判断timeList是否全在时间范围内，如果全在时间范围内，则返回true
      if (currentDate === endTime.split(' ')[0]) {
        var _timeList$find;
        var haveTime = timeList === null || timeList === void 0 || (_timeList$find = timeList.find) === null || _timeList$find === void 0 ? void 0 : _timeList$find.call(timeList, function (item) {
          var end_at = "".concat(item.date, " ").concat(item.end_at);
          // 当前时间在开始时间和结束时间之间 end_at < endTime
          if (Dayjs(end_at).valueOf() > Dayjs(endTime).valueOf()) {
            return true;
          }
        });
        if (haveTime) {
          isShowCallToBook = false;
        } else {
          isShowCallToBook = true;
        }
      } else {
        isShowCallToBook = false;
      }
    }
    // 判断开始时间和结束时间是否大于1天
  }
  return isShowCallToBook;
};
export var localeTypes = {
  'en-US': 'en',
  'zh-CN': 'zh-cn',
  'zh-TW': 'zh-tw'
};