import { getUrlForDomainInContext } from '@atlaskit/atlassian-context';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
import { linkPreferencesPath } from './constants';
const STAGING = 'staging';
const PRODUCTION = 'prod';
const DEV = 'dev';
export function isTextAtPos(pos) {
  return ({
    tr
  }) => {
    const node = tr.doc.nodeAt(pos);
    return !!node && node.isText;
  };
}
export function isLinkAtPos(pos) {
  return state => {
    const node = state.doc.nodeAt(pos);
    return !!node && !!state.schema.marks.link.isInSet(node.marks);
  };
}
export const getLinkPreferencesURLFromENV = () => {
  const envType = process.env.CLOUD_ENV === 'staging' ? STAGING : PRODUCTION;
  return getUrlForDomainInContext('id', envType) + linkPreferencesPath;
};
const isSelectionInsideLink = state => !!state.doc.type.schema.marks.link.isInSet(state.selection.$from.marks());
const isEmptySelectionBeforeLink = state => {
  var _$from$nodeAfter;
  const {
    $from,
    $to
  } = state.selection;
  return $from === $to && !!state.doc.type.schema.marks.link.isInSet(((_$from$nodeAfter = $from.nodeAfter) === null || _$from$nodeAfter === void 0 ? void 0 : _$from$nodeAfter.marks) || []);
};
const isEmptySelectionAfterLink = state => {
  var _$from$nodeBefore;
  const {
    $from,
    $to
  } = state.selection;
  return $from === $to && !!state.doc.type.schema.marks.link.isInSet(((_$from$nodeBefore = $from.nodeBefore) === null || _$from$nodeBefore === void 0 ? void 0 : _$from$nodeBefore.marks) || []);
};
const isSelectionAroundLink = state => {
  const {
    $from,
    $to
  } = state.selection;
  const node = $from.nodeAfter;
  return !!node && $from.textOffset === 0 && $to.pos - $from.pos === node.nodeSize && !!state.doc.type.schema.marks.link.isInSet(node.marks);
};
export const getActiveLinkMark = state => {
  const {
    selection: {
      $from
    }
  } = state;
  if (isSelectionInsideLink(state) || isSelectionAroundLink(state)) {
    const pos = $from.pos - $from.textOffset;
    const node = state.doc.nodeAt(pos);
    return node && node.isText ? {
      node,
      pos
    } : undefined;
  }
  if (isEmptySelectionBeforeLink(state) && 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
    const node = state.doc.nodeAt($from.pos);
    return node && node.isText && node.nodeSize === 1 ? {
      node,
      pos: $from.pos
    } : undefined;
  }
  if (isEmptySelectionAfterLink(state) && 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
    const node = state.doc.nodeAt($from.pos - 1);
    return node && node.isText && node.nodeSize === 1 ? {
      node,
      pos: $from.pos - 1
    } : undefined;
  }
  return undefined;
};