"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _common = require("../utils/common");
const {
  useEffect
} = require('react');
const {
  EXTERNAL_EVENTS
} = require('../constants/event-types');
const {
  default: EventBus
} = require('../utils/event-bus');
const useLinkClick = (editorId, server, callback) => {
  useEffect(() => {
    function onLinkClick(event, sourceEditorId) {
      event.preventDefault();
      event.stopPropagation();
      let link = '';
      let target = event.target;
      while (!target.dataset || !target.dataset.url) {
        target = target.parentNode;
      }
      if (!target) return;
      link = target.dataset.url;
      if (editorId !== sourceEditorId) return;
      let isValid = true;
      if (!(0, _common.isUrl)(link)) {
        isValid = false;
        const validServer = server || window.location.origin;
        if (link.startsWith('/') && (0, _common.isUrl)(validServer + link)) {
          isValid = true;
        }
        if (!link.startsWith('/') && (link.startsWith('mailto:') || link.startsWith('tel:'))) {
          isValid = true;
        }
      }
      if (!isValid) return;
      if (callback) {
        callback(link);
        return;
      }
      window.open(link);
    }
    const eventBus = EventBus.getInstance();
    const unsubscribe = eventBus.subscribe(EXTERNAL_EVENTS.ON_LINK_CLICK, onLinkClick);
    return unsubscribe;
  }, [editorId, server, callback]);
};
var _default = exports.default = useLinkClick;