"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _commonUtil = require("../../utils/commonUtil");

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

var __extends = void 0 && (void 0).__extends || function () {
  var _extendStatics = function extendStatics(d, b) {
    _extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function (d, b) {
      d.__proto__ = b;
    } || function (d, b) {
      for (var p in b) {
        if (b.hasOwnProperty(p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    _extendStatics(d, b);

    function __() {
      this.constructor = d;
    }

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();

var debug = require('debug')('worker:count-down');

var CountDown =
/** @class */
function (_super) {
  __extends(CountDown, _super);

  function CountDown() {
    var _this = _super !== null && _super.apply(this, arguments) || this;

    _this.state = {
      hour: '00',
      min: '00',
      second: '00',
      nowTime: _this.props.now
    };
    _this.timer = null;

    _this.componentWillUnmount = function () {
      _this.clearTimer();
    };

    _this.calcLastTime = function () {
      try {
        var _a = _this.props,
            effDate = _a.effDate,
            expDate = _a.expDate,
            refresh = _a.refresh;
        var nowTime = _this.state.nowTime;
        var lastTime = 0;

        if (nowTime > effDate && nowTime < expDate) {
          lastTime = expDate - nowTime;
        } else if (nowTime < effDate) {
          lastTime = effDate - nowTime;
        }

        if (lastTime === 0) {
          refresh();

          _this.clearTimer();
        }

        var _b = (0, _commonUtil.calcLastTime)(lastTime).split(':'),
            hour = _b[0],
            min = _b[1],
            second = _b[2];

        _this.setState({
          hour: hour,
          min: min,
          second: second,
          nowTime: nowTime + 1000
        });
      } catch (error) {
        debug(error);
      }
    };

    _this.clearTimer = function () {
      if (_this.timer) {
        clearInterval(_this.timer);
        _this.timer = null;
      }
    };

    return _this;
  }

  CountDown.prototype.componentWillReceiveProps = function (nextProps) {
    var _this = this;

    if (nextProps.now !== this.props.now) {
      this.setState({
        nowTime: nextProps.now
      }, function () {
        _this.clearTimer();

        _this.calcLastTime();

        _this.timer = setInterval(_this.calcLastTime, 1000);
      });
    }
  };

  CountDown.prototype.render = function () {
    var _a = this.state,
        hour = _a.hour,
        min = _a.min,
        second = _a.second;
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "count-down-wrap"
    }, /*#__PURE__*/_react["default"].createElement("span", null, hour), /*#__PURE__*/_react["default"].createElement("span", null, min), /*#__PURE__*/_react["default"].createElement("span", null, second));
  };

  CountDown.defaultProps = {
    effDate: '',
    expDate: '',
    now: '',
    refresh: function refresh() {}
  };
  return CountDown;
}(_react.Component);

var _default = CountDown;
exports["default"] = _default;