"use strict";

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

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

var _propTypes = require("prop-types");

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _styles = require("@vincent_za/styles");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _typeof(obj) { 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); }

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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

function _templateObject3() {
  var data = _taggedTemplateLiteral(["\n  margin-right: 2rem;\n  margin-left: 0.6rem;\n  color: ", ";\n"]);

  _templateObject3 = function _templateObject3() {
    return data;
  };

  return data;
}

function _templateObject2() {
  var data = _taggedTemplateLiteral(["\n  text-transform: uppercase;\n  cursor: pointer;\n  color: ", ";\n  font-size: ", ";\n  padding: ", " 0.6rem;\n\n  &:hover {\n    color: ", ";\n  };\n"]);

  _templateObject2 = function _templateObject2() {
    return data;
  };

  return data;
}

function _templateObject() {
  var data = _taggedTemplateLiteral(["\n  display: flex;\n  margin-bottom: ", ";\n  background-color: ", ";\n  padding: ", " 1.4rem ", " 2rem;\n  align-items: center;\n  border-radius: 1px;\n  text-transform: uppercase;\n  color: ", ";\n  font-size: ", ";\n  font-family: ", ";\n  box-shadow: 0 0.6rem 1.1rem -0.4rem ", ", 0 0 1.6rem -2px ", ";\n"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var Wrap = _styledComponents.default.div(_templateObject(), _styles.REM_UNIT_LG, _styles.ARSENIC, _styles.REM_UNIT_MD, _styles.REM_UNIT_MD, _styles.VERY_LIGHT_TANGELO, _styles.REM_FONT_SIZE__SM, _styles.FONT_BOLD, _styles.EERIE_BLACK, _styles.EERIE_BLACK);

var Action = _styledComponents.default.div(_templateObject2(), _styles.GRAY_X11, _styles.REM_FONT_SIZE_SM, _styles.REM_UNIT_XSM, _styles.GAINSBORO);

var TimeRemaining = _styledComponents.default.span(_templateObject3(), _styles.SONIC_SILVER);

var Undo =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Undo, _React$Component);

  function Undo(props) {
    var _this;

    _classCallCheck(this, Undo);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Undo).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "onRemove", function () {
      clearInterval(_this.state.intervalId);

      _this.props.onRemove({
        id: _this.props.details.id
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onUndo", function () {
      var _this$props = _this.props,
          _this$props$details = _this$props.details,
          undoCall = _this$props$details.undoCall,
          params = _this$props$details.params,
          onUndo = _this$props.onUndo;

      _this.onRemove();

      onUndo({
        undoCall: undoCall,
        params: params
      });
    });

    _defineProperty(_assertThisInitialized(_this), "countDown", function () {
      var secondsRemaining = _this.state.secondsRemaining;

      if (secondsRemaining) {
        _this.setState({
          secondsRemaining: secondsRemaining - 1
        });
      }

      if (!secondsRemaining) {
        clearInterval(_this.state.intervalId);

        _this.props.onRemove({
          id: _this.props.details.id
        });
      }
    });

    _this.state = {
      intervalId: undefined,
      secondsRemaining: 5
    };
    return _this;
  }

  _createClass(Undo, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      this.setState({
        intervalId: setInterval(function () {
          return _this2.countDown();
        }, 1000)
      });
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState) {
      if (nextState.intervalId !== this.state.intervalId) return true;
      if (nextState.secondsRemaining !== this.state.secondsRemaining) return true;
      if (nextProps.details !== this.props.details) return true;
      return false;
    }
  }, {
    key: "render",
    value: function render() {
      var secondsRemaining = this.state.secondsRemaining;
      var title = this.props.details.title;
      return _react.default.createElement(Wrap, null, title, _react.default.createElement(TimeRemaining, null, secondsRemaining), _react.default.createElement(Action, {
        onClick: this.onRemove
      }, "Cancel"), _react.default.createElement(Action, {
        onClick: this.onUndo
      }, "Undo"));
    }
  }]);

  return Undo;
}(_react.default.Component);

Undo.propTypes = {
  details: (0, _propTypes.shape)({
    id: _propTypes.string.isRequired,
    title: _propTypes.string.isRequired
  }).isRequired,
  onUndo: _propTypes.func.isRequired,
  onRemove: _propTypes.func.isRequired
};
var _default = Undo;
exports.default = _default;