"use strict";

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

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

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

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

var defaultStyle = {
  zIndex: '1',
  position: 'absolute',
  pointerEvents: 'none',
  color: '#a0a7b4',
  backgroundColor: '#29323c',
  padding: '10px',
  top: '0',
  left: '0',
  display: 'none'
};

var Tooltip = function () {
  function Tooltip() {
    (0, _classCallCheck2.default)(this, Tooltip);
    this.id = 'default-tooltip';
    this.placement = 'fill';
    this.props = {};
    this.isVisible = false;
    this.deck = void 0;
    this.element = void 0;
    this.lastViewport = void 0;
  }

  var _proto = Tooltip.prototype;

  _proto.onAdd = function onAdd(_ref) {
    var deck = _ref.deck;
    var el = document.createElement('div');
    el.className = 'deck-tooltip';
    Object.assign(el.style, defaultStyle);
    this.deck = deck;
    this.element = el;
    return el;
  };

  _proto.onRemove = function onRemove() {
    this.deck = undefined;
    this.element = undefined;
  };

  _proto.setProps = function setProps() {};

  _proto.onViewportChange = function onViewportChange(viewport) {
    var _this$lastViewport;

    if (this.isVisible && viewport.id === ((_this$lastViewport = this.lastViewport) === null || _this$lastViewport === void 0 ? void 0 : _this$lastViewport.id) && viewport !== this.lastViewport) {
      this.setTooltip(null);
    }
  };

  _proto.onHover = function onHover(info) {
    var deck = this.deck;
    var getTooltip = deck && deck.props.getTooltip;

    if (!getTooltip) {
      return;
    }

    var displayInfo = getTooltip(info);
    this.lastViewport = info.viewport;
    this.setTooltip(displayInfo, info.x, info.y);
  };

  _proto.setTooltip = function setTooltip(displayInfo, x, y) {
    var el = this.element;

    if (!el) {
      return;
    }

    if (typeof displayInfo === 'string') {
      el.innerText = displayInfo;
    } else if (!displayInfo) {
      this.isVisible = false;
      el.style.display = 'none';
      return;
    } else {
      if (displayInfo.text) {
        el.innerText = displayInfo.text;
      }

      if (displayInfo.html) {
        el.innerHTML = displayInfo.html;
      }

      if (displayInfo.className) {
        el.className = displayInfo.className;
      }
    }

    this.isVisible = true;
    el.style.display = 'block';
    el.style.transform = "translate(".concat(x, "px, ").concat(y, "px)");

    if (displayInfo && (0, _typeof2.default)(displayInfo) === 'object' && 'style' in displayInfo) {
      Object.assign(el.style, displayInfo.style);
    }
  };

  return Tooltip;
}();

exports.default = Tooltip;
//# sourceMappingURL=tooltip.js.map