"use strict";

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

var _tslib = require("tslib");

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

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

var _form = require("../../form");

var _selectDrawer = _interopRequireDefault(require("../../select-drawer"));

var _view = _interopRequireDefault(require("../../view"));

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

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

require("../styles/index.css");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 _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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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 BasePicker = /*#__PURE__*/function (_Component) {
  _inheritsLoose(BasePicker, _Component);

  function BasePicker(props) {
    var _this = _Component.call(this, props) || this;

    _this.handleClear = function () {
      var _this$props = _this.props,
          onClear = _this$props.onClear,
          onChange = _this$props.onChange;

      if (!('value' in _this.props)) {
        _this.setState({
          pickerValue: new Date(),
          value: null
        });
      }

      if (onClear) {
        onClear();
      }

      if (onChange) {
        onChange(null);
      }
    };

    var v = (0, _date.toDate)(props.value || props.defaultValue);
    _this.state = {
      value: (0, _date.isValidDate)(v) ? v : null,
      pickerValue: (0, _date.isValidDate)(v) ? v : new Date()
    };
    _this.handleOk = _this.handleOk.bind(_assertThisInitialized(_this));
    _this.handleCancel = _this.handleCancel.bind(_assertThisInitialized(_this));
    _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));
    _this.handleVisibleChange = _this.handleVisibleChange.bind(_assertThisInitialized(_this));
    return _this;
  }

  var _proto = BasePicker.prototype;

  _proto.handleChange = function handleChange(pickerValue) {
    this.setState({
      pickerValue: pickerValue
    });
  };

  _proto.handleOk = function handleOk() {
    var _this$props2 = this.props,
        onOk = _this$props2.onOk,
        onChange = _this$props2.onChange;
    var value = this.state.pickerValue;

    if (onChange) {
      onChange(value);
    }

    if (onOk) {
      onOk(value);
    }

    this.setState({
      value: value
    });
  };

  _proto.handleVisibleChange = function handleVisibleChange(to) {
    var onVisibleChange = this.props.onVisibleChange;

    if (onVisibleChange) {
      onVisibleChange(to);
    }
  };

  _proto.handleCancel = function handleCancel(reason) {
    var onCancel = this.props.onCancel;
    this.setState({
      pickerValue: this.state.value || new Date()
    });

    if ((0, _utils.isFunction)(onCancel)) {
      onCancel(reason);
    }
  };

  _proto.componentDidUpdate = function componentDidUpdate() {
    if ('value' in this.props) {
      var _this$props3 = this.props,
          value = _this$props3.value,
          defaultValue = _this$props3.defaultValue;
      var newValue = value || defaultValue;

      if (!newValue && this.state.value) {
        this.setState({
          value: null
        });
        return;
      }

      if (newValue === this.state.value) {
        return;
      }

      var newValueDate = (0, _date.toDate)(newValue);

      if (!(0, _date.isValidDate)(newValueDate)) {
        return;
      }

      if (!this.state.value) {
        var newPickerValue = (0, _date.toValidDate)(value);
        this.setState({
          value: newValueDate,
          pickerValue: newPickerValue
        });
        return;
      }

      if (newValueDate.getTime() === this.state.value.getTime()) {
        return;
      }

      this.setState({
        value: newValueDate,
        pickerValue: (0, _date.toValidDate)(value)
      });
    }
  };

  _proto.render = function render() {
    var _this2 = this;

    var value = this.state.value;
    var _a = this.props,
        _a$prefix = _a.prefix,
        prefix = _a$prefix === void 0 ? 'mt-' : _a$prefix,
        iconType = _a.iconType,
        placeholder = _a.placeholder,
        alignProp = _a.align,
        sizeProp = _a.size,
        disabled = _a.disabled,
        readOnly = _a.readOnly,
        hasClear = _a.hasClear,
        others = (0, _tslib.__rest)(_a, ["prefix", "iconType", "placeholder", "align", "size", "disabled", "readOnly", "hasClear"]);
    return /*#__PURE__*/(0, _react.createElement)(_form.ItemContext.Consumer, null, function (context) {
      var isInsideForm = context.isInsideForm,
          contentAlign = context.contentAlign,
          contextSize = context.size;
      var align = alignProp || (isInsideForm ? contentAlign : 'left');
      var size = sizeProp || (isInsideForm ? contextSize : 'medium');
      return /*#__PURE__*/(0, _react.createElement)(_selectDrawer.default, _extends({}, others, {
        prefix: prefix,
        hasClear: hasClear,
        content: value ? (0, _dayjs.default)(value).format(_this2.format) : '',
        align: align,
        size: size,
        iconType: iconType,
        placeholder: placeholder,
        disabled: disabled,
        readOnly: readOnly,
        onOk: _this2.handleOk,
        onCancel: _this2.handleCancel,
        onClear: _this2.handleClear,
        onVisibleChange: _this2.handleVisibleChange,
        disableScroll: true
      }), /*#__PURE__*/(0, _react.createElement)(_view.default, {
        className: prefix + "datepicker-container"
      }, _this2.renderPicker()));
    });
  };

  _createClass(BasePicker, [{
    key: "format",
    get: function get() {
      return this.props.format || this.getDefaultFormat();
    }
  }]);

  return BasePicker;
}(_react.Component);

var _default = BasePicker;
exports.default = _default;