"use strict";

exports.__esModule = true;
exports.default = void 0;

var _dayjs = _interopRequireDefault(require("dayjs"));

var _basePicker = _interopRequireDefault(require("./base-picker"));

var _utils = require("../../time-picker/utils");

var _date = require("../../utils/date");

var _utils2 = require("../../utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function (o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

var DateTimePicker = /*#__PURE__*/function (_BasePicker) {
  _inheritsLoose(DateTimePicker, _BasePicker);

  function DateTimePicker() {
    return _BasePicker.apply(this, arguments) || this;
  }

  var _proto = DateTimePicker.prototype;

  _proto.getStep = function getStep() {
    return 'day';
  };

  _proto.getDisableType = function getDisableType() {
    return 'date';
  };

  _proto.getDataSource = function getDataSource(locale) {
    var _this$props = this.props,
        validRange = _this$props.validRange,
        _this$props$format = _this$props.format,
        format = _this$props$format === void 0 ? 'YYYY-MM-DD' : _this$props$format;
    var result = [];
    var startDate = (0, _date.cloneDate)(validRange[0]);
    startDate.setHours(0);
    startDate.setMinutes(0);
    startDate.setSeconds(0);
    startDate.setMilliseconds(0);

    for (var d = startDate; d.getTime() < validRange[1].getTime(); d = new Date(d.getTime() + 24 * 3600 * 1000)) {
      result.push({
        label: (0, _dayjs.default)(d).format(format),
        value: d.getFullYear() + "," + d.getMonth() + "," + d.getDate()
      });
    }

    var timeProps = this.timeProps;

    if (!timeProps.locale && locale && !!(0, _utils2.get)(locale, 'locale.TimePicker', undefined)) {
      timeProps.locale = locale.locale.TimePicker;
    }

    var options = (0, _utils.getTimeOption)(timeProps);
    return [result].concat((0, _utils.getColumns)(this.value, options, timeProps));
  };

  _proto.toDate = function toDate(values) {
    var options = (0, _utils.getTimeOption)(this.timeProps);
    var x = values.shift().split(',').map(function (it) {
      return parseInt(it, 10);
    });
    var d = (0, _utils.pickerToDate)(values, options);
    d.setFullYear(x[0]);
    d.setDate(1);
    d.setMonth(x[1]);
    d.setDate(x[2]);
    return d;
  };

  _proto.getRangeCheck = function getRangeCheck() {
    var showTime = this.props.showTime;
    var timeFormat = typeof showTime === 'object' ? showTime.format : undefined;
    return {
      date: this.getDateOptions(),
      time: (0, _utils.getTimeOption)({
        format: timeFormat
      })
    };
  };

  _proto.toValues = function toValues(d) {
    var options = (0, _utils.getTimeOption)(this.timeProps);
    var values = [d.getFullYear() + "," + d.getMonth() + "," + d.getDate()].concat((0, _utils.dateToPicker)(d, options));
    return values;
  };

  _proto.getDateOptions = function getDateOptions() {
    var _this$props$format2 = this.props.format,
        format = _this$props$format2 === void 0 ? 'YYYY-MM-DD' : _this$props$format2;
    return {
      hasYear: format.includes('Y'),
      hasMonth: format.includes('M'),
      hasDay: format.includes('D')
    };
  };

  _createClass(DateTimePicker, [{
    key: "validRange",
    get: function () {
      if (!this.props.validRange) {
        return undefined;
      }

      var options = (0, _utils.getTimeOption)(this.timeProps);
      return (0, _date.getRealRange)(options, this.timeProps, this.props.validRange);
    }
  }, {
    key: "timeProps",
    get: function () {
      var _this$props2 = this.props,
          showTime = _this$props2.showTime,
          validRange = _this$props2.validRange;
      var newRange;

      if (validRange) {
        var format = 'YYYY-MM-DD';
        var value = (0, _date.getRangeDate)((0, _date.toValidDate)(this.props.value || this.state.value), {
          date: this.getDateOptions()
        }, validRange);

        if ((0, _dayjs.default)(value).format(format) === (0, _dayjs.default)(validRange[0]).format(format)) {
          newRange = [validRange[0], new Date(2020, 1, 1, 23, 59, 59)];
        }

        if ((0, _dayjs.default)(value).format(format) === (0, _dayjs.default)(validRange[1]).format(format)) {
          newRange = [new Date(2020, 1, 1, 0, 0, 0), validRange[1]];
        }
      }

      if (typeof showTime === 'object') {
        return Object.assign({
          validRange: newRange
        }, showTime);
      }

      return {
        validRange: newRange
      };
    }
  }]);

  return DateTimePicker;
}(_basePicker.default);

var _default = DateTimePicker;
exports.default = _default;