_G.LIDCOUNCIL_CreateCoreNestedPatchHelpers_Impl = function(deps)
  deps = type(deps) == "table" and deps or {}

  local TrimText = deps.TrimText
  local DeepCopy = deps.DeepCopy
  local CalculateStableChecksum = deps.CalculateStableChecksum
  local BuildStrictIdentityMap = deps.BuildStrictIdentityMap
  local BuildPatchKeyedChanges = deps.BuildPatchKeyedChanges
  local BuildPatchScalarSource = deps.BuildPatchScalarSource
  local ApplyPatchScalarSource = deps.ApplyPatchScalarSource
  local SortPatchKeys = deps.SortPatchKeys

  local function trim(value)
    if type(TrimText) == "function" then
      return TrimText(value)
    end
    local text = tostring(value or "")
    text = text:gsub("^%s+", "")
    text = text:gsub("%s+$", "")
    return text
  end

  local function copy(value)
    if type(DeepCopy) == "function" then
      return DeepCopy(value)
    end
    return value
  end

  local function BuildExistingIdentityState(entries, identityKey)
    local byIdentity = {}
    local order = {}
    for _, entry in ipairs(type(entries) == "table" and entries or {}) do
      local identity = trim(type(entry) == "table" and entry[identityKey] or "")
      if identity ~= "" and byIdentity[identity] == nil then
        byIdentity[identity] = entry
        order[#order + 1] = identity
      end
    end
    return byIdentity, order
  end

  local function BuildSequentialIdentityPatchManifest(kind, entries, identityKey)
    local byIdentity, order = BuildStrictIdentityMap(entries, identityKey)
    if not byIdentity then
      return nil
    end
    local entryChecksums = {}
    for identity, entry in pairs(byIdentity) do
      entryChecksums[identity] = CalculateStableChecksum(entry)
    end
    return {
      kind = kind,
      entryChecksums = entryChecksums,
      orderChecksum = CalculateStableChecksum(order),
    }
  end

  local function BuildSequentialIdentityPatchPayload(kind, entries, identityKey, remoteDetail)
    remoteDetail = type(remoteDetail) == "table" and remoteDetail or {}
    if type(remoteDetail.entryChecksums) ~= "table" then
      return nil
    end

    local byIdentity, order = BuildStrictIdentityMap(entries, identityKey)
    if not byIdentity then
      return nil
    end

    local changedEntries, removedEntries = BuildPatchKeyedChanges(byIdentity, remoteDetail.entryChecksums)
    local patch = {
      kind = kind,
    }
    if next(changedEntries) ~= nil then
      patch.changedEntries = changedEntries
    end
    if #removedEntries > 0 then
      patch.removedEntries = removedEntries
    end
    if trim(remoteDetail.orderChecksum) ~= CalculateStableChecksum(order) then
      patch.order = copy(order)
    end
    if patch.changedEntries == nil and patch.removedEntries == nil and patch.order == nil then
      return nil
    end
    return patch
  end

  local function ApplySequentialIdentityPatch(existingList, patch, identityKey)
    patch = type(patch) == "table" and patch or {}
    local currentByIdentity, currentOrder = BuildExistingIdentityState(existingList, identityKey)

    for _, identity in ipairs(type(patch.removedEntries) == "table" and patch.removedEntries or {}) do
      currentByIdentity[identity] = nil
    end
    for identity, entry in pairs(type(patch.changedEntries) == "table" and patch.changedEntries or {}) do
      currentByIdentity[identity] = copy(entry)
    end

    local rebuilt = {}
    local seen = {}
    local order = type(patch.order) == "table" and patch.order or currentOrder
    for _, identity in ipairs(order) do
      if currentByIdentity[identity] ~= nil then
        rebuilt[#rebuilt + 1] = currentByIdentity[identity]
        seen[identity] = true
      end
    end

    local extraIdentities = {}
    for identity in pairs(currentByIdentity) do
      if not seen[identity] then
        extraIdentities[#extraIdentities + 1] = identity
      end
    end
    if type(SortPatchKeys) == "function" then
      SortPatchKeys(extraIdentities)
    else
      table.sort(extraIdentities, function(left, right)
        return tostring(left) < tostring(right)
      end)
    end
    for _, identity in ipairs(extraIdentities) do
      rebuilt[#rebuilt + 1] = currentByIdentity[identity]
    end
    return rebuilt
  end

  local LIVE_BOARD_ITEM_OMITTED_KEYS = {
    selections = true,
    votesByVoter = true,
    voteUpdatedAtByVoter = true,
    voteSequenceByVoter = true,
  }

  local function EnsureVoteStateEntry(target, voterKey)
    local normalizedVoter = trim(voterKey)
    if normalizedVoter == "" then
      return nil, ""
    end
    local entry = type(target[normalizedVoter]) == "table" and target[normalizedVoter] or {}
    target[normalizedVoter] = entry
    return entry, normalizedVoter
  end

  local function BuildLiveBoardVoteStateMap(entry)
    local output = {}
    local votesByVoter = type(entry) == "table" and type(entry.votesByVoter) == "table" and entry.votesByVoter or {}
    local updatedAtByVoter = type(entry) == "table" and type(entry.voteUpdatedAtByVoter) == "table" and entry.voteUpdatedAtByVoter or {}
    local sequenceByVoter = type(entry) == "table" and type(entry.voteSequenceByVoter) == "table" and entry.voteSequenceByVoter or {}

    for voterKey, targetKey in pairs(votesByVoter) do
      local state = EnsureVoteStateEntry(output, voterKey)
      if state then
        local normalizedTarget = trim(targetKey)
        if normalizedTarget ~= "" then
          state.target = normalizedTarget
        end
      end
    end
    for voterKey, updatedAt in pairs(updatedAtByVoter) do
      local state = EnsureVoteStateEntry(output, voterKey)
      if state then
        local normalizedUpdatedAt = tonumber(updatedAt)
        if normalizedUpdatedAt and normalizedUpdatedAt > 0 then
          state.updatedAt = normalizedUpdatedAt
        end
      end
    end
    for voterKey, sequence in pairs(sequenceByVoter) do
      local state = EnsureVoteStateEntry(output, voterKey)
      if state then
        local normalizedSequence = tonumber(sequence)
        if normalizedSequence and normalizedSequence > 0 then
          state.sequence = math.floor(normalizedSequence)
        end
      end
    end

    for voterKey, state in pairs(output) do
      if next(state) == nil then
        output[voterKey] = nil
      end
    end
    return output
  end

  local function BuildLiveBoardItemVoteChecksums(entry)
    local voteChecksums = {}
    for voterKey, voteState in pairs(BuildLiveBoardVoteStateMap(entry)) do
      voteChecksums[voterKey] = CalculateStableChecksum(voteState)
    end
    return voteChecksums
  end

  local function BuildLiveBoardItemPatchManifest(entry)
    entry = type(entry) == "table" and entry or {}
    local selectionManifest = BuildSequentialIdentityPatchManifest("live_board_selections", entry.selections, "optionKey")
    if type(entry.selections) == "table" and #entry.selections > 0 and type(selectionManifest) ~= "table" then
      return nil
    end
    return {
      kind = "live_board_item",
      fieldsChecksum = CalculateStableChecksum(BuildPatchScalarSource(entry, LIVE_BOARD_ITEM_OMITTED_KEYS)),
      selectionChecksums = type(selectionManifest) == "table" and selectionManifest.entryChecksums or {},
      selectionOrderChecksum = type(selectionManifest) == "table" and selectionManifest.orderChecksum or CalculateStableChecksum({}),
      voteChecksums = BuildLiveBoardItemVoteChecksums(entry),
    }
  end

  local function BuildLiveBoardItemPatchPayload(entry, remoteDetail)
    entry = type(entry) == "table" and entry or {}
    remoteDetail = type(remoteDetail) == "table" and remoteDetail or {}
    if type(remoteDetail.selectionChecksums) ~= "table" or type(remoteDetail.voteChecksums) ~= "table" then
      return nil
    end

    local selectionManifest = BuildSequentialIdentityPatchManifest("live_board_selections", entry.selections, "optionKey")
    if type(entry.selections) == "table" and #entry.selections > 0 and type(selectionManifest) ~= "table" then
      return nil
    end
    local selectionPatch = BuildSequentialIdentityPatchPayload("live_board_selections", entry.selections, "optionKey", {
      entryChecksums = remoteDetail.selectionChecksums,
      orderChecksum = remoteDetail.selectionOrderChecksum,
    })

    local fields = BuildPatchScalarSource(entry, LIVE_BOARD_ITEM_OMITTED_KEYS)
    local changedVotes, removedVotes = BuildPatchKeyedChanges(BuildLiveBoardVoteStateMap(entry), remoteDetail.voteChecksums)
    local patch = {
      kind = "live_board_item",
      itemKey = trim(entry.itemKey),
    }
    if trim(remoteDetail.fieldsChecksum) ~= CalculateStableChecksum(fields) then
      patch.fields = fields
    end
    if type(selectionPatch) == "table" then
      patch.changedSelections = selectionPatch.changedEntries
      patch.removedSelections = selectionPatch.removedEntries
      patch.selectionOrder = selectionPatch.order
    end
    if next(changedVotes) ~= nil then
      patch.changedVotes = changedVotes
    end
    if #removedVotes > 0 then
      patch.removedVotes = removedVotes
    end
    if patch.fields == nil
      and patch.changedSelections == nil
      and patch.removedSelections == nil
      and patch.selectionOrder == nil
      and patch.changedVotes == nil
      and patch.removedVotes == nil then
      return nil
    end
    return patch
  end

  local function ApplyLiveBoardVotePatch(entry, changedVotes, removedVotes)
    entry = type(entry) == "table" and entry or {}
    entry.votesByVoter = type(entry.votesByVoter) == "table" and entry.votesByVoter or {}
    entry.voteUpdatedAtByVoter = type(entry.voteUpdatedAtByVoter) == "table" and entry.voteUpdatedAtByVoter or {}
    entry.voteSequenceByVoter = type(entry.voteSequenceByVoter) == "table" and entry.voteSequenceByVoter or {}

    for _, voterKey in ipairs(type(removedVotes) == "table" and removedVotes or {}) do
      entry.votesByVoter[voterKey] = nil
      entry.voteUpdatedAtByVoter[voterKey] = nil
      entry.voteSequenceByVoter[voterKey] = nil
    end

    for voterKey, voteState in pairs(type(changedVotes) == "table" and changedVotes or {}) do
      local resolvedVoter = trim(voterKey)
      if resolvedVoter ~= "" then
        voteState = type(voteState) == "table" and voteState or {}
        local targetKey = trim(voteState.target)
        if targetKey ~= "" then
          entry.votesByVoter[resolvedVoter] = targetKey
        else
          entry.votesByVoter[resolvedVoter] = nil
        end

        local updatedAt = tonumber(voteState.updatedAt)
        if updatedAt and updatedAt > 0 then
          entry.voteUpdatedAtByVoter[resolvedVoter] = updatedAt
        else
          entry.voteUpdatedAtByVoter[resolvedVoter] = nil
        end

        local sequence = tonumber(voteState.sequence)
        if sequence and sequence > 0 then
          entry.voteSequenceByVoter[resolvedVoter] = math.floor(sequence)
        else
          entry.voteSequenceByVoter[resolvedVoter] = nil
        end
      end
    end
  end

  local function ApplyLiveBoardItemPatchPayload(existingEntry, patch, itemKey)
    local entry = type(existingEntry) == "table" and existingEntry or {}
    patch = type(patch) == "table" and patch or {}

    if type(patch.fields) == "table" then
      ApplyPatchScalarSource(entry, patch.fields, LIVE_BOARD_ITEM_OMITTED_KEYS)
    end
    if patch.changedSelections ~= nil or patch.removedSelections ~= nil or patch.selectionOrder ~= nil then
      entry.selections = ApplySequentialIdentityPatch(entry.selections, {
        changedEntries = patch.changedSelections,
        removedEntries = patch.removedSelections,
        order = patch.selectionOrder,
      }, "optionKey")
    end
    if patch.changedVotes ~= nil or patch.removedVotes ~= nil then
      ApplyLiveBoardVotePatch(entry, patch.changedVotes, patch.removedVotes)
    end

    local resolvedItemKey = trim(entry.itemKey)
    if resolvedItemKey == "" then
      entry.itemKey = trim(type(patch.itemKey) == "string" and patch.itemKey or itemKey)
    end
    return entry
  end

  local function BuildRollOptionsPatchManifest(rollOptions)
    return BuildSequentialIdentityPatchManifest("roll_options", rollOptions, "key")
  end

  local function BuildRollOptionsPatchPayload(rollOptions, remoteDetail)
    remoteDetail = type(remoteDetail) == "table" and remoteDetail or {}
    return BuildSequentialIdentityPatchPayload("roll_options", rollOptions, "key", {
      entryChecksums = remoteDetail.rollOptionChecksums,
      orderChecksum = remoteDetail.rollOptionOrderChecksum,
    })
  end

  local function ApplyRollOptionsPatch(existingList, patch)
    return ApplySequentialIdentityPatch(existingList, patch, "key")
  end

  return {
    BuildRollOptionsPatchManifest = BuildRollOptionsPatchManifest,
    BuildRollOptionsPatchPayload = BuildRollOptionsPatchPayload,
    ApplyRollOptionsPatch = ApplyRollOptionsPatch,
    BuildLiveBoardItemPatchManifest = BuildLiveBoardItemPatchManifest,
    BuildLiveBoardItemPatchPayload = BuildLiveBoardItemPatchPayload,
    ApplyLiveBoardItemPatchPayload = ApplyLiveBoardItemPatchPayload,
    BuildLiveBoardItemVoteChecksums = BuildLiveBoardItemVoteChecksums,
  }
end
