"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _balloon = _interopRequireDefault(require("@alifd/next/lib/balloon"));
var _react = _interopRequireWildcard(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
var targetClass = 'render-wrapper-target';
var rootClass = 'render-wrapper-root done-note-wrapper';

// 获取DOM真实style的方法
var getStyle = function getStyle(obj, attr) {
  if (obj.currentStyle) {
    // 兼容IE
    return obj.currentStyle[attr];
  } else {
    return window.getComputedStyle(obj, null)[attr];
  }
};
var NoteWrapper = function NoteWrapper(props, ref) {
  var el = document.createElement('div');
  var innerRef = null;
  el.className = rootClass;
  el.id = props.id;
  el.ref = ref;
  var _renderLayer = function _renderLayer() {
    var note = props.note;
    var defaultTrigger = /*#__PURE__*/_react["default"].createElement("div", {
      className: "render-wrapper-note"
    }, "N");
    var node = /*#__PURE__*/_react["default"].createElement(_balloon["default"].Tooltip, {
      trigger: defaultTrigger,
      align: "rb",
      triggerType: "click",
      popupStyle: {
        fontSize: '12px',
        lineHeight: '1.6',
        background: '#fff'
      }
    }, note);
    _reactDom["default"].render(node, el);
  };
  (0, _react.useEffect)(function () {
    _renderLayer();
    if (innerRef) {
      var refDom = _reactDom["default"].findDOMNode(innerRef);
      if (refDom.className.indexOf(targetClass) === -1) {
        if (getStyle(refDom, 'position') === 'static') {
          refDom.style.position = 'relative'; // 当target目标组件的position为static时, 则使用relative, 方便内层元素定位
        }

        refDom.className += " " + targetClass;
        refDom.appendChild(el);
      }
    }
  });
  var children = props.children;
  var _children = _react["default"].Children.map(children, function (child) {
    var c = child;
    if (typeof child === 'string') c = /*#__PURE__*/_react["default"].createElement("span", null, child);
    return /*#__PURE__*/_react["default"].cloneElement(c, {
      ref: function ref(_ref) {
        return innerRef = _ref;
      }
    });
  });
  return _children || /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null);
};
var RefNoteWrapper = /*#__PURE__*/_react["default"].forwardRef(NoteWrapper);
RefNoteWrapper.displayName = 'Wrapper';
var _default = RefNoteWrapper;
exports["default"] = _default;