"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports["default"] = void 0;

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

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

var _moment = _interopRequireDefault(require("moment"));

var _title = _interopRequireDefault(require("../title"));

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 renderNumberWithUnit(num, unit) {
  return /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement("span", null, num.toString().length < 2 ? "0" + num : num), /*#__PURE__*/_react["default"].createElement("span", null, unit));
}

;

/**
 * 时间器
 *
 * @class Timer
 * @extends {PureComponent<IProps, IState>}
 */
var Timer = /*#__PURE__*/function (_PureComponent) {
  (0, _inheritsLoose2["default"])(Timer, _PureComponent);

  function Timer(props) {
    var _this;

    _this = _PureComponent.call(this, props) || this;
    _this.updateTimer = void 0;
    _this.state = {
      time: (0, _moment["default"])()
    };
    return _this;
  }

  var _proto = Timer.prototype;

  _proto.componentDidMount = function componentDidMount() {
    window.moment = _moment["default"];
    this.updateTime();
  };

  _proto.componentWillUnmount = function componentWillUnmount() {
    clearTimeout(this.updateTimer);
  };

  _proto.updateTime = function updateTime() {
    var _this$props = this.props,
        start = _this$props.start,
        end = _this$props.end;

    if (!(start && end)) {
      this.setState({
        time: (0, _moment["default"])()
      });
      this.updateTimer = setTimeout(this.updateTime.bind(this), 1000);
    }
  };

  _proto.processTime = function processTime(a, b) {
    var start = (0, _moment["default"])(a);
    var end = (0, _moment["default"])(b);
    var diff = end.diff(start);
    return {
      diff: diff,
      days: _moment["default"].duration(diff).days(),
      hours: _moment["default"].duration(diff).hours(),
      minutes: _moment["default"].duration(diff).minutes(),
      seconds: _moment["default"].duration(diff).seconds()
    };
  };

  _proto.renderFormatTime = function renderFormatTime(obj) {
    var humanize = this.props.humanize;
    var days = obj.days,
        hours = obj.hours,
        minutes = obj.minutes,
        seconds = obj.seconds;

    if (!humanize) {
      return /*#__PURE__*/_react["default"].createElement("span", null, renderNumberWithUnit(days, '天'), renderNumberWithUnit(hours, '小时'), renderNumberWithUnit(minutes, '分'), renderNumberWithUnit(seconds, '秒'));
    }

    if (!days && !hours && !minutes) {
      return /*#__PURE__*/_react["default"].createElement("span", null, renderNumberWithUnit(seconds, '秒'));
    } else if (minutes && !days && !hours) {
      return /*#__PURE__*/_react["default"].createElement("span", null, renderNumberWithUnit(minutes, '分'), renderNumberWithUnit(seconds, '秒'));
    } else if (hours && !days) {
      return /*#__PURE__*/_react["default"].createElement("span", null, renderNumberWithUnit(hours, '小时'), renderNumberWithUnit(minutes, '分'), renderNumberWithUnit(seconds, '秒'));
    }

    return /*#__PURE__*/_react["default"].createElement("span", null, renderNumberWithUnit(days, '天'), renderNumberWithUnit(hours, '小时'), renderNumberWithUnit(minutes, '分'), renderNumberWithUnit(seconds, '秒'));
  };

  _proto.renderTime = function renderTime() {
    var _this$props2 = this.props,
        start = _this$props2.start,
        end = _this$props2.end,
        format = _this$props2.format,
        render = _this$props2.render;
    var time = this.state.time;

    if (!start && !end) {
      var _origin = time.format(format); // 显示当前时间


      return render ? render(time, _origin) : _origin;
    } else if (start && !end) {
      // 距离此刻的时长
      var _obj = this.processTime(start, time);

      var _origin2 = this.renderFormatTime(_obj);

      return render ? render(_obj.diff, _origin2) : _origin2;
    } else if (!start && end) {
      // 倒计时
      var _obj2 = this.processTime(time, end);

      var _origin3 = this.renderFormatTime(_obj2);

      return render ? render(_obj2.diff, _origin3) : _origin3;
    } else {
      // 时长
      var _obj3 = this.processTime(start, end);

      var _origin4 = this.renderFormatTime(_obj3);

      return render ? render(_obj3.diff, _origin4) : _origin4;
    }
  };

  _proto.render = function render() {
    return /*#__PURE__*/_react["default"].createElement(_title["default"], this.props, this.renderTime());
  };

  return Timer;
}(_react.PureComponent); // @ts-ignore


Timer.defaultProps = {
  className: null,
  humanize: true,
  format: 'MM月DD日 HH:mm:ss',
  align: 'middle',
  textStyle: {
    fontWeight: 400
  }
};
var _default = Timer;
exports["default"] = _default;