"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.momentable = exports.formatMomentValue = void 0;
var _shared = require("@formily/shared");
var _moment = _interopRequireDefault(require("moment"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var moment = function moment(date, format) {
  return (0, _moment.default)((date === null || date === void 0 ? void 0 : date.toDate) ? date.toDate() : date, format);
};
var momentable = function momentable(value, format) {
  return Array.isArray(value) ? value.map(function (val) {
    return moment(val, format);
  }) : value ? moment(value, format) : value;
};
exports.momentable = momentable;
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 ((0, _shared.isArr)(format)) {
      var _format = format[i];
      if ((0, _shared.isFn)(_format)) {
        return _format(date);
      }
      if ((0, _shared.isEmpty)(_format)) {
        return date;
      }
      if (typeof date === 'number') {
        return moment(date).format(_format);
      }
      return moment(date, _format).format(_format);
    } else {
      if ((0, _shared.isFn)(format)) {
        return format(date);
      }
      if ((0, _shared.isEmpty)(format)) {
        return date;
      }
      if (typeof date === 'number') {
        return moment(date).format(format);
      }
      return moment(date, format).format(format);
    }
  };
  if ((0, _shared.isArr)(value)) {
    return value.map(function (val, index) {
      return formatDate(val, format, index);
    });
  } else {
    return value ? formatDate(value, format) : value || placeholder;
  }
};
exports.formatMomentValue = formatMomentValue;