import locale from './../locale'; // 日期纠正

function validateDate(val, config) {
  if (config === void 0) {
    config = {};
  }

  var type = config.type;
  var min = config.min;
  var max = config.max;
  var split = config.split || '-';
  var timeSplit = config.timeSplit || ':';
  var onError = config.onError;
  var value = val;
  var selectDate = value.toDate(split, timeSplit);
  var e = config.event || {};

  if (min && (min.isYear(split) || min.isQuarter(split) || min.isMonth(split) || min.isDate(split) || min.isDateTime(split, timeSplit) || min.isTime(timeSplit))) {
    if (type === 'year' && selectDate.compareYear(min.toDate(split, timeSplit)) === -1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能小于', 'hint_cannot_be_less_than') + min,
          select: value,
          min: min,
          value: value
        });
        return false;
      }

      value = min;
    } else if (type === 'quarter' && selectDate.compareMonth(min.toDate(split, timeSplit)) === -1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能小于', 'hint_cannot_be_less_than') + min,
          select: value,
          min: min,
          value: value
        });
        return false;
      }

      value = min;
    } else if (type === 'month' && selectDate.compareMonth(min.toDate(split, timeSplit)) === -1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能小于', 'hint_cannot_be_less_than') + min,
          select: value,
          min: min,
          value: value
        });
        return false;
      }

      value = min;
    } else if (type === 'date' && selectDate.compareDate(min.toDate(split, timeSplit)) === -1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能小于', 'hint_cannot_be_less_than') + min,
          select: value,
          min: min,
          value: value
        });
        return false;
      }

      value = min;
    } else if (type === 'datetime' && selectDate.compareDateTime(min.toDate(split, timeSplit)) === -1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能小于', 'hint_cannot_be_less_than') + min,
          select: value,
          min: min,
          value: value
        });
        return false;
      }

      value = min;
    } else if (type === 'time' && selectDate.compareTime(min.toDate(split, timeSplit)) === -1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能小于', 'hint_cannot_be_less_than') + min,
          select: value,
          min: min,
          value: value
        });
        return false;
      }

      value = min;
    }
  }

  if (max && (max.isYear(split) || max.isMonth(split) || max.isQuarter(split) || max.isDateTime(split, timeSplit) || max.isDate(split) || max.isTime(timeSplit))) {
    if (type === 'year' && selectDate.compareYear(max.toDate(split, timeSplit)) === 1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能大于', 'hint_cannot_be_greater_than') + max,
          select: value,
          max: max,
          value: value
        });
        return false;
      }

      value = max;
    } else if (type === 'quarter' && selectDate.compareMonth(max.toDate(split, timeSplit)) === 1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能大于', 'hint_cannot_be_greater_than') + max,
          select: value,
          max: max,
          value: value
        });
        return false;
      }

      value = max;
    } else if (type === 'month' && selectDate.compareMonth(max.toDate(split, timeSplit)) === 1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能大于', 'hint_cannot_be_greater_than') + max,
          select: value,
          max: max,
          value: value
        });
        return false;
      }

      value = max;
    } else if (type === 'date' && selectDate.compareDate(max.toDate(split, timeSplit)) === 1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能大于', 'hint_cannot_be_greater_than') + max,
          select: value,
          max: max,
          value: value
        });
        return false;
      }

      value = max;
    } else if (type === 'time' && selectDate.compareTime(max.toDate(split, timeSplit)) === 1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能大于', 'hint_cannot_be_greater_than') + max,
          select: value,
          max: max,
          value: value
        });
        return false;
      }

      value = max;
    } else if (type === 'datetime' && selectDate.compareDateTime(max.toDate(split, timeSplit)) === 1) {
      if (onError) {
        onError(e, {
          errMsg: locale('不能大于', 'hint_cannot_be_greater_than') + max,
          select: value,
          max: max,
          value: value
        });
        return false;
      }

      value = max;
    }
  }

  return value;
}

export default validateDate;