import { isArr, isEmpty, isFn } from '@formily/shared';
import Moment from 'moment';
var moment = function moment(date, format) {
  return Moment((date === null || date === void 0 ? void 0 : date.toDate) ? date.toDate() : date, format);
};
export var momentable = function momentable(value, format) {
  return Array.isArray(value) ? value.map(function (val) {
    return moment(val, format);
  }) : value ? moment(value, format) : value;
};
export var formatMomentValue = function formatMomentValue(value, format, placeholder) {
  var formatDate = function formatDate(date, format) {
    var i = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
    if (!date) return placeholder;
    if (isArr(format)) {
      var _format = format[i];
      if (isFn(_format)) {
        return _format(date);
      }
      if (isEmpty(_format)) {
        return date;
      }
      if (typeof date === 'number') {
        return moment(date).format(_format);
      }
      return moment(date, _format).format(_format);
    } else {
      if (isFn(format)) {
        return format(date);
      }
      if (isEmpty(format)) {
        return date;
      }
      if (typeof date === 'number') {
        return moment(date).format(format);
      }
      return moment(date, format).format(format);
    }
  };
  if (isArr(value)) {
    return value.map(function (val, index) {
      return formatDate(val, format, index);
    });
  } else {
    return value ? formatDate(value, format) : value || placeholder;
  }
};