"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

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

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

var _LocaleReceiver = _interopRequireDefault(require("../locale-provider/LocaleReceiver"));

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

var _en_US = _interopRequireDefault(require("./locale/en_US"));

var _Panel = _interopRequireDefault(require("../rc-components/time-picker/Panel"));

var _enum = require("../_util/enum");

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

function getColumns(_ref) {
  var showHour = _ref.showHour,
      showMinute = _ref.showMinute,
      showSecond = _ref.showSecond,
      use12Hours = _ref.use12Hours;
  var column = 0;

  if (showHour) {
    column += 1;
  }

  if (showMinute) {
    column += 1;
  }

  if (showSecond) {
    column += 1;
  }

  if (use12Hours) {
    column += 1;
  }

  return column;
}

function wrapPicker(Picker, defaultFormat) {
  var _a;

  return _a = /*#__PURE__*/function (_Component) {
    (0, _inherits2["default"])(PickerWrapper, _Component);

    var _super = (0, _createSuper2["default"])(PickerWrapper);

    function PickerWrapper() {
      var _this;

      (0, _classCallCheck2["default"])(this, PickerWrapper);
      _this = _super.apply(this, arguments);

      _this.handleOpenChange = function (open) {
        var onOpenChange = _this.props.onOpenChange;
        onOpenChange(open);
      };

      _this.handleFocus = function (e) {
        var onFocus = _this.props.onFocus;

        if (onFocus) {
          onFocus(e);
        }
      };

      _this.handleBlur = function (e) {
        var onBlur = _this.props.onBlur;

        if (onBlur) {
          onBlur(e);
        }
      };

      _this.savePicker = function (node) {
        _this.picker = node;
      };

      _this.getDefaultLocale = function () {
        var locale = _this.props.locale;
        var result = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, _en_US["default"]), locale);
        result.lang = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, result.lang), (locale || {}).lang);
        return result;
      };

      _this.renderPicker = function (locale, localeCode) {
        var _classNames2, _classNames3;

        var _assertThisInitialize = (0, _assertThisInitialized2["default"])(_this),
            props = _assertThisInitialize.props;

        var getPrefixCls = _this.context.getPrefixCls;
        var _props$prefixCls = props.prefixCls,
            prefixCls = _props$prefixCls === void 0 ? getPrefixCls('calendar') : _props$prefixCls,
            _props$inputPrefixCls = props.inputPrefixCls,
            inputPrefixCls = _props$inputPrefixCls === void 0 ? getPrefixCls('input') : _props$inputPrefixCls;
        var pickerClass = (0, _classnames["default"])("".concat(prefixCls, "-picker"), (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-picker-").concat(props.size), !!props.size));
        var pickerInputClass = (0, _classnames["default"])("".concat(prefixCls, "-picker-input"), inputPrefixCls, (_classNames2 = {}, (0, _defineProperty2["default"])(_classNames2, "".concat(inputPrefixCls, "-lg"), props.size === _enum.Size.large), (0, _defineProperty2["default"])(_classNames2, "".concat(inputPrefixCls, "-sm"), props.size === _enum.Size.small), (0, _defineProperty2["default"])(_classNames2, "".concat(inputPrefixCls, "-disabled"), props.disabled), _classNames2));
        var pickerWrapperInputClass = (0, _classnames["default"])("".concat(inputPrefixCls, "-wrapper"), (_classNames3 = {}, (0, _defineProperty2["default"])(_classNames3, "".concat(inputPrefixCls, "-disabled"), props.disabled), (0, _defineProperty2["default"])(_classNames3, "".concat(inputPrefixCls, "-has-border"), props.border && props.layoutLayout === 'float'), _classNames3));
        var timeFormat = props.showTime && props.showTime.format || 'HH:mm:ss';
        var rcTimePickerProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, (0, _timePicker.generateShowHourMinuteSecond)(timeFormat)), {}, {
          format: timeFormat,
          use12Hours: props.showTime && props.showTime.use12Hours
        });
        var columns = getColumns(rcTimePickerProps);
        var timePickerCls = "".concat(prefixCls, "-time-picker-column-").concat(columns);
        var timePicker = props.showTime ? /*#__PURE__*/_react["default"].createElement(_Panel["default"], (0, _extends2["default"])({}, rcTimePickerProps, props.showTime, {
          prefixCls: "".concat(prefixCls, "-time-picker"),
          className: timePickerCls,
          placeholder: locale.timePickerLocale.placeholder,
          transitionName: "slide-up"
        })) : null;
        return /*#__PURE__*/_react["default"].createElement(Picker, (0, _extends2["default"])({}, props, {
          ref: _this.savePicker,
          pickerClass: pickerClass,
          pickerInputClass: pickerInputClass,
          pickerWrapperInputClass: pickerWrapperInputClass,
          locale: locale,
          localeCode: localeCode,
          timePicker: timePicker,
          onOpenChange: _this.handleOpenChange,
          onFocus: _this.handleFocus,
          onBlur: _this.handleBlur
        }));
      };

      return _this;
    }

    (0, _createClass2["default"])(PickerWrapper, [{
      key: "componentDidMount",
      value: function componentDidMount() {
        var _this$props = this.props,
            autoFocus = _this$props.autoFocus,
            disabled = _this$props.disabled;

        if (autoFocus && !disabled) {
          this.focus();
        }
      }
    }, {
      key: "focus",
      value: function focus() {
        this.picker.focus();
      }
    }, {
      key: "blur",
      value: function blur() {
        this.picker.blur();
      }
    }, {
      key: "render",
      value: function render() {
        return /*#__PURE__*/_react["default"].createElement(_LocaleReceiver["default"], {
          componentName: "DatePicker",
          defaultLocale: this.getDefaultLocale
        }, this.renderPicker);
      }
    }], [{
      key: "contextType",
      get: function get() {
        return _ConfigContext["default"];
      }
    }]);
    return PickerWrapper;
  }(_react.Component), _a.displayName = 'PickerWrapper', _a.defaultProps = {
    format: defaultFormat || 'YYYY-MM-DD',
    transitionName: 'slide-up',
    popupStyle: {},
    onChange: function onChange() {},
    onOk: function onOk() {},
    onOpenChange: function onOpenChange() {},
    locale: {},
    border: true,
    layoutLayout: 'float'
  }, _a;
}
//# sourceMappingURL=wrapPicker.js.map
