"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 _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 _renderUi = require("../../render-ui");

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

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

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 Wrapper = function Wrapper(_ref) {
  var useWrapper = _ref.useWrapper,
      children = _ref.children,
      classNameContainer = _ref.classNameContainer,
      iconClass = _ref.iconClass,
      Icon = _ref.Icon;

  if (useWrapper) {
    return /*#__PURE__*/_react["default"].createElement("span", {
      className: classNameContainer
    }, children, /*#__PURE__*/_react["default"].createElement(Icon, {
      className: iconClass,
      viewBox: '0 1 24 24'
    }));
  }

  return 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() {
    (0, _classCallCheck2["default"])(this, Token);
    return _super.apply(this, arguments);
  }

  (0, _createClass2["default"])(Token, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          text = _this$props.text,
          selectable = _this$props.selectable,
          selected = _this$props.selected,
          classes = _this$props.classes,
          classNameProp = _this$props.className,
          disabled = _this$props.disabled,
          index = _this$props.index,
          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 className;
      var classNameContainer;
      var Icon;
      var iconClass;

      if (correct === undefined && selected && disabled) {
        className = (0, _classnames["default"])(classes.token, classes.selected, classes.disabledBlack);
      } else if (correct !== undefined) {
        className = (0, _classnames["default"])(Token.rootClassName, classes.custom);
        Icon = correct ? _Check["default"] : _Close["default"];
        classNameContainer = correct === true ? classes.correct : classes.incorrect;
        iconClass = correct === true ? classes.correctIcon : classes.incorrectIcon;
      } else if (isMissing) {
        className = (0, _classnames["default"])(Token.rootClassName, classes.custom, isMissing === true && classes.missing);
      } else {
        className = (0, _classnames["default"])(Token.rootClassName, 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);
      }

      return /*#__PURE__*/_react["default"].createElement(Wrapper, {
        useWrapper: correct !== undefined,
        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,
  className: _propTypes["default"].string,
  disabled: _propTypes["default"].bool,
  highlight: _propTypes["default"].bool,
  correct: _propTypes["default"].bool,
  text: _propTypes["default"].string.isRequired
}));
(0, _defineProperty2["default"])(Token, "defaultProps", {
  selectable: false,
  text: ''
});

var _default = (0, _styles.withStyles)(function (theme) {
  return {
    token: {
      cursor: 'pointer',
      textIndent: 0,
      padding: theme.spacing.unit / 2,
      paddingRight: 0,
      paddingLeft: 0,
      transition: 'background-color 100ms ease-in'
    },
    disabled: {
      cursor: 'inherit',
      color: _renderUi.color.disabled()
    },
    disabledBlack: {
      cursor: 'inherit'
    },
    disabledAndSelected: {
      backgroundColor: _renderUi.color.secondaryLight()
    },
    selectable: (0, _defineProperty2["default"])({}, theme.breakpoints.up(769), {
      '&:hover': {
        backgroundColor: _renderUi.color.primaryLight(),
        '& > *': {
          backgroundColor: _renderUi.color.primaryLight()
        }
      }
    }),
    selected: {
      lineHeight: 2,
      marginTop: theme.spacing.unit / 2,
      backgroundColor: _renderUi.color.primaryLight(),
      '& > *': {
        backgroundColor: _renderUi.color.primaryLight()
      }
    },
    highlight: {
      border: "dashed 2px ".concat(_renderUi.color.disabled()),
      lineHeight: 2,
      boxSizing: 'border-box',
      marginTop: theme.spacing.unit / 2,
      display: 'inline-block',
      padding: theme.spacing.unit
    },
    print: {
      border: "dashed 2px ".concat(_renderUi.color.disabled()),
      lineHeight: 2,
      boxSizing: 'border-box',
      marginTop: theme.spacing.unit / 2,
      display: 'inline-block',
      padding: theme.spacing.unit,
      color: _renderUi.color.text()
    },
    custom: {
      display: 'initial'
    },
    correct: {
      backgroundColor: _renderUi.color.correctSecondary(),
      border: "".concat(_renderUi.color.correct(), " solid 2px"),
      lineHeight: "".concat(theme.spacing.unit * 4, "px")
    },
    incorrect: {
      backgroundColor: _renderUi.color.incorrectSecondary(),
      border: "".concat(_renderUi.color.missing(), " solid 2px"),
      lineHeight: "".concat(theme.spacing.unit * 4, "px")
    },
    missing: {
      backgroundColor: _renderUi.color.incorrectSecondary(),
      border: "".concat(_renderUi.color.missing(), " dashed 2px"),
      textDecoration: "line-through ".concat(_renderUi.color.missing())
    },
    incorrectIcon: {
      verticalAlign: 'middle',
      fontSize: 'larger',
      color: _renderUi.color.missing()
    },
    correctIcon: {
      verticalAlign: 'middle',
      fontSize: 'larger',
      color: _renderUi.color.correct()
    }
  };
})(Token);

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