import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
export var defaultWordWrapState = false;
export var codeBlockWrappedStates = new WeakMap();

// Code folding state management - similar to word wrapping

var codeBlockFoldStates = new WeakMap();
export var isCodeBlockWordWrapEnabled = function isCodeBlockWordWrapEnabled(codeBlockNode) {
  var currentNodeWordWrapState = codeBlockWrappedStates.get(codeBlockNode);
  return currentNodeWordWrapState !== undefined ? currentNodeWordWrapState : defaultWordWrapState;
};
export var getCodeBlockFoldState = function getCodeBlockFoldState(codeBlockNode) {
  var currentNodeFoldState = codeBlockFoldStates.get(codeBlockNode);
  return currentNodeFoldState || [];
};
export var setCodeBlockFoldState = function setCodeBlockFoldState(codeBlockNode, foldRanges) {
  codeBlockFoldStates.set(codeBlockNode, foldRanges);
};

/**
 * Swap the old node key with the new node key in the wrapped states WeakMap.
 */
export var transferCodeBlockWrappedValue = function transferCodeBlockWrappedValue(oldCodeBlockNode, newCodeBlockNode) {
  if (expValEquals('platform_editor_code_block_fold_gutter', 'isEnabled', true)) {
    transferCodeBlockFoldValue(oldCodeBlockNode, newCodeBlockNode);
  }

  // Don't overwrite the value for the new node if it already exists.
  // This can happen when a drag&drop is swapping nodes.
  if (codeBlockWrappedStates.has(newCodeBlockNode)) {
    return;
  }
  var previousValue = isCodeBlockWordWrapEnabled(oldCodeBlockNode);
  codeBlockWrappedStates.set(newCodeBlockNode, previousValue);
  codeBlockWrappedStates.delete(oldCodeBlockNode);
};

/**
 * Swap the old node key with the new node key in the fold states WeakMap.
 */
var transferCodeBlockFoldValue = function transferCodeBlockFoldValue(oldCodeBlockNode, newCodeBlockNode) {
  // Don't overwrite the value for the new node if it already exists.
  // This can happen when a drag&drop is swapping nodes.
  if (codeBlockFoldStates.has(newCodeBlockNode)) {
    return;
  }
  var previousValue = getCodeBlockFoldState(oldCodeBlockNode);
  codeBlockFoldStates.set(newCodeBlockNode, previousValue);
  codeBlockFoldStates.delete(oldCodeBlockNode);
};

/**
 * As the code block node is used as the wrapped state key, there is instances where the node will be destroyed & recreated and is no longer a valid key.
 * In these instances, we must get the value from that old node and set it to the value of the new node.
 * This function takes all the given nodes, finds their old nodes from the old state and updates these old node keys.
 */
export var updateCodeBlockWrappedStateNodeKeys = function updateCodeBlockWrappedStateNodeKeys(newCodeBlockNodes, oldState) {
  newCodeBlockNodes.forEach(function (newCodeBlockNode) {
    if (expValEquals('platform_editor_code_block_fold_gutter', 'isEnabled', true)) {
      updateCodeBlockFoldStateNodeKeys(newCodeBlockNode, oldState);
    }
    // Don't overwrite the value for the new node if it already exists.
    // This can happen when a drag&drop is swapping nodes.
    if (codeBlockWrappedStates.has(newCodeBlockNode.node)) {
      return;
    }

    // Do not go out of range on the oldState doc. Happens on initial load.
    if (oldState.doc.content.size <= newCodeBlockNode.pos) {
      return;
    }
    var oldCodeBlockNode = oldState.doc.nodeAt(newCodeBlockNode.pos);
    if (!oldCodeBlockNode || oldCodeBlockNode.type !== oldState.schema.nodes.codeBlock) {
      return;
    }
    var previousValue = isCodeBlockWordWrapEnabled(oldCodeBlockNode);
    codeBlockWrappedStates.set(newCodeBlockNode.node, previousValue);
  });
};

/**
 * As the code block node is used as the fold state key, there is instances where the node will be destroyed & recreated and is no longer a valid key.
 * In these instances, we must get the value from that old node and set it to the value of the new node.
 * This function takes all the given nodes, finds their old nodes from the old state and updates these old node keys.
 */
var updateCodeBlockFoldStateNodeKeys = function updateCodeBlockFoldStateNodeKeys(newCodeBlockNode, oldState) {
  // Don't overwrite the value for the new node if it already exists.
  // This can happen when a drag&drop is swapping nodes.
  if (codeBlockFoldStates.has(newCodeBlockNode.node)) {
    return;
  }

  // Do not go out of range on the oldState doc. Happens on initial load.
  if (oldState.doc.content.size <= newCodeBlockNode.pos) {
    return;
  }
  var oldCodeBlockNode = oldState.doc.nodeAt(newCodeBlockNode.pos);
  if (!oldCodeBlockNode || oldCodeBlockNode.type !== oldState.schema.nodes.codeBlock) {
    return;
  }
  var previousValue = getCodeBlockFoldState(oldCodeBlockNode);
  codeBlockFoldStates.set(newCodeBlockNode.node, previousValue);
};