'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Annotation = exports.AnnotationBlock = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _Common = {
  "inner-block": "Common__inner-block___2cOrF",
  "disable-inner-block": "Common__disable-inner-block___1MLd0",
  "components": "Common__components___2fQuj",
  "component": "Common__component___3bwMg",
  "hide": "Common__hide___2HyoH",
  "desc-text-color": "Common__desc-text-color___2fV3l",
  "desc-text-block": "Common__desc-text-block___1Z4b-",
  "text-link": "Common__text-link___1jaLy",
  "image": "Common__image___AKB6W",
  "imagediff": "Common__imagediff___2j7tX",
  "slideshow": "Common__slideshow___1Fz23",
  "embeddedcode": "Common__embeddedcode___3Avmp",
  "youtube": "Common__youtube___2oq1n",
  "topic-box": "Common__topic-box___2Q-kN"
};

var _Common2 = _interopRequireDefault(_Common);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _Annotation = {
  "block": "Annotation__block___XxxXG",
  "container": "Annotation__container___SXG0I",
  "annotated-text": "Annotation__annotated-text___3wUrQ",
  "indicator": "Annotation__indicator___2SIZf",
  "body": "Annotation__body___2q6Em",
  "expand": "Annotation__expand___2vHXG"
};

var _Annotation2 = _interopRequireDefault(_Annotation);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

// eslint-disable-next-line

var AnnotationBlock = exports.AnnotationBlock = function (_React$Component) {
  _inherits(AnnotationBlock, _React$Component);

  function AnnotationBlock(props) {
    _classCallCheck(this, AnnotationBlock);

    var _this = _possibleConstructorReturn(this, (AnnotationBlock.__proto__ || Object.getPrototypeOf(AnnotationBlock)).call(this, props));

    _this.state = {
      isExpanded: false
    };
    _this.handleExpand = _this._handleExpand.bind(_this);
    return _this;
  }

  _createClass(AnnotationBlock, [{
    key: '_handleExpand',
    value: function _handleExpand(e) {
      e.stopPropagation();
      this.setState({
        isExpanded: !this.state.isExpanded
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props;
      var annotation = _props.annotation;
      var pureAnnotationText = _props.pureAnnotationText;
      var text = _props.text;
      var isExpanded = this.state.isExpanded;

      return _react2.default.createElement(
        'abbr',
        {
          className: (0, _classnames2.default)(_defineProperty({}, _Annotation2.default.expand, isExpanded), _Annotation2.default['container']),
          title: pureAnnotationText
        },
        _react2.default.createElement(
          'span',
          {
            className: (0, _classnames2.default)(_Annotation2.default['annotated-text']),
            onClick: this.handleExpand
          },
          text,
          _react2.default.createElement('span', {
            className: (0, _classnames2.default)(_Annotation2.default['indicator'])
          })
        ),
        _react2.default.createElement('div', {
          dangerouslySetInnerHTML: { __html: annotation },
          className: (0, _classnames2.default)(_Annotation2.default['body'], _Common2.default['disable-inner-block'], _Common2.default['desc-text-color'], 'text-justify')
        })
      );
    }
  }]);

  return AnnotationBlock;
}(_react2.default.Component);

AnnotationBlock.propTypes = {
  annotation: _react2.default.PropTypes.string,
  device: _react2.default.PropTypes.string,
  pureAnnotationText: _react2.default.PropTypes.string,
  text: _react2.default.PropTypes.string
};

AnnotationBlock.defaultProps = {
  annotation: '',
  device: '',
  pureAnnotationText: '',
  text: ''
};

var Annotation = exports.Annotation = function Annotation(_ref) {
  var content = _ref.content;

  var html = _lodash2.default.get(content, 0, '');

  // annotation data will be in the comment with prefix __ANNOTATION__=
  var re = /<!--__ANNOTATION__=(.+?)-->/g;

  var sections = [];
  var result = void 0;
  var lastIndex = 0;
  do {
    result = re.exec(html);
    if (result) {
      try {
        var inlineText = html.substring(lastIndex, result.index);
        var annotationObj = JSON.parse(result[1]);
        sections.push(_react2.default.createElement('span', { key: sections.length,
          dangerouslySetInnerHTML: { __html: inlineText }
        }), _react2.default.createElement(AnnotationBlock, _extends({
          key: 'annotation ' + sections.length
        }, annotationObj)));
        html = html.replace(inlineText, '');
        html = html.replace(result[0], '');
        lastIndex = result.index;
      } catch (e) {
        console.warn('Can not JSON parse annotation obj string', _lodash2.default.get(result, 1)); // eslint-disable-line
      }
    }
  } while (result);

  if (html) {
    sections.push(_react2.default.createElement('span', { key: sections.length,
      dangerouslySetInnerHTML: { __html: html }
    }));
  }

  return _react2.default.createElement(
    'div',
    { className: (0, _classnames2.default)(_Annotation2.default['block'], _Common2.default['inner-block'], 'text-justify') },
    sections
  );
};