var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

import { DateUnitKeys, DateUntilKeys, DynamincType, FastTypeKeys, showNature } from "./constant";
import dayjs from 'dayjs';
import { FilterTypeKeys } from "./constant";
import { TimeFormat } from "./..";
export var getDurationOfFastType = function getDurationOfFastType(fast, val, type, unit, fastMap, until, include, nature) {
  if (val === void 0) {
    val = 0;
  }

  var isoUnit = unit === DateUnitKeys.week ? 'isoWeek' : unit;
  if (!fast) return [null, null];

  if (fast === FastTypeKeys.customer) {
    var currentDay = dayjs();
    var checkNature = showNature(unit);
    var _a = [currentDay, currentDay],
        start = _a[0],
        end = _a[1]; // 有自然且不包含 ||  没有自然且不包含

    var noUnitCal = checkNature && nature && !include || !checkNature && !nature && !include;

    if (type === DynamincType.after && noUnitCal) {
      start = start.add(1, unit).startOf(isoUnit);
      end = start.add(val, unit);
    }

    if (type === DynamincType.before && noUnitCal) {
      end = end.startOf(isoUnit);
      start = end.subtract(val, unit);
    }

    return customerGetDatesfromUntil([start, end], until, unit, val, type, include, nature, checkNature);
  }

  return fastMap[fast];
};

var customerGetDatesfromUntil = function customerGetDatesfromUntil(dates, until, unit, val, type, include, nature, checkNature) {
  var isNature = checkNature && nature;

  if (isNature && !include || !checkNature && !include) {
    return dates;
  }

  var start = dates[0],
      end = dates[1];
  var isoUnit = unit === DateUnitKeys.week ? 'isoWeek' : unit;

  switch (until) {
    case DateUntilKeys.yearEnd:
      if (nature && include) {
        end = end.add(1, DateUnitKeys.years).startOf(DateUnitKeys.years);
        start = end.subtract(val, DateUnitKeys.years);
      }

      break;

    case DateUntilKeys.yearStart:
      if (nature && include) {
        start = start.startOf(DateUnitKeys.years);
        end = start.add(val, DateUnitKeys.years);
      }

      break;

    case DateUntilKeys.quarterEnd:
      if (nature && include) {
        end = end.add(1, DateUnitKeys.quarter).startOf(DateUnitKeys.quarter);
        start = end.subtract(val, DateUnitKeys.quarter);
      }

      break;

    case DateUntilKeys.quarterStart:
      if (nature && include) {
        start = start.startOf(DateUnitKeys.quarter);
        end = start.add(val, DateUnitKeys.quarter);
      }

      break;

    case DateUntilKeys.monthEnd:
      if (nature && include) {
        end = end.add(1, DateUnitKeys.month).startOf(DateUnitKeys.month);
        start = end.subtract(val, DateUnitKeys.month);
      }

      break;

    case DateUntilKeys.monthStart:
      if (nature && include) {
        start = start.startOf(DateUnitKeys.month);
        end = start.add(val, DateUnitKeys.month);
      }

      break;

    case DateUntilKeys.weekend:
      if (nature && include) {
        end = end.add(1, DateUnitKeys.week).startOf(isoUnit);
        start = end.subtract(val, DateUnitKeys.week);
      }

      break;

    case DateUntilKeys.weekStart:
      if (nature && include) {
        start = start.startOf(isoUnit);
        end = start.add(val, DateUnitKeys.week);
      }

      break;

    case DateUntilKeys.today:
      if (type === DynamincType.after) {
        start = dayjs().startOf(DateUnitKeys.day);

        if (isNature) {
          end = end.add(val, unit).startOf(isoUnit);
        } else {
          end = start.add(val, unit); // 关于周的特殊处理

          if (unit === DateUnitKeys.week && val) {
            end = end.add(1, DateUnitKeys.day);
          }
        }
      } else {
        end = end.add(1, DateUnitKeys.day).startOf(DateUnitKeys.day);
        start = start.add(1, unit);

        if (isNature) {
          start = start.subtract(val, unit).startOf(isoUnit);
        } else {
          start = end.subtract(val, unit); // 关于周的特殊处理

          if (unit === DateUnitKeys.week && val) {
            start = start.subtract(1, DateUnitKeys.day);
          }
        }
      }

      break;

    case DateUntilKeys.now:
      if (type === DynamincType.after) {
        start = dayjs();
        end = unit === DateUnitKeys.day ? start.add(val, unit).startOf(isoUnit) : start.add(val, unit);
      } else {
        end = dayjs();
        start = unit === DateUnitKeys.day ? end.subtract(val, unit).add(val ? 1 : 0, unit).startOf(isoUnit) : end.subtract(val, unit);
      }

      break;

    case DateUntilKeys.tomorrow:
      start = dayjs().add(1, DateUnitKeys.day).startOf(DateUnitKeys.day);

      if (isNature) {
        end = end.add(val, unit).startOf(isoUnit);
      } else {
        end = start.add(val, unit); // 关于周的特殊处理

        if (unit === DateUnitKeys.week && val) {
          end = end.add(1, DateUnitKeys.day);
        }
      }

      break;

    case DateUntilKeys.yeasterday:
      end = dayjs().startOf(DateUnitKeys.day);
      start = start.add(1, unit);

      if (isNature) {
        start = start.subtract(val, unit).startOf(isoUnit);
      } else {
        start = end.subtract(val, unit); // 关于周的特殊处理

        if (unit === DateUnitKeys.week && val) {
          start = start.subtract(1, DateUnitKeys.day);
        }
      }

      break;

    case DateUntilKeys.onHour:
      if (type === DynamincType.after) {
        start = start.startOf(DateUnitKeys.hour);
        end = start.add(val, DateUnitKeys.hour);
      } else {
        end = end.add(1, DateUnitKeys.hour).startOf(DateUnitKeys.hour);
        start = end.subtract(val, DateUnitKeys.hour);
      }

      break;

    case DateUntilKeys.onMinute:
      if (type === DynamincType.after) {
        start = start.startOf(DateUnitKeys.minute);
        end = start.add(val, DateUnitKeys.minute);
      } else {
        end = end.add(1, DateUnitKeys.minute).startOf(DateUnitKeys.minute);
        start = end.subtract(val, DateUnitKeys.minute);
      }

      break;

    case DateUntilKeys.onSecond:
      if (type === DynamincType.after) {
        start = start.startOf(DateUnitKeys.second);
        end = start.add(val, DateUnitKeys.second);
      } else {
        end = end.add(1, DateUnitKeys.second).startOf(DateUnitKeys.second);
        start = end.subtract(val, DateUnitKeys.second);
      }

      break;

    default:
      break;
  }

  return [start, end];
}; // eslint-disable-next-line @typescript-eslint/no-unused-vars


