"use strict";

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

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

var _tslib = require("tslib");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 IotCountdown =
/** @class */
function (_super) {
  (0, _tslib.__extends)(IotCountdown, _super);

  function IotCountdown(props) {
    var _this = _super.call(this, props) || this;

    _this.state = {
      timeLeft: Number.parseInt(props.time)
    };
    return _this;
  }

  IotCountdown.prototype.tick = function () {
    var _this = this;

    var _a = this.props,
        onComplete = _a.onComplete,
        onTick = _a.onTick;
    var timeLeft = this.state.timeLeft;

    if (timeLeft <= 0) {
      onComplete && onComplete();

      if (this.timer) {
        clearTimeout(this.timer);
        this.timer = null;
      }
    } else {
      this.timer = setTimeout(function () {
        var newTimeLeft = timeLeft - 1000;
        onTick && onTick(newTimeLeft);

        _this.setState({
          timeLeft: newTimeLeft
        });

        _this.tick();
      }, 1000);
    }
  };

  IotCountdown.prototype.componentDidMount = function () {
    if (this.state.timeLeft) {
      this.tick();
    }
  };

  IotCountdown.prototype.componentWillUnmount = function () {
    if (this.timer) {
      clearTimeout(this.timer);
    }
  };

  IotCountdown.prototype.render = function () {
    var _a = this.props,
        time = _a.time,
        text = _a.text,
        finishText = _a.finishText,
        onComplete = _a.onComplete,
        onTick = _a.onTick,
        rest = (0, _tslib.__rest)(_a, ["time", "text", "finishText", "onComplete", "onTick"]);
    var timeLeft = this.state.timeLeft;
    var showNumber = timeLeft / 1000;
    return /*#__PURE__*/_react["default"].createElement("div", (0, _tslib.__assign)({}, rest), timeLeft <= 0 ? finishText ? finishText : text : text, timeLeft ? /*#__PURE__*/_react["default"].createElement("span", null, "(" + showNumber + "s)") : null);
  };

  return IotCountdown;
}(_react.Component);

IotCountdown.defaultProps = {
  time: 10000
};
var _default = IotCountdown;
exports["default"] = _default;