"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _core = require("../../../core");
var _elementTypes = require("../../../constants/element-types");
require("./index.css");
/* eslint-disable react-hooks/rules-of-hooks */

const renderLinkReference = (_ref, editor) => {
  let {
    attributes,
    children,
    element,
    option
  } = _ref;
  const {
    render
  } = option || {};
  const onClick = (0, _react.useCallback)(() => {
    const doms = (0, _core.getNodesByType)(editor.children, _elementTypes.DEFINITION);
    const dom = doms.find(d => d.identifier === element.identifier);
    if (!dom) return;
    const selector = "[data-id=\"".concat(dom.id, "\"]");
    const definitionDom = document.querySelector(selector);
    if (!definitionDom) return;
    definitionDom.scrollIntoView({
      behavior: 'smooth',
      block: 'center'
    });
  }, [element, editor]);
  if (render && /*#__PURE__*/(0, _react.isValidElement)(render)) {
    return /*#__PURE__*/(0, _react.cloneElement)(render, {
      element,
      onClick,
      attributes,
      editor
    });
  }
  return /*#__PURE__*/_react.default.createElement("sup", Object.assign({
    onClick: onClick,
    className: "sf-virtual-link-reference",
    "data-id": element.id
  }, attributes, {
    title: element.label
  }), "[".concat(element.identifier, "]"));
};
var _default = exports.default = renderLinkReference;