function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

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 _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

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

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

import { __rest } from "tslib";
import React, { Component, createElement } from "react";
import dayjs from 'dayjs';
import { ItemContext } from '../../form';
import SelectDrawer from '../../select-drawer';
import View from '../../view';
import { isFunction } from '../../utils';
import { isValidDate, toDate, toValidDate } from '../../utils/date';
import '../styles/index.css';

var BasePicker = /*#__PURE__*/function (_Component) {
  _inherits(BasePicker, _Component);

  var _super = _createSuper(BasePicker);

  function BasePicker(props) {
    var _this;

    _classCallCheck(this, BasePicker);

    _this = _super.call(this, props);

    _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 = toDate(props.value || props.defaultValue);
    _this.state = {
      value: isValidDate(v) ? v : null,
      pickerValue: 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;
  }

  _createClass(BasePicker, [{
    key: "handleChange",
    value: function handleChange(pickerValue) {
      this.setState({
        pickerValue: pickerValue
      });
    }
  }, {
    key: "handleOk",
    value: 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
      });
    }
  }, {
    key: "handleVisibleChange",
    value: function handleVisibleChange(to) {
      var onVisibleChange = this.props.onVisibleChange;

      if (onVisibleChange) {
        onVisibleChange(to);
      }
    }
  }, {
    key: "handleCancel",
    value: function handleCancel(reason) {
      var onCancel = this.props.onCancel;
      this.setState({
        pickerValue: this.state.value || new Date()
      });

      if (isFunction(onCancel)) {
        onCancel(reason);
      }
    }
  }, {
    key: "format",
    get: function get() {
      return this.props.format || this.getDefaultFormat();
    }
  }, {
    key: "componentDidUpdate",
    value: 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 = toDate(newValue);

        if (!isValidDate(newValueDate)) {
          return;
        }

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

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

        this.setState({
          value: newValueDate,
          pickerValue: toValidDate(value)
        });
      }
    }
  }, {
    key: "render",
    value: 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 = __rest(_a, ["prefix", "iconType", "placeholder", "align", "size", "disabled", "readOnly", "hasClear"]);

      return /*#__PURE__*/React.createElement(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__*/React.createElement(SelectDrawer, _extends({}, others, {
          prefix: prefix,
          hasClear: hasClear,
          content: value ? dayjs(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__*/React.createElement(View, {
          className: "".concat(prefix, "datepicker-container")
        }, _this2.renderPicker()));
      });
    }
  }]);

  return BasePicker;
}(Component);

export default BasePicker;