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 _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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

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

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

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

/* eslint-disable react/no-danger */
import React, { Component } from 'react';
import { injectIntl, FormattedMessage } from 'react-intl';
import Lightbox from 'react-image-lightbox';
import "./IssueDescription.less";

var QuillDeltaToHtmlConverter = require('quill-delta-to-html');

var IssueDescription = /*#__PURE__*/function (_Component) {
  _inherits(IssueDescription, _Component);

  var _super = _createSuper(IssueDescription);

  function IssueDescription(props, context) {
    var _this;

    _classCallCheck(this, IssueDescription);

    _this = _super.call(this, props, context);

    _this.onOpenLightboxChange = function (index) {
      var isOpen = _this.state.isOpen;
      isOpen[index] = !isOpen[index];

      _this.setState({
        isOpen: isOpen
      });
    };

    _this.getSubject = function (data) {
      var replyContents = [];
      var formattedMessage = _this.props.intl.formattedMessage;

      if (data) {
        JSON.parse(data).forEach(function (item, index) {
          if (item.insert && item.insert.image) {
            replyContents.push( /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("img", {
              role: "none",
              src: item.insert.image,
              style: {
                display: 'block',
                maxWidth: '600px'
              },
              alt: formattedMessage({
                id: 'branch.issue.img'
              })
            }), _this.state.isOpen["".concat(index)] ? /*#__PURE__*/React.createElement(Lightbox, {
              mainSrc: item.insert.image,
              onCloseRequest: function onCloseRequest() {
                return _this.onOpenLightboxChange("".concat(index));
              },
              imageTitle: "images"
            }) : ''));
          } else {
            var delta = [];
            delta.push(item);
            var converter = new QuillDeltaToHtmlConverter(delta, {}); // 去掉p标签

            var text = converter.convert();

            if (converter.convert().substring(0, 3) === '<p>') {
              text = converter.convert().substring(3, converter.convert().length - 4);
            }

            replyContents.push( /*#__PURE__*/React.createElement("span", {
              dangerouslySetInnerHTML: {
                __html: "".concat(_this.escape(text))
              }
            }));
          }
        });
      }

      return replyContents;
    };

    _this.open = function (src) {
      _this.setState({
        open: true,
        src: src
      });
    };

    _this.escape = function (str) {
      return str.replace(/<\/script/g, '<\\/script').replace(/<!--/g, '<\\!--');
    };

    _this.state = {
      isOpen: {},
      src: '',
      open: false
    };
    return _this;
  }

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

      window.addEventListener('click', function (e) {
        if (e.target.nodeName === 'IMG' && _this2.props.data && _this2.props.data.search(e.target.src) > -1) {
          e.stopPropagation();

          _this2.open(e.target.src);
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      return /*#__PURE__*/React.createElement("div", {
        className: "c7n-read-delta",
        style: {
          width: '100%'
        }
      }, /*#__PURE__*/React.createElement("div", {
        dangerouslySetInnerHTML: {
          __html: "".concat(this.escape(this.props.data))
        }
      }), this.state.open ? /*#__PURE__*/React.createElement(Lightbox, {
        mainSrc: this.state.src,
        onCloseRequest: function onCloseRequest() {
          return _this3.setState({
            open: false
          });
        },
        imageTitle: "images"
      }) : null);
    }
  }]);

  return IssueDescription;
}(Component);

export default injectIntl(IssueDescription);