"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 _components = require("../../../../components");
require("./index.css");
/* eslint-disable react-hooks/rules-of-hooks */

const renderDefinition = (_ref, editor) => {
  let {
    attributes,
    children,
    element,
    option
  } = _ref;
  const [isShowConfirmDialog, setIsShowConfirmDialog] = (0, _react.useState)(false);
  const {
    render
  } = option || {};
  const onHrefClick = (0, _react.useCallback)(event => {
    event.preventDefault();
    if (element.url.startsWith(window.location.origin)) {
      window.open(element.url);
      return;
    }
    setIsShowConfirmDialog(true);
  }, [element]);
  const onToggle = (0, _react.useCallback)(() => {
    setIsShowConfirmDialog(!isShowConfirmDialog);
  }, [isShowConfirmDialog]);
  if ( /*#__PURE__*/(0, _react.isValidElement)(render)) {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/(0, _react.cloneElement)(render, {
      element,
      onClick: onHrefClick,
      attributes,
      editor
    }), isShowConfirmDialog && /*#__PURE__*/_react.default.createElement(_components.LinkVerifiedDialog, {
      link: element.url,
      onToggle: onToggle
    }));
  }
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("p", Object.assign({
    "data-url": element.url,
    "data-id": element.id,
    className: "sf-virtual-definition"
  }, attributes), /*#__PURE__*/_react.default.createElement("span", null, element.identifier + '. '), /*#__PURE__*/_react.default.createElement("a", {
    href: element.url,
    "data-url": element.url,
    onClick: onHrefClick
  }, children)), isShowConfirmDialog && /*#__PURE__*/_react.default.createElement(_components.LinkVerifiedDialog, {
    link: element.url,
    onToggle: onToggle
  }));
};
var _default = exports.default = renderDefinition;