"use strict";

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

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

require("./index.less");

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 _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; } 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); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * @class CountDown 倒计时
 */
var CountDown = /*#__PURE__*/function (_Component) {
  _inherits(CountDown, _Component);

  var _super = _createSuper(CountDown);

  function CountDown() {
    var _this2;

    _classCallCheck(this, CountDown);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this2 = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this2), "state", {
      day: 0,
      hour: 0,
      minute: 0,
      second: 0,
      timer1: null,
      timer2: null
    });

    _defineProperty(_assertThisInitialized(_this2), "componentDidMount", function () {
      var $endTime = new Date(_this2.props.endTime).getTime();
      var timeOffset = $endTime - new Date(_this2.props.nowTime || new Date()).getTime();

      if (timeOffset > 1000) {
        _this2.startCounter();
      }
    });

    _defineProperty(_assertThisInitialized(_this2), "componentWillUnmount", function () {
      _this2.clear();
    });

    _defineProperty(_assertThisInitialized(_this2), "clear", function () {
      // eslint-disable-next-line no-unused-expressions
      _this2.state.timer1 && clearTimeout(_this2.state.timer1);
      _this2.state.timer1 = null; // eslint-disable-next-line no-unused-expressions

      _this2.state.timer2 && clearInterval(_this2.state.timer2);
      _this2.state.timer2 = null;
    });

    _defineProperty(_assertThisInitialized(_this2), "startCounter", function () {
      var _this = _assertThisInitialized(_this2);

      _this2.clear();

      _this2.state.timer1 = setTimeout(function () {
        var $endTime = new Date(_this.props.endTime).getTime();
        var timeOffset = $endTime - new Date(_this2.props.nowTime || new Date()).getTime();
        _this2.state.timer2 = setInterval(function () {
          if (timeOffset > 1000) {
            timeOffset -= 1000;
            var day = Math.floor(timeOffset / 1000 / 3600 / 24);
            var hour = Math.floor(timeOffset / 1000 / 3600 % 24);
            var minute = Math.floor(timeOffset / 1000 / 60 % 60);
            var second = Math.floor(timeOffset / 1000 % 60); // TODO:i18n string.

            _this.setState({
              day: day,
              hour: hour < 10 ? "0".concat(hour) : hour,
              minute: minute < 10 ? "0".concat(minute) : minute,
              second: second < 10 ? "0".concat(second) : second
            });
          } else {
            clearInterval(_this2.state.timer2); // eslint-disable-next-line no-unused-expressions

            _this.props.reloadCart && _this.props.reloadCart(); // window.location.reload();
            // eslint-disable-next-line no-unused-expressions

            _this.props.timeEnd && _this.props.timeEnd();
          }
        }, 1000);
      }, 0);
    });

    _defineProperty(_assertThisInitialized(_this2), "render", function () {
      var _this2$props = _this2.props,
          prefixCls = _this2$props.prefixCls,
          style = _this2$props.style,
          daysStyle = _this2$props.daysStyle,
          hourStyle = _this2$props.hourStyle,
          minuteStyle = _this2$props.minuteStyle,
          secondStyle = _this2$props.secondStyle,
          showDay = _this2$props.showDay;
      return (
        /*#__PURE__*/
        // TODO:i18n string.
        _react["default"].createElement("div", {
          className: "".concat(prefixCls),
          style: style
        }, showDay ? /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls)
        }, /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-day"),
          style: daysStyle
        }, _this2.state.day)) : null, /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-day"),
          style: hourStyle
        }, _this2.state.hour), /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-point")
        }, ":"), /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-day"),
          style: minuteStyle
        }, _this2.state.minute), /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-point")
        }, ":"), /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-day"),
          style: secondStyle
        }, _this2.state.second))
      );
    });

    return _this2;
  }

  _createClass(CountDown, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if (nextProps.endTime !== this.props.endTime) {
        var $endTime = new Date(nextProps.endTime).getTime();
        var timeOffset = $endTime - new Date(nextProps.nowTime || new Date()).getTime();

        if (timeOffset > 1000) {
          this.startCounter();
        }
      }
    }
  }]);

  return CountDown;
}(_react.Component);

exports["default"] = CountDown;

_defineProperty(CountDown, "defaultProps", {
  prefixCls: 'new-cylon-count-down',
  showDay: false
});