"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getLinkPreferencesURLFromENV = exports.getActiveLinkMark = void 0;
exports.isLinkAtPos = isLinkAtPos;
exports.isTextAtPos = isTextAtPos;
var _atlassianContext = require("@atlaskit/atlassian-context");
var _experiments = require("@atlaskit/tmp-editor-statsig/experiments");
var _constants = require("./constants");
var STAGING = 'staging';
var PRODUCTION = 'prod';
var DEV = 'dev';
function isTextAtPos(pos) {
  return function (_ref) {
    var tr = _ref.tr;
    var node = tr.doc.nodeAt(pos);
    return !!node && node.isText;
  };
}
function isLinkAtPos(pos) {
  return function (state) {
    var node = state.doc.nodeAt(pos);
    return !!node && !!state.schema.marks.link.isInSet(node.marks);
  };
}
var getLinkPreferencesURLFromENV = exports.getLinkPreferencesURLFromENV = function getLinkPreferencesURLFromENV() {
  var envType = process.env.CLOUD_ENV === 'staging' ? STAGING : PRODUCTION;
  return (0, _atlassianContext.getUrlForDomainInContext)('id', envType) + _constants.linkPreferencesPath;
};
var isSelectionInsideLink = function isSelectionInsideLink(state) {
  return !!state.doc.type.schema.marks.link.isInSet(state.selection.$from.marks());
};
var isEmptySelectionBeforeLink = function isEmptySelectionBeforeLink(state) {
  var _$from$nodeAfter;
  var _state$selection = state.selection,
    $from = _state$selection.$from,
    $to = _state$selection.$to;
  return $from === $to && !!state.doc.type.schema.marks.link.isInSet(((_$from$nodeAfter = $from.nodeAfter) === null || _$from$nodeAfter === void 0 ? void 0 : _$from$nodeAfter.marks) || []);
};
var isEmptySelectionAfterLink = function isEmptySelectionAfterLink(state) {
  var _$from$nodeBefore;
  var _state$selection2 = state.selection,
    $from = _state$selection2.$from,
    $to = _state$selection2.$to;
  return $from === $to && !!state.doc.type.schema.marks.link.isInSet(((_$from$nodeBefore = $from.nodeBefore) === null || _$from$nodeBefore === void 0 ? void 0 : _$from$nodeBefore.marks) || []);
};
var isSelectionAroundLink = function isSelectionAroundLink(state) {
  var _state$selection3 = state.selection,
    $from = _state$selection3.$from,
    $to = _state$selection3.$to;
  var node = $from.nodeAfter;
  return !!node && $from.textOffset === 0 && $to.pos - $from.pos === node.nodeSize && !!state.doc.type.schema.marks.link.isInSet(node.marks);
};
var getActiveLinkMark = exports.getActiveLinkMark = function getActiveLinkMark(state) {
  var $from = state.selection.$from;
  if (isSelectionInsideLink(state) || isSelectionAroundLink(state)) {
    var pos = $from.pos - $from.textOffset;
    var node = state.doc.nodeAt(pos);
    return node && node.isText ? {
      node: node,
      pos: pos
    } : undefined;
  }
  if (isEmptySelectionBeforeLink(state) && (0, _experiments.editorExperiment)('platform_editor_controls', 'variant1')) {
    // if user clicks right before the link, we want to show the toolbar for the link
    // but only if the link is a single character
    var _node = state.doc.nodeAt($from.pos);
    return _node && _node.isText && _node.nodeSize === 1 ? {
      node: _node,
      pos: $from.pos
    } : undefined;
  }
  if (isEmptySelectionAfterLink(state) && (0, _experiments.editorExperiment)('platform_editor_controls', 'variant1')) {
    // if user clicks right after the link, we want to show the toolbar for the link
    // but only if the link is a single character
    // and we return the position of the link
    var _node2 = state.doc.nodeAt($from.pos - 1);
    return _node2 && _node2.isText && _node2.nodeSize === 1 ? {
      node: _node2,
      pos: $from.pos - 1
    } : undefined;
  }
  return undefined;
};