"use strict";

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

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

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

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

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

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 _react = _interopRequireDefault(require("react"));

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

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

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

var _Check = _interopRequireDefault(require("@material-ui/icons/Check"));

var _Close = _interopRequireDefault(require("@material-ui/icons/Close"));

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

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

// we need to use a larger line height for the token to be more readable
var LINE_HEIGHT_MULTIPLIER = 3.2; // we need a bit more space for correctness indicators

var CORRECTNESS_LINE_HEIGHT_MULTIPLIER = 3.4;
var CORRECTNESS_PADDING = 2;

var Wrapper = function Wrapper(_ref) {
  var useWrapper = _ref.useWrapper,
      children = _ref.children,
      classNameContainer = _ref.classNameContainer,
      iconClass = _ref.iconClass,
      Icon = _ref.Icon;
  return useWrapper ? /*#__PURE__*/_react["default"].createElement("span", {
    className: classNameContainer
  }, children, /*#__PURE__*/_react["default"].createElement(Icon, {
    className: iconClass
  })) : children;
};

Wrapper.propTypes = {
  useWrapper: _propTypes["default"].bool,
  classNameContainer: _propTypes["default"].string,
  iconClass: _propTypes["default"].string,
  Icon: _propTypes["default"].func,
  children: _propTypes["default"].element
};
var TokenTypes = {
  text: _propTypes["default"].string,
  selectable: _propTypes["default"].bool
};
exports.TokenTypes = TokenTypes;

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

  var _super = _createSuper(Token);

  function Token() {
    var _this;

    (0, _classCallCheck2["default"])(this, Token);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getClassAndIconConfig", function () {
      var _this$props = _this.props,
          selectable = _this$props.selectable,
          selected = _this$props.selected,
          classes = _this$props.classes,
          classNameProp = _this$props.className,
          disabled = _this$props.disabled,
          highlight = _this$props.highlight,
          correct = _this$props.correct,
          animationsDisabled = _this$props.animationsDisabled,
          isMissing = _this$props.isMissing;
      var isTouchEnabled = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
      var baseClassName = Token.rootClassName;
      var classNameContainer;
      var Icon;
      var iconClass;

      if (correct === undefined && selected && disabled) {
        return {
          className: (0, _classnames["default"])(classes.token, classes.selected, classes.disabledBlack)
        };
      }

      if (correct !== undefined) {
        var isCorrect = correct === true;
        return {
          className: (0, _classnames["default"])(baseClassName, classes.custom),
          classNameContainer: (0, _classnames["default"])(isCorrect ? classes.correct : classes.incorrect, classes.commonTokenStyle),
          Icon: isCorrect ? _Check["default"] : _Close["default"],
          iconClass: (0, _classnames["default"])(classes.correctnessIndicatorIcon, isCorrect ? classes.correctIcon : classes.incorrectIcon)
        };
      }

      if (isMissing) {
        return {
          className: (0, _classnames["default"])(baseClassName, classes.custom, classes.missing, classes.commonTokenStyle),
          classNameContainer: classes.commonTokenStyle,
          Icon: _Close["default"],
          iconClass: (0, _classnames["default"])(classes.correctnessIndicatorIcon, classes.incorrectIcon)
        };
      }

      return {
        className: (0, _classnames["default"])(baseClassName, classes.token, disabled && classes.disabled, selectable && !disabled && !isTouchEnabled && classes.selectable, selected && !disabled && classes.selected, selected && disabled && classes.disabledAndSelected, highlight && selectable && !disabled && !selected && classes.highlight, animationsDisabled && classes.print, classNameProp),
        classNameContainer: classNameContainer,
        Icon: Icon,
        iconClass: iconClass
      };
    });
    return _this;
  }

  (0, _createClass2["default"])(Token, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          text = _this$props2.text,
          index = _this$props2.index,
          correct = _this$props2.correct,
          isMissing = _this$props2.isMissing;

      var _this$getClassAndIcon = this.getClassAndIconConfig(),
          className = _this$getClassAndIcon.className,
          classNameContainer = _this$getClassAndIcon.classNameContainer,
          Icon = _this$getClassAndIcon.Icon,
          iconClass = _this$getClassAndIcon.iconClass;

      return /*#__PURE__*/_react["default"].createElement(Wrapper, {
        useWrapper: correct !== undefined || isMissing,
        classNameContainer: classNameContainer,
        iconClass: iconClass,
        Icon: Icon
      }, /*#__PURE__*/_react["default"].createElement("span", {
        className: className,
        dangerouslySetInnerHTML: {
          __html: (text || '').replace(/\n/g, '<br>')
        },
        "data-indexkey": index
      }));
    }
  }]);
  return Token;
}(_react["default"].Component);

