"use strict";

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

var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");

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

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

var _context = _interopRequireDefault(require("../../locale/context"));

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

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

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

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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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); Object.defineProperty(Constructor, "prototype", { writable: false }); 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;

    var d = (0, _date.toDate)(props.value || props.defaultValue);

    var realDate = _this.getAvailableDate((0, _date.isValidDate)(d) ? d : new Date());

    _this.state = {
      value: realDate
    };
    _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));

    if (d && realDate.getTime() !== d.getTime() && props.onChange) {
      props.onChange(realDate);
    }

    return _this;
  }

  var _proto = BasePicker.prototype;

  _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
    if ('value' in this.props) {
      var nowValue = this.state.value;
      var _this$props = this.props,
          value = _this$props.value,
          defaultValue = _this$props.defaultValue;

      if (value === prevProps.value) {
        return;
      }

      var newValue = this.getAvailableDate((0, _date.toValidDate)(value || defaultValue));

      if (newValue.getTime() === nowValue.getTime()) {
        return;
      }

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

  _proto.filterDataSource = function filterDataSource(source, changeDate, type) {
    var validRange = this.validRange;
    var disabledDate = this.props.disabledDate;

    if (!disabledDate && !validRange) {
      return source;
    }

    for (var i = 0; i < source.length; i++) {
      var startDate = new Date();
      changeDate(startDate, source[i]);
      var endDate = (0, _dayjs.default)(startDate).endOf(type);
      var isDisable = (0, _utils.isFunction)(disabledDate) ? disabledDate(startDate, type) && disabledDate(endDate.toDate(), type) : false;

      if (!isDisable && validRange && this.getStep() === type) {
        isDisable = (0, _date.checkDateRange)(startDate, this.getRangeCheck(), validRange) !== _date.RANGE_CHECK_RESULT.IN_RANGE;
      }

      if (isDisable) {
        source.splice(i, 1);
        i--;
      }
    }

    return source;
  };

  _proto.getAvailableDate = function getAvailableDate(d) {
    var validRange = this.validRange;
    var baseDate = d;

    if (validRange) {
      baseDate = (0, _date.getRangeDate)(d, this.getRangeCheck(), validRange);
    }

    if (this.isAvailable(baseDate)) {
      return baseDate;
    }

    var dayjsObj = (0, _dayjs.default)(baseDate);

    for (var i = 0; i < 9999; i++) {
      var a = dayjsObj.add(i, this.getStep()).toDate();

      if (this.isAvailable(a)) {
        return a;
      }

      var b = dayjsObj.add(0 - i, this.getStep()).toDate();

      if (this.isAvailable(a)) {
        return b;
      }
    }

    return baseDate;
  };

  _proto.handleChange = function handleChange(values) {
    var newDate = this.getAvailableDate(this.toDate(values));

    if (!('value' in this.props)) {
      this.setState({
        value: newDate
      });
    }

    if (this.props.onChange) {
      this.props.onChange(newDate);
    }
  };

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

    var _this$props2 = this.props,
        id = _this$props2.id,
        className = _this$props2.className,
        style = _this$props2.style;
    return /*#__PURE__*/(0, _react.createElement)(_context.default.Consumer, null, function (locale) {
      return /*#__PURE__*/(0, _react.createElement)(_picker.default, {
        id: id,
        className: className,
        style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(style),
        value: _this2.toValues(_this2.value),
        data: _this2.getDataSource(locale || {}),
        onChange: _this2.handleChange
      });
    });
  };

  _proto.hasAvailable = function hasAvailable() {
    var validRange = this.validRange;
    var disabledDate = this.props.disabledDate;
    return disabledDate || validRange;
  };

  _proto.isAvailable = function isAvailable(d) {
    var validRange = this.validRange;
    var disabledDate = this.props.disabledDate;

    if (!this.hasAvailable()) {
      return true;
    }

    if (validRange) {
      if ((0, _date.checkDateRange)(d, this.getRangeCheck(), validRange) !== _date.RANGE_CHECK_RESULT.IN_RANGE) {
        return false;
      }
    }

    if (disabledDate) {
      return !disabledDate(d, this.getDisableType());
    }

    return true;
  };

  _createClass(BasePicker, [{
    key: "value",
    get: function get() {
      if (this.props.value) {
        return this.getAvailableDate((0, _date.toValidDate)(this.props.value));
      }

      return this.state.value;
    }
  }, {
    key: "validRange",
    get: function get() {
      return this.props.validRange;
    }
  }]);

  return BasePicker;
}(_react.Component);

var _default = BasePicker;
exports.default = _default;