"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = 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 _reactKonva = require("react-konva");

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 ImageComponent = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(ImageComponent, _React$Component);

  var _super = _createSuper(ImageComponent);

  function ImageComponent(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, ImageComponent);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleLoad", function () {
      _this.setState({
        image: _this.image
      });
    });
    _this.state = {
      image: null,
      showTooltip: false
    };
    return _this;
  }

  (0, _createClass2["default"])(ImageComponent, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.loadImage();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(oldProps) {
      if (oldProps.src !== this.props.src) {
        this.loadImage();
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.image.removeEventListener('load', this.handleLoad);
    }
  }, {
    key: "loadImage",
    value: function loadImage() {
      var src = this.props.src;
      this.image = new window.Image();
      this.image.src = src;
      this.image.addEventListener('load', this.handleLoad);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          x = _this$props.x,
          y = _this$props.y,
          tooltip = _this$props.tooltip;
      var _this$state = this.state,
          image = _this$state.image,
          showTooltip = _this$state.showTooltip;
      return /*#__PURE__*/_react["default"].createElement(_reactKonva.Group, null, /*#__PURE__*/_react["default"].createElement(_reactKonva.Image, {
        width: 20,
        height: 20,
        x: x,
        y: y,
        image: image,
        onMouseEnter: function onMouseEnter() {
          return _this2.setState({
            showTooltip: true
          });
        },
        onMouseLeave: function onMouseLeave() {
          return _this2.setState({
            showTooltip: false
          });
        }
      }), showTooltip && tooltip && /*#__PURE__*/_react["default"].createElement(_reactKonva.Label, {
        x: x - 30,
        y: y + 25
      }, /*#__PURE__*/_react["default"].createElement(_reactKonva.Tag, {
        fill: "white",
        cornerRadius: 5,
        opacity: 0.9
      }), /*#__PURE__*/_react["default"].createElement(_reactKonva.Text, {
        text: tooltip,
        padding: 5
      })));
    }
  }]);
  return ImageComponent;
}(_react["default"].Component);

ImageComponent.propTypes = {
  src: _propTypes["default"].string.isRequired,
  x: _propTypes["default"].number.isRequired,
  y: _propTypes["default"].number.isRequired,
  tooltip: _propTypes["default"].string
};
var _default = ImageComponent;
exports["default"] = _default;
//# sourceMappingURL=image-konva.js.map