export var translateUnitToFormat = function translateUnitToFormat(_unit) {
  // normal format
  if ([DateUnitKeys.years, DateUnitKeys.month, DateUnitKeys.day, DateUnitKeys.week].includes(_unit)) {
    return TimeFormat.no;
  }

  if ([DateUnitKeys.minute, DateUnitKeys.second, DateUnitKeys.hour]) {
    return TimeFormat.hms;
  }

  return TimeFormat.no;
};
/** 翻译传进来的结构 */

export var translateFilterData = function translateFilterData(data) {
  var $and = data.$and;
  var result = [];
  $and.forEach(function (andArr) {
    var notTrans = andArr.$or.map(function (orObj) {
      var obj;

      if (orObj.$not) {
        var _a = orObj.$not,
            type = _a.type,
            value = _a.value;

        if (type === FilterTypeKeys.empty) {
          obj = {
            value: value,
            type: FilterTypeKeys.unempty
          };
        }

        if (type === FilterTypeKeys.contain) {
          obj = {
            value: value,
            type: FilterTypeKeys.nocontain
          };
        }
      } else {
        obj = orObj;
      }

      return obj;
    });
    result.push(notTrans);
  });
  return result;
};
/** 规范传出结构 */

export var formatFilterData = function formatFilterData(data) {
  var andResult = data.map(function (orArr) {
    var orObj = {
      $or: {}
    };
    orObj.$or = orArr.map(function (obj) {
      var box = obj;

      if (obj.type === FilterTypeKeys.unempty) {
        box = {
          $not: __assign(__assign({}, obj), {
            type: FilterTypeKeys.empty
          })
        };
      }

      if (obj.type === FilterTypeKeys.nocontain) {
        box = {
          $not: __assign(__assign({}, obj), {
            type: FilterTypeKeys.contain
          })
        };
      }

      return box;
    });
    return orObj;
  });
  return {
    $and: andResult
  };
};
/** DateFilter customer模式 options的类型返回 */

