"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _moment = _interopRequireDefault(require("moment"));

var _picker = _interopRequireDefault(require("../../../picker"));

var _timePicker = _interopRequireDefault(require("../../../time-picker"));

require("./index.css");

var _excluded = ["label", "layout", "require", "value", "onChange", "datePopupAlign", "timePopupAlign"];

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var prefixCls = 'form-field-date';

var Date = function Date(props) {
  var label = props.label,
      layout = props.layout,
      require = props.require,
      _props$value = props.value,
      value = _props$value === void 0 ? (0, _moment["default"])() : _props$value,
      onChange = props.onChange,
      datePopupAlign = props.datePopupAlign,
      timePopupAlign = props.timePopupAlign,
      restProps = _objectWithoutProperties(props, _excluded);

  var onDateChange = function onDateChange(date, type) {
    var newDate;

    switch (type) {
      case 'date':
        newDate = (0, _moment["default"])(value || (0, _moment["default"])()).set({
          year: (0, _moment["default"])(date[0]).year(),
          month: (0, _moment["default"])(date[0]).month(),
          date: (0, _moment["default"])(date[0]).date()
        });
        break;

      case "time":
        newDate = (0, _moment["default"])(value || (0, _moment["default"])()).set({
          hour: (0, _moment["default"])(date[0], "HH:mm").hour(),
          minute: (0, _moment["default"])(date[0], "HH:mm").minute()
        });
        break;

      default:
        break;
    }

    onChange(newDate);
    return true;
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])(prefixCls, _defineProperty({}, "".concat(prefixCls, "-row"), layout === 'row'))
  }, !!label && /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefixCls, "__label")
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefixCls, "__label-text")
  }, label), require && /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefixCls, "__label-symbol")
  }, "*")), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(prefixCls, "__pickers"))
  }, /*#__PURE__*/_react["default"].createElement(_picker["default"], _extends({
    value: [value],
    onChange: function onChange(date) {
      return onDateChange(date, 'date');
    },
    popupAlign: datePopupAlign
  }, restProps)), /*#__PURE__*/_react["default"].createElement(_timePicker["default"], _extends({
    value: value,
    popupAlign: timePopupAlign,
    onChange: function onChange(time) {
      return onDateChange([time], "time");
    }
  }, restProps))));
};

Date.defaultProps = {
  disabled: false,
  layout: 'column',
  require: false
};
var _default = Date;
exports["default"] = _default;