"use strict";

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

var React = _interopRequireWildcard(require("react"));

var _reactPdf = require("react-pdf");

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

var _Loading = _interopRequireDefault(require("./Loading"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(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 _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 _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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

var ViewerCanvas =
/*#__PURE__*/
function (_React$Component) {
  _inherits(ViewerCanvas, _React$Component);

  function ViewerCanvas() {
    var _this;

    _classCallCheck(this, ViewerCanvas);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(ViewerCanvas).call(this));

    _this.handleResize = function (e) {
      _this.props.onResize();
    };

    _this.handleCanvasMouseDown = function (e) {
      _this.props.onCanvasMouseDown(e);

      _this.handleMouseDown(e);
    };

    _this.handleMouseDown = function (e) {
      if (!_this.props.visible || !_this.props.drag) {
        return;
      }

      e.preventDefault();
      e.stopPropagation();

      _this.setState({
        isMouseDown: true,
        mouseX: e.nativeEvent.clientX,
        mouseY: e.nativeEvent.clientY
      });
    };

    _this.handleMouseMove = function (e) {
      if (_this.state.isMouseDown) {
        var diffX = e.clientX - _this.state.mouseX;
        var diffY = e.clientY - _this.state.mouseY;

        _this.setState({
          mouseX: e.clientX,
          mouseY: e.clientY
        });

        _this.props.onChangeImgState(_this.props.width, _this.props.height, _this.props.top + diffY, _this.props.left + diffX);
      }
    };

    _this.handleMouseUp = function (e) {
      _this.setState({
        isMouseDown: false
      });
    };

    _this.bindEvent = function (remove) {
      var funcName = 'addEventListener';

      if (remove) {
        funcName = 'removeEventListener';
      }

      document[funcName]('click', _this.handleMouseUp, false);
      document[funcName]('mousemove', _this.handleMouseMove, false);
      window[funcName]('resize', _this.handleResize, false);
    };

    _this.refPage = function (ref) {
      _this.page = ref;
    };

    _this.state = {
      isMouseDown: false,
      mouseX: 0,
      mouseY: 0
    };
    return _this;
  }

  _createClass(ViewerCanvas, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.drag) {
        this.bindEvent();
      }
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (!this.props.visible && nextProps.visible) {
        if (nextProps.drag) {
          return this.bindEvent();
        }
      }

      if (this.props.visible && !nextProps.visible) {
        this.handleMouseUp({});

        if (nextProps.drag) {
          return this.bindEvent(true);
        }
      }

      if (this.props.drag && !nextProps.drag) {
        return this.bindEvent(true);
      }

      if (!this.props.drag && nextProps.drag) {
        if (nextProps.visible) {
          return this.bindEvent(true);
        }
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.bindEvent(true);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var src = this.props.activeFile.src;
      var imgStyle = {
        width: "".concat(this.props.width, "px"),
        height: "".concat(this.props.height, "px"),
        transform: "translateX(".concat(this.props.left !== null ? this.props.left + 'px' : 'aoto', ") translateY(").concat(this.props.top + 44, "px)\n      rotate(").concat(this.props.rotate, "deg) scaleX(").concat(this.props.scaleX, ") scaleY(").concat(this.props.scaleY, ")"),
        visibility: this.props.loading && !this.page ? 'hidden' : 'visible'
      };
      var imgClass = (0, _classnames2["default"])("".concat(this.props.prefixCls, "-image"), _defineProperty({
        drag: this.props.drag
      }, "".concat(this.props.prefixCls, "-image-transition"), !this.state.isMouseDown));
      var style = {
        zIndex: this.props.zIndex
      };
      var fileNode = React.createElement("div", {
        className: imgClass,
        style: imgStyle,
        onMouseDown: this.handleMouseDown
      }, this.props.isImage ? React.createElement("img", {
        src: src,
        ref: this.refPage,
        alt: ""
      }) : React.createElement(_reactPdf.Document, {
        file: src,
        noData: "\u626B\u7801\u8BC6\u522B\uFF0C\u65E0\u53D1\u7968\u5F71\u50CF",
        options: {
          cMapUrl: 'cmaps/',
          cMapPacked: true,
          fontFamily: 'Microsoft YaHei'
        }
      }, React.createElement(_reactPdf.Page, {
        pageNumber: 1,
        width: this.props.width,
        inputRef: this.refPage,
        onRenderSuccess: function onRenderSuccess() {
          _this2.props.onRenderSuccess();
        }
      })));
      return React.createElement("div", {
        className: "".concat(this.props.prefixCls, "-canvas"),
        onMouseDown: this.handleCanvasMouseDown,
        style: style
      }, this.props.loading && React.createElement(_Loading["default"], {
        prefixCls: this.props.prefixCls
      }), fileNode);
    }
  }]);

  return ViewerCanvas;
}(React.Component);

exports["default"] = ViewerCanvas;