"use strict";

var __assign = void 0 && (void 0).__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);
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getDateWithFormat = exports.getDateCustomerIncludeTitle = exports.getDateCustomerOption = exports.formatFilterData = exports.translateFilterData = exports.translateUnitToFormat = exports.getDurationOfFastType = void 0;

var constant_1 = require("./constant");

var dayjs_1 = __importDefault(require("dayjs"));

var constant_2 = require("./constant");

var create_filter_1 = require("./..");

var getDurationOfFastType = function getDurationOfFastType(fast, val, type, unit, fastMap, until, include, nature) {
  if (val === void 0) {
    val = 0;
  }

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

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

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

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

    if (type === constant_1.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];
};

exports.getDurationOfFastType = getDurationOfFastType;

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 === constant_1.DateUnitKeys.week ? 'isoWeek' : unit;

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

      break;

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

      break;

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

      break;

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

      break;

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

      break;

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

      break;

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

      break;

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

      break;

    case constant_1.DateUntilKeys.today:
      if (type === constant_1.DynamincType.after) {
        start = dayjs_1["default"]().startOf(constant_1.DateUnitKeys.day);

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

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

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

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

      break;

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

      break;

    case constant_1.DateUntilKeys.tomorrow:
      start = dayjs_1["default"]().add(1, constant_1.DateUnitKeys.day).startOf(constant_1.DateUnitKeys.day);

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

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

      break;

    case constant_1.DateUntilKeys.yeasterday:
      end = dayjs_1["default"]().startOf(constant_1.DateUnitKeys.day);
      start = start.add(1, unit);

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

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

      break;

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

      break;

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

      break;

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

      break;

    default:
      break;
  }

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


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

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

  return create_filter_1.TimeFormat.no;
};

exports.translateUnitToFormat = translateUnitToFormat;
/** 翻译传进来的结构 */

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 === constant_2.FilterTypeKeys.empty) {
          obj = {
            value: value,
            type: constant_2.FilterTypeKeys.unempty
          };
        }

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

      return obj;
    });
    result.push(notTrans);
  });
  return result;
};

exports.translateFilterData = translateFilterData;
/** 规范传出结构 */

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 === constant_2.FilterTypeKeys.unempty) {
        box = {
          $not: __assign(__assign({}, obj), {
            type: constant_2.FilterTypeKeys.empty
          })
        };
      }

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

      return box;
    });
    return orObj;
  });
  return {
    $and: andResult
  };
};

exports.formatFilterData = formatFilterData;
/** DateFilter customer模式 options的类型返回 */

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

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

exports.getDateCustomerOption = getDateCustomerOption;
/** DateFilter customer模式 包含文案 */

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

  var obj = (_a = {}, _a[constant_1.DateUnitKeys.years] = locale.dateUnitlLocale.years, _a[constant_1.DateUnitKeys.month] = locale.dateUnitlLocale.month, _a[constant_1.DateUnitKeys.quarter] = locale.dateUnitlLocale.quarter, _a[constant_1.DateUnitKeys.day] = locale.dateUnitlLocale.day, _a[constant_1.DateUnitKeys.week] = locale.dateUnitlLocale.week, _a[constant_1.DateUnitKeys.hour] = locale.dateUnitlLocale.hour, _a[constant_1.DateUnitKeys.minute] = locale.dateUnitlLocale.minute, _a[constant_1.DateUnitKeys.second] = locale.dateUnitlLocale.second, _a);
  return obj[unit];
};

exports.getDateCustomerIncludeTitle = getDateCustomerIncludeTitle;
/** DateFilter 根据format修正date */

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

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

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

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

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

    default:
      resultDate = date;
      break;
  }

  return resultDate;
};

exports.getDateWithFormat = getDateWithFormat;

