"use strict";

exports.__esModule = true;
exports.default = void 0;
var _tslib = require("tslib");
var _react = _interopRequireWildcard(require("react"));
var _dayjs = _interopRequireDefault(require("dayjs"));
var _view = _interopRequireDefault(require("../../view"));
var _selectDrawer = _interopRequireDefault(require("../../select-drawer"));
var _form = require("../../form");
var _locale = require("../../locale");
var _utils = require("../../utils");
var _date = require("../../utils/date");
var _utils2 = require("../utils");
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 ? Object.assign.bind() : 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, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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 ? Object.setPrototypeOf.bind() : 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.ref = /*#__PURE__*/(0, _react.createRef)();
    _this.calendarRef = /*#__PURE__*/(0, _react.createRef)();
    _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);
    var pv = new Date();
    if ((0, _date.isValidDate)(v)) {
      pv = v;
    } else if ((0, _utils.isValidArray)(_this.props.validRange) && !(0, _date.isBetween)(new Date(), _this.props.validRange[0], _this.props.validRange[1], 'd')) {
      pv = _this.props.validRange[0];
    }
    _this.state = {
      value: (0, _date.isValidDate)(v) ? v : null,
      pickerValue: pv,
      yearPickerVisible: false
    };
    _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));
    _this.show = _this.show.bind(_assertThisInitialized(_this));
    _this.hide = _this.hide.bind(_assertThisInitialized(_this));
    _this.handleYearPickerVisibleChange = _this.handleYearPickerVisibleChange.bind(_assertThisInitialized(_this));
    return _this;
  }
  var _proto = BasePicker.prototype;
  _proto.handleChange = function handleChange(pickerValue) {
    this.setState({
      pickerValue: pickerValue
    });
  };
  _proto.handleYearPickerVisibleChange = function handleYearPickerVisibleChange(show) {
    this.setState({
      yearPickerVisible: show
    });
  };
  _proto.handleOk = function handleOk() {
    var _a;
    var _this$props2 = this.props,
      onOk = _this$props2.onOk,
      onChange = _this$props2.onChange,
      validRange = _this$props2.validRange,
      disabledDate = _this$props2.disabledDate,
      format = _this$props2.format;
    var _this$state = this.state,
      value = _this$state.pickerValue,
      yearPickerVisible = _this$state.yearPickerVisible;
    if (yearPickerVisible && this.calendarRef.current) {
      (_a = this.calendarRef.current) === null || _a === void 0 ? void 0 : _a.toggleYearPicker(false);
      return false;
    }
    if (value) {
      if (Array.isArray(validRange) && !(0, _date.isBetween)(value, validRange[0], validRange[1], (0, _utils2.getFormatType)(format, true))) {
        return false;
      }
      if ((0, _utils.isFunction)(disabledDate) && disabledDate(value, (0, _utils2.getFormatType)(format))) {
        return false;
      }
    }
    if (onChange) {
      onChange(value);
    }
    if (onOk) {
      onOk(value);
    }
    this.setState({
      value: value
    });
    return true;
  };
  _proto.handleVisibleChange = function handleVisibleChange(to) {
    var onVisibleChange = this.props.onVisibleChange;
    if (onVisibleChange) {
      onVisibleChange(to);
    }
    var yearPickerVisible = this.state.yearPickerVisible;
    if (to && yearPickerVisible) {
      this.setState({
        yearPickerVisible: false
      });
    }
  };
  _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.show = function show() {
    var _a, _b;
    (_b = (_a = this.ref) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.show();
  };
  _proto.hide = function hide() {
    var _a, _b;
    (_b = (_a = this.ref) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.hide();
  };
  _proto.render = function render() {
    var _this2 = this;
    var _this$state2 = this.state,
      value = _this$state2.value,
      yearPickerVisible = _this$state2.yearPickerVisible;
    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)(_locale.Context.Consumer, null, function (localeContext) {
        var _a, _b;
        return /*#__PURE__*/(0, _react.createElement)(_selectDrawer.default, _extends({}, others, {
          ref: _this2.ref,
          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,
          okText: yearPickerVisible ? ((_b = (_a = localeContext === null || localeContext === void 0 ? void 0 : localeContext.locale) === null || _a === void 0 ? void 0 : _a.DatePicker) === null || _b === void 0 ? void 0 : _b.foldUp) || '收起' : undefined,
          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;