export var getDateCustomerOption = function getDateCustomerOption(locale, unit, nature, type) {
  var _a;

  var isRecently = type === DynamincType.before;
  var obj = (_a = {}, _a[DateUnitKeys.years] = isRecently ? [{
    label: locale.dateUnitlLocale.today,
    value: DateUntilKeys.today
  }, {
    label: locale.dateUnitlLocale.yeasterday,
    value: DateUntilKeys.yeasterday
  }, {
    label: locale.dateUnitlLocale.yearEnd,
    value: DateUntilKeys.yearEnd,
    disabled: !nature
  }] : [{
    label: locale.dateUnitlLocale.today,
    value: DateUntilKeys.today
  }, {
    label: locale.dateUnitlLocale.tomorrow,
    value: DateUntilKeys.tomorrow
  }, {
    label: locale.dateUnitlLocale.yearStart,
    value: DateUntilKeys.yearStart,
    disabled: !nature
  }], _a[DateUnitKeys.month] = isRecently ? [{
    label: locale.dateUnitlLocale.today,
    value: DateUntilKeys.today
  }, {
    label: locale.dateUnitlLocale.yeasterday,
    value: DateUntilKeys.yeasterday
  }, {
    label: locale.dateUnitlLocale.monthEnd,
    value: DateUntilKeys.monthEnd,
    disabled: !nature
  }] : [{
    label: locale.dateUnitlLocale.today,
    value: DateUntilKeys.today
  }, {
    label: locale.dateUnitlLocale.tomorrow,
    value: DateUntilKeys.tomorrow
  }, {
    label: locale.dateUnitlLocale.monthStart,
    value: DateUntilKeys.monthStart,
    disabled: !nature
  }], _a[DateUnitKeys.quarter] = isRecently ? [{
    label: locale.dateUnitlLocale.today,
    value: DateUntilKeys.today
  }, {
    label: locale.dateUnitlLocale.yeasterday,
    value: DateUntilKeys.yeasterday
  }, {
    label: locale.dateUnitlLocale.quarterEnd,
    value: DateUntilKeys.quarterEnd,
    disabled: !nature
  }] : [{
    label: locale.dateUnitlLocale.today,
    value: DateUntilKeys.today
  }, {
    label: locale.dateUnitlLocale.tomorrow,
    value: DateUntilKeys.tomorrow
  }, {
    label: locale.dateUnitlLocale.quarterStart,
    value: DateUntilKeys.quarterStart,
    disabled: !nature
  }], _a[DateUnitKeys.week] = isRecently ? [{
    label: locale.dateUnitlLocale.today,
    value: DateUntilKeys.today
  }, {
    label: locale.dateUnitlLocale.yeasterday,
    value: DateUntilKeys.yeasterday
  }, {
    label: locale.dateUnitlLocale.weekEnd,
    value: DateUntilKeys.weekend,
    disabled: !nature
  }] : [{
    label: locale.dateUnitlLocale.today,
    value: DateUntilKeys.today
  }, {
    label: locale.dateUnitlLocale.tomorrow,
    value: DateUntilKeys.tomorrow
  }, {
    label: locale.dateUnitlLocale.weekStart,
    value: DateUntilKeys.weekStart,
    disabled: !nature
  }], _a[DateUnitKeys.day] = [{
    label: locale.dateUnitlLocale.today,
    value: DateUntilKeys.today
  }, {
    label: locale.dateUnitlLocale.now,
    value: DateUntilKeys.now
  }], _a[DateUnitKeys.hour] = [{
    label: locale.dateUnitlLocale.now,
    value: DateUntilKeys.now
  }, {
    label: locale.dateUnitlLocale.onHour,
    value: DateUntilKeys.onHour
  }], _a[DateUnitKeys.minute] = [{
    label: locale.dateUnitlLocale.now,
    value: DateUntilKeys.now
  }, {
    label: locale.dateUnitlLocale.onMinute,
    value: DateUntilKeys.onMinute
  }], _a[DateUnitKeys.second] = [{
    label: locale.dateUnitlLocale.now,
    value: DateUntilKeys.now
  }, {
    label: locale.dateUnitlLocale.onSecond,
    value: DateUntilKeys.onSecond
  }], _a);
  return obj[unit];
};
/** DateFilter customer模式 包含文案 */

export var getDateCustomerIncludeTitle = function getDateCustomerIncludeTitle(locale, unit) {
  var _a;

  var obj = (_a = {}, _a[DateUnitKeys.years] = locale.dateUnitlLocale.years, _a[DateUnitKeys.month] = locale.dateUnitlLocale.month, _a[DateUnitKeys.quarter] = locale.dateUnitlLocale.quarter, _a[DateUnitKeys.day] = locale.dateUnitlLocale.day, _a[DateUnitKeys.week] = locale.dateUnitlLocale.week, _a[DateUnitKeys.hour] = locale.dateUnitlLocale.hour, _a[DateUnitKeys.minute] = locale.dateUnitlLocale.minute, _a[DateUnitKeys.second] = locale.dateUnitlLocale.second, _a);
  return obj[unit];
};
/** DateFilter 根据format修正date */

export var getDateWithFormat = function getDateWithFormat(date, format) {
  var resultDate = null;

  switch (format) {
    case TimeFormat.no:
      resultDate = date.startOf(DateUnitKeys.day);
      break;

    case TimeFormat.h:
      resultDate = date.startOf(DateUnitKeys.hour);
      break;

    case TimeFormat.hm:
      resultDate = date.startOf(DateUnitKeys.minute);
      break;

    case TimeFormat.hms:
      resultDate = date.startOf(DateUnitKeys.second);
      break;

    default:
      resultDate = date;
      break;
  }

  return resultDate;
};

