<commit_before>var applyProperties = require("./applyProperties")
var patchRecursive = require('./patchOp');
var isWidget = require("./isWidget")
var VPatch = require("./isVPatch")
var render = require('./createElement');

module.exports = applyPatch

function applyPatch(vpatch, domNode) {
  var type = vpatch.type
  var vNode = vpatch.vNode
  var patch = vpatch.patch

  switch (type) {
    case VPatch.REMOVE:
      return removeNode(domNode, vNode)
    case VPatch.INSERT:
      return insertNode(domNode, patch)
    case VPatch.VTEXT:
      return stringPatch(domNode, vNode, patch)
    case VPatch.WIDGET:
      return widgetPatch(domNode, vNode, patch)
    case VPatch.VNODE:
      return vNodePatch(domNode, vNode, patch)
    case VPatch.ORDER:
      reorderChildren(domNode, patch)
      return domNode
    case VPatch.PROPS:
      applyProperties(domNode, patch, vNode.properties)
      return domNode
    case VPatch.THUNK:
      return replaceRoot(domNode,
        patchRecursive(domNode, patch))
    default:
      return domNode
  }
}

function removeNode(domNode, vNode) {
  var parentNode = domNode.parentNode

  if (parentNode) {
    parentNode.removeChild(domNode)
  }

  destroyWidget(domNode, vNode);

  return null
}

function insertNode(parentNode, vNode) {
  var newNode = render(vNode)

  if (parentNode) {
    parentNode.appendChild(newNode)
  }

  return parentNode
}

function stringPatch(domNode, leftVNode, vText) {
  var newNode

  if (domNode.nodeType === 3) {
    domNode.replaceData(0, domNode.length, vText.text)
    newNode = domNode
  } else {
    var parentNode = domNode.parentNode
    newNode = render(vText)

    if (parentNode && newNode !== domNode) {
      parentNode.replaceChild(newNode, domNode)
    }
  }

  return newNode
}

function widgetPatch(domNode, leftVNode, widget) {
  var updating = updateWidget(leftVNode, widget)
  var newNode

  if (updating) {
    newNode = widget.update(leftVNode, domNode) || domNode
  } else {
    newNode = render(widget)
  }

  var parentNode = domNode.parentNode

  if (parentNode && newNode !== domNode) {
    parentNode.replaceChild(newNode, domNode)
  }

  if (!updating) {
    destroyWidget(domNode, leftVNode)
  }

  return newNode
}

function vNodePatch(domNode, leftVNode, vNode) {
  var parentNode = domNode.parentNode
  var newNode = render(vNode)

  if (parentNode && newNode !== domNode) {
    parentNode.replaceChild(newNode, domNode)
  }

  return newNode
}

function destroyWidget(domNode, w) {
  if (typeof w.destroy === "function" && isWidget(w)) {
    w.destroy(domNode)
  }
}

function reorderChildren(domNode, moves) {
  var childNodes = domNode.childNodes
  var keyMap = {}
  var node<commit_msg>remove widget stuff<commit_after>var applyProperties = require("./applyProperties")
var patchRecursive = require('./patchOp');
var isWidget = require("./isWidget")
var VPatch = require("./isVPatch")
var render = require('./createElement');

module.exports = applyPatch

function applyPatch(vpatch, domNode) {
  var type = vpatch.type
  var vNode = vpatch.vNode
  var patch = vpatch.patch

  switch (type) {
    case VPatch.REMOVE:
      return removeNode(domNode, vNode)
    case VPatch.INSERT:
      return insertNode(domNode, patch)
    case VPatch.VTEXT:
      return stringPatch(domNode, vNode, patch)
    case VPatch.VNODE:
      return vNodePatch(domNode, vNode, patch)
    case VPatch.ORDER:
      reorderChildren(domNode, patch)
      return domNode
    case VPatch.PROPS:
      applyProperties(domNode, patch, vNode.properties)
      return domNode
    case VPatch.THUNK:
      return replaceRoot(domNode,
        patchRecursive(domNode, patch))
    default:
      return domNode
  }
}

function removeNode(domNode, vNode) {
  var parentNode = domNode.parentNode

  if (parentNode) {
    parentNode.removeChild(domNode)
  }

  return null
}

function insertNode(parentNode, vNode) {
  var newNode = render(vNode)

  if (parentNode) {
    parentNode.appendChild(newNode)
  }

  return parentNode
}

function stringPatch(domNode, leftVNode, vText) {
  var newNode

  if (domNode.nodeType === 3) {
    domNode.replaceData(0, domNode.length, vText.text)
    newNode = domNode
  } else {
    var parentNode = domNode.parentNode
    newNode = render(vText)

    if (parentNode && newNode !== domNode) {
      parentNode.replaceChild(newNode, domNode)
    }
  }

  return newNode
}

function vNodePatch(domNode, leftVNode, vNode) {
  var parentNode = domNode.parentNode
  var newNode = render(vNode)

  if (parentNode && newNode !== domNode) {
    parentNode.replaceChild(newNode, domNode)
  }

  return newNode
}

function reorderChildren(domNode, moves) {
  var childNodes = domNode.childNodes
  var keyMap = {}
  var node
{'fraction_non_alphanumeric': 0.10344827586206896, 'fraction_numerical': 0.0008462026655383964, 'mean_word_length': 3.5417867435158503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4471336', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1531, 'n_words': 358}