"use strict";

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

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

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

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

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

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

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

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

var _styles = _interopRequireDefault(require("./styles"));

var _styles2 = require("@material-ui/core/styles");

var _CSSTransition = _interopRequireDefault(require("react-transition-group/CSSTransition"));

var _icons = require("../icons");

var _renderUi = require("../render-ui");

var _expander = _interopRequireDefault(require("./expander"));

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

var _classnames = _interopRequireDefault(require("classnames"));

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

var _translator = _interopRequireDefault(require("../translator"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

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

var translator = _translator["default"].translator;
/**
 * We export the raw unstyled class for testability. For public use please use the default export.
 */

var CorrectAnswerToggle = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(CorrectAnswerToggle, _React$Component);

  var _super = _createSuper(CorrectAnswerToggle);

  function CorrectAnswerToggle(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, CorrectAnswerToggle);
    _this = _super.call(this, props);
    _this.state = {
      show: props.show
    };
    CorrectAnswerToggle.defaultProps = _objectSpread(_objectSpread({}, CorrectAnswerToggle.defaultProps), {}, {
      showMessage: translator.t('common:showCorrectAnswer', {
        lng: props.language
      }),
      hideMessage: translator.t('common:hideCorrectAnswer', {
        lng: props.language
      })
    });
    return _this;
  }

  (0, _createClass2["default"])(CorrectAnswerToggle, [{
    key: "onClick",
    value: function onClick() {
      this.props.onToggle(!this.props.toggled);
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      var _this$props;

      this.setState({
        show: nextProps.show
      });

      if (nextProps.language !== ((_this$props = this.props) === null || _this$props === void 0 ? void 0 : _this$props.language)) {
        CorrectAnswerToggle.defaultProps = _objectSpread(_objectSpread({}, CorrectAnswerToggle.defaultProps), {}, {
          showMessage: translator.t('common:showCorrectAnswer', {
            lng: nextProps.language
          }),
          hideMessage: translator.t('common:hideCorrectAnswer', {
            lng: nextProps.language
          })
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          classes = _this$props2.classes,
          className = _this$props2.className,
          toggled = _this$props2.toggled,
          hideMessage = _this$props2.hideMessage,
          showMessage = _this$props2.showMessage;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])(classes.root, className)
      }, /*#__PURE__*/_react["default"].createElement(_expander["default"], {
        show: this.state.show,
        className: classes.expander
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.content,
        onClick: this.onClick.bind(this)
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.iconHolder
      }, /*#__PURE__*/_react["default"].createElement(_CSSTransition["default"], {
        timeout: 400,
        "in": toggled,
        exit: !toggled,
        classNames: classes
      }, /*#__PURE__*/_react["default"].createElement(_icons.CorrectResponse, {
        open: toggled,
        key: "correct-open",
        className: classes.icon
      })), /*#__PURE__*/_react["default"].createElement(_CSSTransition["default"], {
        timeout: 5000,
        "in": !toggled,
        exit: toggled,
        classNames: classes
      }, /*#__PURE__*/_react["default"].createElement(_icons.CorrectResponse, {
        open: toggled,
        key: "correct-closed",
        className: classes.icon
      }))), /*#__PURE__*/_react["default"].createElement(_renderUi.Readable, {
        "false": true
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.label,
        "aria-hidden": !this.state.show
      }, toggled ? hideMessage : showMessage)))));
    }
  }]);
  return CorrectAnswerToggle;
}(_react["default"].Component);

exports.CorrectAnswerToggle = CorrectAnswerToggle;
(0, _defineProperty2["default"])(CorrectAnswerToggle, "propTypes", {
  onToggle: _propTypes["default"].func,
  toggled: _propTypes["default"].bool,
  show: _propTypes["default"].bool,
  hideMessage: _propTypes["default"].string,
  showMessage: _propTypes["default"].string,
  classes: _propTypes["default"].object.isRequired,
  className: _propTypes["default"].string,
  language: _propTypes["default"].string
});
(0, _defineProperty2["default"])(CorrectAnswerToggle, "defaultProps", {
  showMessage: 'Show correct answer',
  hideMessage: 'Hide correct answer',
  show: false,
  toggled: false
});

var _default = (0, _styles2.withStyles)(_styles["default"])(CorrectAnswerToggle);

exports["default"] = _default;
//# sourceMappingURL=index.js.map