exports.Token = Token;
(0, _defineProperty2["default"])(Token, "rootClassName", 'tokenRootClass');
(0, _defineProperty2["default"])(Token, "propTypes", _objectSpread(_objectSpread({}, TokenTypes), {}, {
  classes: _propTypes["default"].object.isRequired,
  text: _propTypes["default"].string.isRequired,
  className: _propTypes["default"].string,
  disabled: _propTypes["default"].bool,
  highlight: _propTypes["default"].bool,
  correct: _propTypes["default"].bool
}));
(0, _defineProperty2["default"])(Token, "defaultProps", {
  selectable: false,
  text: ''
});

var _default = (0, _styles.withStyles)(function (theme) {
  return {
    token: {
      cursor: 'pointer',
      textIndent: 0
    },
    disabled: {
      cursor: 'inherit',
      color: _renderUi.color.disabled()
    },
    disabledBlack: {
      cursor: 'inherit'
    },
    disabledAndSelected: {
      backgroundColor: _renderUi.color.blueGrey100()
    },
    selectable: (0, _defineProperty2["default"])({}, theme.breakpoints.up(769), {
      '&:hover': {
        backgroundColor: _renderUi.color.blueGrey300(),
        color: theme.palette.common.black,
        '& > *': {
          backgroundColor: _renderUi.color.blueGrey300()
        }
      }
    }),
    selected: {
      backgroundColor: _renderUi.color.blueGrey100(),
      color: theme.palette.common.black,
      lineHeight: "".concat(theme.spacing.unit * LINE_HEIGHT_MULTIPLIER, "px"),
      border: "solid 2px ".concat(_renderUi.color.blueGrey900()),
      borderRadius: '4px',
      '& > *': {
        backgroundColor: _renderUi.color.blueGrey100()
      }
    },
    highlight: {
      border: "dashed 2px ".concat(_renderUi.color.blueGrey600()),
      borderRadius: '4px',
      lineHeight: "".concat(theme.spacing.unit * LINE_HEIGHT_MULTIPLIER, "px")
    },
    print: {
      border: "dashed 2px ".concat(_renderUi.color.blueGrey600()),
      borderRadius: '4px',
      lineHeight: "".concat(theme.spacing.unit * LINE_HEIGHT_MULTIPLIER, "px"),
      color: _renderUi.color.text()
    },
    custom: {
      display: 'initial'
    },
    commonTokenStyle: {
      position: 'relative',
      borderRadius: '4px',
      color: theme.palette.common.black,
      lineHeight: "".concat(theme.spacing.unit * CORRECTNESS_LINE_HEIGHT_MULTIPLIER + CORRECTNESS_PADDING, "px"),
      padding: "".concat(CORRECTNESS_PADDING, "px")
    },
    correct: {
      border: "".concat(_renderUi.color.correctTertiary(), " solid 2px")
    },
    incorrect: {
      border: "".concat(_renderUi.color.incorrectWithIcon(), " solid 2px")
    },
    missing: {
      border: "".concat(_renderUi.color.incorrectWithIcon(), " dashed 2px")
    },
    incorrectIcon: {
      backgroundColor: _renderUi.color.incorrectWithIcon()
    },
    correctIcon: {
      backgroundColor: _renderUi.color.correctTertiary()
    },
    correctnessIndicatorIcon: {
      color: _renderUi.color.white(),
      position: 'absolute',
      top: '-8px',
      left: '-8px',
      borderRadius: '50%',
      fontSize: '12px',
      padding: '2px'
    }
  };
})(Token);

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