_G.LIDCOUNCIL_CreateCoreProfileConflictResolverHelpers_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 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
    if type(value) ~= "table" then
      return value
    end
    local cloned = {}
    for key, entry in pairs(value) do
      cloned[key] = copy(entry)
    end
    return cloned
  end

  local function checksum(value)
    if type(CalculateStableChecksum) == "function" then
      return tostring(CalculateStableChecksum(value))
    end
    return tostring(value)
  end

  local function sortKeys(values)
    if type(SortPatchKeys) == "function" then
      SortPatchKeys(values)
      return
    end
    table.sort(values, function(left, right)
      return tostring(left) < tostring(right)
    end)
  end

  local function collectKeys(left, right)
    local seen = {}
    local keys = {}
    for key in pairs(type(left) == "table" and left or {}) do
      local resolved = tostring(key)
      if not seen[resolved] then
        seen[resolved] = true
        keys[#keys + 1] = resolved
      end
    end
    for key in pairs(type(right) == "table" and right or {}) do
      local resolved = tostring(key)
      if not seen[resolved] then
        seen[resolved] = true
        keys[#keys + 1] = resolved
      end
    end
    sortKeys(keys)
    return keys
  end

  local function truncate(text, maxLen)
    local resolved = tostring(text or "")
    local limit = math.max(12, tonumber(maxLen) or 96)
    if #resolved <= limit then
      return resolved
    end
    return resolved:sub(1, limit - 3) .. "..."
  end

  local function summarizeValue(value)
    local valueType = type(value)
    if valueType == "nil" then
      return "Missing"
    elseif valueType == "boolean" then
      return value and "true" or "false"
    elseif valueType == "number" then
      return tostring(value)
    elseif valueType == "string" then
      local text = trim(value)
      return text ~= "" and truncate(text, 72) or "\"\""
    elseif valueType ~= "table" then
      return truncate(tostring(value), 72)
    end

    if value.deleted == true then
      return "Deleted"
    end
    if trim(value.itemLink) ~= "" then
      return truncate(value.itemLink, 72)
    end
    if trim(value.label) ~= "" then
      return truncate(value.label, 72)
    end
    if trim(value.name) ~= "" then
      return truncate(value.name, 72)
    end
    if trim(value.optionKey) ~= "" then
      return "Option " .. tostring(value.optionKey)
    end
    if trim(value.entryId) ~= "" then
      return "Entry " .. tostring(value.entryId)
    end
    if trim(value.target) ~= "" then
      local parts = { "Vote " .. tostring(value.target) }
      if tonumber(value.sequence) then
        parts[#parts + 1] = "seq " .. tostring(math.floor(tonumber(value.sequence) or 0))
      end
      return table.concat(parts, " | ")
    end

    local count = 0
    for _ in pairs(value) do
      count = count + 1
    end
    return "Table (" .. tostring(count) .. ")"
  end

  local function buildRowId(subdomain, suffix)
    return tostring(subdomain) .. "|" .. tostring(suffix)
  end

  local function addRow(rows, subdomain, pathLabel, localValue, remoteValue, applySpec)
    if checksum(localValue) == checksum(remoteValue) then
      return
    end
    rows[#rows + 1] = {
      id = buildRowId(subdomain, applySpec.idSuffix or pathLabel),
      subdomain = tostring(subdomain),
      pathLabel = tostring(pathLabel),
      hostSummary = summarizeValue(localValue),
      memberSummary = summarizeValue(remoteValue),
      applySpec = applySpec,
    }
  end

  local function buildIdentityMaps(entries, identityKey)
    local byIdentity, order = BuildStrictIdentityMap(entries, identityKey)
    if byIdentity then
      return byIdentity, order
    end
    return {}, {}
  end

  local function setSequentialEntryByIdentity(entries, identityKey, identity, remoteValue)
    local resolvedIdentity = trim(identity)
    local output = {}
    local inserted = false
    for _, entry in ipairs(type(entries) == "table" and entries or {}) do
      local entryIdentity = trim(type(entry) == "table" and entry[identityKey] or "")
      if entryIdentity == resolvedIdentity then
        if remoteValue ~= nil then
          output[#output + 1] = copy(remoteValue)
          inserted = true
        end
      else
        output[#output + 1] = entry
      end
    end
    if remoteValue ~= nil and not inserted then
      output[#output + 1] = copy(remoteValue)
    end
    return output
  end

  local function applySequentialOrder(entries, identityKey, remoteEntries)
    local currentByIdentity = select(1, buildIdentityMaps(entries, identityKey))
    local remoteByIdentity, remoteOrder = buildIdentityMaps(remoteEntries, identityKey)
    local rebuilt = {}
    local seen = {}
    for _, identity in ipairs(remoteOrder) do
      local current = currentByIdentity[identity]
      if current ~= nil then
        rebuilt[#rebuilt + 1] = current
        seen[identity] = true
      elseif remoteByIdentity[identity] ~= nil then
        rebuilt[#rebuilt + 1] = copy(remoteByIdentity[identity])
        seen[identity] = true
      end
    end
    local extras = {}
    for identity in pairs(currentByIdentity) do
      if not seen[identity] then
        extras[#extras + 1] = identity
      end
    end
    sortKeys(extras)
    for _, identity in ipairs(extras) do
      rebuilt[#rebuilt + 1] = currentByIdentity[identity]
    end
    return rebuilt
  end

  local function buildVoteStateMap(item)
    local output = {}
    local votesByVoter = type(item) == "table" and type(item.votesByVoter) == "table" and item.votesByVoter or {}
    local updatedAtByVoter = type(item) == "table" and type(item.voteUpdatedAtByVoter) == "table" and item.voteUpdatedAtByVoter or {}
    local sequenceByVoter = type(item) == "table" and type(item.voteSequenceByVoter) == "table" and item.voteSequenceByVoter or {}
    for voterKey, targetKey in pairs(votesByVoter) do
      local key = trim(voterKey)
      if key ~= "" then
        output[key] = output[key] or {}
        output[key].target = trim(targetKey)
      end
    end
    for voterKey, updatedAt in pairs(updatedAtByVoter) do
      local key = trim(voterKey)
      if key ~= "" then
        output[key] = output[key] or {}
        output[key].updatedAt = tonumber(updatedAt)
      end
    end
    for voterKey, sequence in pairs(sequenceByVoter) do
      local key = trim(voterKey)
      if key ~= "" then
        output[key] = output[key] or {}
        output[key].sequence = tonumber(sequence)
      end
    end
    return output
  end

  local function applyVoteState(item, voterKey, remoteState)
    item.votesByVoter = type(item.votesByVoter) == "table" and item.votesByVoter or {}
    item.voteUpdatedAtByVoter = type(item.voteUpdatedAtByVoter) == "table" and item.voteUpdatedAtByVoter or {}
    item.voteSequenceByVoter = type(item.voteSequenceByVoter) == "table" and item.voteSequenceByVoter or {}
    local resolvedVoter = trim(voterKey)
    if resolvedVoter == "" then
      return item
    end
    if type(remoteState) ~= "table" then
      item.votesByVoter[resolvedVoter] = nil
      item.voteUpdatedAtByVoter[resolvedVoter] = nil
      item.voteSequenceByVoter[resolvedVoter] = nil
      return item
    end
    if trim(remoteState.target) ~= "" then
      item.votesByVoter[resolvedVoter] = trim(remoteState.target)
    else
      item.votesByVoter[resolvedVoter] = nil
    end
    if tonumber(remoteState.updatedAt) then
      item.voteUpdatedAtByVoter[resolvedVoter] = tonumber(remoteState.updatedAt)
    else
      item.voteUpdatedAtByVoter[resolvedVoter] = nil
    end
    if tonumber(remoteState.sequence) then
      item.voteSequenceByVoter[resolvedVoter] = math.floor(tonumber(remoteState.sequence) or 0)
    else
      item.voteSequenceByVoter[resolvedVoter] = nil
    end
    return item
  end

  local function addScalarRows(rows, subdomain, prefixLabel, localTable, remoteTable, omittedKeys, tableField, leafKey)
    local keys = collectKeys(localTable, remoteTable)
    for _, key in ipairs(keys) do
      if omittedKeys[key] ~= true then
        addRow(rows, subdomain, prefixLabel .. " > " .. tostring(key), localTable[key], remoteTable[key], {
          kind = "table_field",
          idSuffix = tostring(leafKey) .. ":field:" .. tostring(key),
          tableField = tableField,
          key = key,
        })
      end
    end
  end

  local function addSequentialEntryRows(rows, subdomain, prefixLabel, localEntries, remoteEntries, identityKey, applyKind, idPrefix, tableField, extraSpec)
    local localByIdentity, localOrder = buildIdentityMaps(localEntries, identityKey)
    local remoteByIdentity, remoteOrder = buildIdentityMaps(remoteEntries, identityKey)
    if checksum(localOrder) ~= checksum(remoteOrder) then
      addRow(rows, subdomain, prefixLabel .. " > Order", localOrder, remoteOrder, {
        kind = "sequential_order",
        idSuffix = tostring(idPrefix) .. ":order",
        tableField = tableField,
        identityKey = identityKey,
        remoteEntries = copy(remoteEntries),
        itemKey = type(extraSpec) == "table" and extraSpec.itemKey or nil,
      })
    end
    local identities = collectKeys(localByIdentity, remoteByIdentity)
    for _, identity in ipairs(identities) do
      addRow(rows, subdomain, prefixLabel .. " > " .. tostring(identity), localByIdentity[identity], remoteByIdentity[identity], {
        kind = applyKind,
        idSuffix = tostring(idPrefix) .. ":entry:" .. tostring(identity),
        tableField = tableField,
        identityKey = identityKey,
        identity = identity,
        itemKey = type(extraSpec) == "table" and extraSpec.itemKey or nil,
      })
    end
  end

  local function buildStatsRows(subdomain, localPayload, remotePayload)
    local rows = {}
    local eventField = (
      (type(localPayload) == "table" and localPayload.voteEvents ~= nil)
      or (type(remotePayload) == "table" and remotePayload.voteEvents ~= nil)
    ) and "voteEvents" or "events"
    addScalarRows(rows, subdomain, "Stats", localPayload, remotePayload, {
      events = true,
      voteEvents = true,
    }, nil, "stats")
    local localEntries = type(localPayload) == "table" and type(localPayload[eventField]) == "table" and localPayload[eventField] or {}
    local remoteEntries = type(remotePayload) == "table" and type(remotePayload[eventField]) == "table" and remotePayload[eventField] or {}
    for _, entryKey in ipairs(collectKeys(localEntries, remoteEntries)) do
      addRow(rows, subdomain, "Stats > " .. tostring(entryKey), localEntries[entryKey], remoteEntries[entryKey], {
        kind = "keyed_entry",
        idSuffix = "stats:entry:" .. tostring(entryKey),
        tableField = eventField,
        key = entryKey,
      })
    end
    return rows
  end

  local function buildLootRowRows(subdomain, localPayload, remotePayload)
    local rows = {}
    if (type(localPayload) == "table" and localPayload.deleted == true) ~= (type(remotePayload) == "table" and remotePayload.deleted == true) then
      addRow(rows, subdomain, "Leaf State", localPayload, remotePayload, {
        kind = "whole_payload",
        idSuffix = "leaf",
      })
      return rows
    end
    local localRow = type(localPayload) == "table" and type(localPayload.row) == "table" and localPayload.row or {}
    local remoteRow = type(remotePayload) == "table" and type(remotePayload.row) == "table" and remotePayload.row or {}
    addScalarRows(rows, subdomain, "Row", localRow, remoteRow, {
      items = true,
      totalPoints = true,
      character = true,
    }, "row", "row")
    addSequentialEntryRows(rows, subdomain, "Row > Items", localRow.items, remoteRow.items, "entryId", "row_item", "row_items", "row.items")
    return rows
  end

  local function buildKeyedMapRows(subdomain, localPayload, remotePayload, payloadField, labelPrefix)
    local rows = {}
    if (type(localPayload) == "table" and localPayload.deleted == true) ~= (type(remotePayload) == "table" and remotePayload.deleted == true) then
      addRow(rows, subdomain, "Leaf State", localPayload, remotePayload, {
        kind = "whole_payload",
        idSuffix = "leaf",
      })
      return rows
    end
    local localEntries = type(localPayload) == "table" and type(localPayload[payloadField]) == "table" and localPayload[payloadField] or {}
    local remoteEntries = type(remotePayload) == "table" and type(remotePayload[payloadField]) == "table" and remotePayload[payloadField] or {}
    for _, entryKey in ipairs(collectKeys(localEntries, remoteEntries)) do
      addRow(rows, subdomain, labelPrefix .. " > " .. tostring(entryKey), localEntries[entryKey], remoteEntries[entryKey], {
        kind = "keyed_entry",
        idSuffix = tostring(payloadField) .. ":entry:" .. tostring(entryKey),
        tableField = payloadField,
        key = entryKey,
      })
    end
    return rows
  end

  local function buildSessionRows(subdomain, localPayload, remotePayload)
    local rows = {}
    if (type(localPayload) == "table" and localPayload.deleted == true) ~= (type(remotePayload) == "table" and remotePayload.deleted == true) then
      addRow(rows, subdomain, "Leaf State", localPayload, remotePayload, {
        kind = "whole_payload",
        idSuffix = "leaf",
      })
      return rows
    end
    local localSession = type(localPayload) == "table" and type(localPayload.session) == "table" and localPayload.session or {}
    local remoteSession = type(remotePayload) == "table" and type(remotePayload.session) == "table" and remotePayload.session or {}
    addScalarRows(rows, subdomain, "Session", localSession, remoteSession, {
      members = true,
      rollOptions = true,
    }, "session", "session")
    local localMembers = type(localSession.members) == "table" and localSession.members or {}
    local remoteMembers = type(remoteSession.members) == "table" and remoteSession.members or {}
    for _, memberKey in ipairs(collectKeys(localMembers, remoteMembers)) do
      addRow(rows, subdomain, "Session > Members > " .. tostring(memberKey), localMembers[memberKey], remoteMembers[memberKey], {
        kind = "keyed_entry",
        idSuffix = "session:member:" .. tostring(memberKey),
        tableField = "session.members",
        key = memberKey,
      })
    end
    addSequentialEntryRows(rows, subdomain, "Session > Roll Options", localSession.rollOptions, remoteSession.rollOptions, "optionKey", "roll_option", "session:roll_options", "session.rollOptions")
    return rows
  end

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

  local function buildLiveBoardItemRows(rows, subdomain, itemKey, localItem, remoteItem)
    if type(localItem) ~= "table" or type(remoteItem) ~= "table" then
      addRow(rows, subdomain, "Snapshot > Item " .. tostring(itemKey), localItem, remoteItem, {
        kind = "live_board_item",
        idSuffix = "snapshot:item:" .. tostring(itemKey),
        itemKey = itemKey,
      })
      return
    end
    for _, key in ipairs(collectKeys(localItem, remoteItem)) do
      if LIVE_BOARD_ITEM_OMITTED_KEYS[key] ~= true then
        addRow(rows, subdomain, "Snapshot > Item " .. tostring(itemKey) .. " > " .. tostring(key), localItem[key], remoteItem[key], {
          kind = "table_field",
          idSuffix = "snapshot:item:" .. tostring(itemKey) .. ":field:" .. tostring(key),
          tableField = "snapshot.items_scalar",
          key = key,
          itemKey = itemKey,
        })
      end
    end
    addSequentialEntryRows(
      rows,
      subdomain,
      "Snapshot > Item " .. tostring(itemKey) .. " > Selections",
      localItem.selections,
      remoteItem.selections,
      "optionKey",
      "live_board_selection",
      "snapshot:item:" .. tostring(itemKey) .. ":selections",
      "snapshot.items_selections",
      { itemKey = itemKey }
    )
    local localVotes = buildVoteStateMap(localItem)
    local remoteVotes = buildVoteStateMap(remoteItem)
    for _, voterKey in ipairs(collectKeys(localVotes, remoteVotes)) do
      addRow(rows, subdomain, "Snapshot > Item " .. tostring(itemKey) .. " > Votes > " .. tostring(voterKey), localVotes[voterKey], remoteVotes[voterKey], {
        kind = "live_board_vote",
        idSuffix = "snapshot:item:" .. tostring(itemKey) .. ":vote:" .. tostring(voterKey),
        itemKey = itemKey,
        voterKey = voterKey,
      })
    end
  end

  local function buildLiveBoardRows(subdomain, localPayload, remotePayload)
    local rows = {}
    if (type(localPayload) == "table" and localPayload.deleted == true) ~= (type(remotePayload) == "table" and remotePayload.deleted == true) then
      addRow(rows, subdomain, "Leaf State", localPayload, remotePayload, {
        kind = "whole_payload",
        idSuffix = "leaf",
      })
      return rows
    end
    local localSnapshot = type(localPayload) == "table" and type(localPayload.snapshot) == "table" and localPayload.snapshot or {}
    local remoteSnapshot = type(remotePayload) == "table" and type(remotePayload.snapshot) == "table" and remotePayload.snapshot or {}
    addScalarRows(rows, subdomain, "Snapshot", localSnapshot, remoteSnapshot, {
      items = true,
      itemOrder = true,
    }, "snapshot", "snapshot")
    if checksum(type(localSnapshot.itemOrder) == "table" and localSnapshot.itemOrder or {}) ~= checksum(type(remoteSnapshot.itemOrder) == "table" and remoteSnapshot.itemOrder or {}) then
      addRow(rows, subdomain, "Snapshot > Item Order", localSnapshot.itemOrder or {}, remoteSnapshot.itemOrder or {}, {
        kind = "snapshot_item_order",
        idSuffix = "snapshot:item_order",
      })
    end
    local localItems = type(localSnapshot.items) == "table" and localSnapshot.items or {}
    local remoteItems = type(remoteSnapshot.items) == "table" and remoteSnapshot.items or {}
    for _, itemKey in ipairs(collectKeys(localItems, remoteItems)) do
      buildLiveBoardItemRows(rows, subdomain, itemKey, localItems[itemKey], remoteItems[itemKey])
    end
    return rows
  end

  local function BuildSubdomainConflictRows(subdomain, localPayload, remotePayload)
    local resolvedSubdomain = trim(subdomain)
    if resolvedSubdomain == "" then
      return {}
    end
    if resolvedSubdomain == "lrsr" or resolvedSubdomain == "lrsv" then
      return buildStatsRows(resolvedSubdomain, localPayload, remotePayload)
    end
    if resolvedSubdomain:match("^llr:") then
      return buildLootRowRows(resolvedSubdomain, localPayload, remotePayload)
    end
    if resolvedSubdomain:match("^lmr:") then
      return buildKeyedMapRows(resolvedSubdomain, localPayload, remotePayload, "meta", "Meta")
    end
    if resolvedSubdomain:match("^ssr:") then
      return buildSessionRows(resolvedSubdomain, localPayload, remotePayload)
    end
    if resolvedSubdomain:match("^slb:") then
      return buildLiveBoardRows(resolvedSubdomain, localPayload, remotePayload)
    end
    return {}
  end

  local function ensureNestedTable(root, path)
    local cursor = root
    for _, key in ipairs(path) do
      cursor[key] = type(cursor[key]) == "table" and cursor[key] or {}
      cursor = cursor[key]
    end
    return cursor
  end

  local function applyTableField(payload, applySpec, remotePayload)
    local localValue = payload
    local remoteValue = remotePayload
    local containerPath = {}
    local key = tostring(applySpec.key or "")
    if applySpec.tableField == "row" then
      containerPath = { "row" }
    elseif applySpec.tableField == "session" then
      containerPath = { "session" }
    elseif applySpec.tableField == "snapshot" then
      containerPath = { "snapshot" }
    elseif applySpec.tableField == "snapshot.items_scalar" then
      local itemKey = tostring(applySpec.itemKey or "")
      if itemKey == "" then
        return payload
      end
      containerPath = { "snapshot", "items", itemKey }
    end
    for _, pathKey in ipairs(containerPath) do
      localValue = type(localValue) == "table" and localValue[pathKey] or nil
      remoteValue = type(remoteValue) == "table" and remoteValue[pathKey] or nil
    end
    local localContainer = ensureNestedTable(payload, containerPath)
    local remoteContainer = type(remoteValue) == "table" and remoteValue or {}
    localContainer[key] = copy(remoteContainer[key])
    return payload
  end

  local function applyKeyedEntry(payload, applySpec, remotePayload)
    local tableField = tostring(applySpec.tableField or "")
    local entryKey = tostring(applySpec.key or "")
    if tableField == "events" or tableField == "voteEvents" then
      payload[tableField] = type(payload[tableField]) == "table" and payload[tableField] or {}
      local remoteEntries = type(remotePayload[tableField]) == "table" and remotePayload[tableField] or {}
      payload[tableField][entryKey] = copy(remoteEntries[entryKey])
      if remoteEntries[entryKey] == nil then
        payload[tableField][entryKey] = nil
      end
      return payload
    end
    if tableField == "meta" then
      payload.meta = type(payload.meta) == "table" and payload.meta or {}
      local remoteEntries = type(remotePayload.meta) == "table" and remotePayload.meta or {}
      payload.meta[entryKey] = copy(remoteEntries[entryKey])
      if remoteEntries[entryKey] == nil then
        payload.meta[entryKey] = nil
      end
      return payload
    end
    if tableField == "session.members" then
      payload.session = type(payload.session) == "table" and payload.session or {}
      payload.session.members = type(payload.session.members) == "table" and payload.session.members or {}
      local remoteEntries = type(remotePayload.session) == "table" and type(remotePayload.session.members) == "table" and remotePayload.session.members or {}
      payload.session.members[entryKey] = copy(remoteEntries[entryKey])
      if remoteEntries[entryKey] == nil then
        payload.session.members[entryKey] = nil
      end
      return payload
    end
    return payload
  end

  local function applySequentialEntry(payload, applySpec, remotePayload)
    local tableField = tostring(applySpec.tableField or "")
    local identityKey = tostring(applySpec.identityKey or "")
    local identity = tostring(applySpec.identity or "")
    local function remoteSequential(path)
      local cursor = remotePayload
      for _, key in ipairs(path) do
        cursor = type(cursor) == "table" and cursor[key] or nil
      end
      return type(cursor) == "table" and cursor or {}
    end
    local function localSequential(path)
      local cursor = payload
      for _, key in ipairs(path) do
        cursor = type(cursor) == "table" and cursor[key] or nil
      end
      return type(cursor) == "table" and cursor or {}
    end
    if tableField == "row.items" then
      payload.row = type(payload.row) == "table" and payload.row or {}
      local remoteEntries = remoteSequential({ "row", "items" })
      payload.row.items = setSequentialEntryByIdentity(localSequential({ "row", "items" }), identityKey, identity, select(1, buildIdentityMaps(remoteEntries, identityKey))[identity])
      return payload
    end
    if tableField == "session.rollOptions" then
      payload.session = type(payload.session) == "table" and payload.session or {}
      local remoteEntries = remoteSequential({ "session", "rollOptions" })
      payload.session.rollOptions = setSequentialEntryByIdentity(localSequential({ "session", "rollOptions" }), identityKey, identity, select(1, buildIdentityMaps(remoteEntries, identityKey))[identity])
      return payload
    end
    if tableField == "snapshot.items_selections" then
      local itemKey = tostring(applySpec.itemKey or "")
      payload.snapshot = type(payload.snapshot) == "table" and payload.snapshot or {}
      payload.snapshot.items = type(payload.snapshot.items) == "table" and payload.snapshot.items or {}
      payload.snapshot.items[itemKey] = type(payload.snapshot.items[itemKey]) == "table" and payload.snapshot.items[itemKey] or {}
      local remoteEntries = remoteSequential({ "snapshot", "items", itemKey, "selections" })
      payload.snapshot.items[itemKey].selections = setSequentialEntryByIdentity(
        localSequential({ "snapshot", "items", itemKey, "selections" }),
        identityKey,
        identity,
        select(1, buildIdentityMaps(remoteEntries, identityKey))[identity]
      )
      return payload
    end
    return payload
  end

  local function applySequentialOrderSelection(payload, applySpec, remotePayload)
    local tableField = tostring(applySpec.tableField or "")
    local identityKey = tostring(applySpec.identityKey or "")
    local remoteEntries = type(applySpec.remoteEntries) == "table" and applySpec.remoteEntries or {}
    if tableField == "row.items" then
      payload.row = type(payload.row) == "table" and payload.row or {}
      payload.row.items = applySequentialOrder(payload.row.items, identityKey, remoteEntries)
      return payload
    end
    if tableField == "session.rollOptions" then
      payload.session = type(payload.session) == "table" and payload.session or {}
      payload.session.rollOptions = applySequentialOrder(payload.session.rollOptions, identityKey, remoteEntries)
      return payload
    end
    if tableField == "snapshot.items_selections" then
      local itemKey = tostring(applySpec.itemKey or "")
      payload.snapshot = type(payload.snapshot) == "table" and payload.snapshot or {}
      payload.snapshot.items = type(payload.snapshot.items) == "table" and payload.snapshot.items or {}
      payload.snapshot.items[itemKey] = type(payload.snapshot.items[itemKey]) == "table" and payload.snapshot.items[itemKey] or {}
      payload.snapshot.items[itemKey].selections = applySequentialOrder(payload.snapshot.items[itemKey].selections, identityKey, remoteEntries)
      return payload
    end
    return payload
  end

  local function applyLiveBoardVote(payload, applySpec, remotePayload)
    local itemKey = tostring(applySpec.itemKey or "")
    local voterKey = tostring(applySpec.voterKey or "")
    if itemKey == "" or voterKey == "" then
      return payload
    end
    payload.snapshot = type(payload.snapshot) == "table" and payload.snapshot or {}
    payload.snapshot.items = type(payload.snapshot.items) == "table" and payload.snapshot.items or {}
    payload.snapshot.items[itemKey] = type(payload.snapshot.items[itemKey]) == "table" and payload.snapshot.items[itemKey] or {}
    local remoteItem = type(remotePayload.snapshot) == "table" and type(remotePayload.snapshot.items) == "table" and remotePayload.snapshot.items[itemKey] or nil
    local remoteVotes = buildVoteStateMap(remoteItem)
    payload.snapshot.items[itemKey] = applyVoteState(payload.snapshot.items[itemKey], voterKey, remoteVotes[voterKey])
    return payload
  end

  local function applyLiveBoardMemberSelection(payload, applySpec, remotePayload)
    local itemKey = tostring(applySpec.itemKey or "")
    local memberKey = tostring(applySpec.memberKey or "")
    if itemKey == "" or memberKey == "" then
      return payload
    end
    payload.snapshot = type(payload.snapshot) == "table" and payload.snapshot or {}
    payload.snapshot.items = type(payload.snapshot.items) == "table" and payload.snapshot.items or {}
    payload.snapshot.items[itemKey] = type(payload.snapshot.items[itemKey]) == "table" and payload.snapshot.items[itemKey] or {}
    payload.snapshot.items[itemKey].selections = type(payload.snapshot.items[itemKey].selections) == "table"
      and payload.snapshot.items[itemKey].selections
      or {}
    local remoteSelection = type(remotePayload.snapshot) == "table"
      and type(remotePayload.snapshot.items) == "table"
      and type(remotePayload.snapshot.items[itemKey]) == "table"
      and type(remotePayload.snapshot.items[itemKey].selections) == "table"
      and remotePayload.snapshot.items[itemKey].selections[memberKey]
      or nil
    payload.snapshot.items[itemKey].selections[memberKey] = copy(remoteSelection)
    if remoteSelection == nil then
      payload.snapshot.items[itemKey].selections[memberKey] = nil
    end
    return payload
  end

  local function applyLiveBoardItem(payload, applySpec, remotePayload)
    local itemKey = tostring(applySpec.itemKey or "")
    if itemKey == "" then
      return payload
    end
    payload.snapshot = type(payload.snapshot) == "table" and payload.snapshot or {}
    payload.snapshot.items = type(payload.snapshot.items) == "table" and payload.snapshot.items or {}
    local remoteItems = type(remotePayload.snapshot) == "table" and type(remotePayload.snapshot.items) == "table" and remotePayload.snapshot.items or {}
    payload.snapshot.items[itemKey] = copy(remoteItems[itemKey])
    if remoteItems[itemKey] == nil then
      payload.snapshot.items[itemKey] = nil
    end
    return payload
  end

  local function applyWholePayload(payload, remotePayload)
    return copy(remotePayload)
  end

  local function applySnapshotItemOrder(payload, remotePayload)
    payload.snapshot = type(payload.snapshot) == "table" and payload.snapshot or {}
    payload.snapshot.itemOrder = copy(type(remotePayload.snapshot) == "table" and remotePayload.snapshot.itemOrder or {})
    return payload
  end

  local function buildSyntheticRemotePayload(applySpec, remotePayload)
    applySpec = type(applySpec) == "table" and applySpec or {}
    remotePayload = type(remotePayload) == "table" and remotePayload or {}
    local kind = tostring(applySpec.kind or "")
    if kind == "whole_payload" then
      return copy(remotePayload)
    elseif kind == "table_field" then
      local payload = {}
      local key = tostring(applySpec.key or "")
      if applySpec.tableField == "row" then
        payload.row = { [key] = copy(type(remotePayload.row) == "table" and remotePayload.row[key] or nil) }
      elseif applySpec.tableField == "session" then
        payload.session = { [key] = copy(type(remotePayload.session) == "table" and remotePayload.session[key] or nil) }
      elseif applySpec.tableField == "snapshot" then
        payload.snapshot = { [key] = copy(type(remotePayload.snapshot) == "table" and remotePayload.snapshot[key] or nil) }
      elseif applySpec.tableField == "snapshot.items_scalar" then
        local itemKey = tostring(applySpec.itemKey or "")
        payload.snapshot = {
          items = {
            [itemKey] = {
              [key] = copy(
                type(remotePayload.snapshot) == "table"
                  and type(remotePayload.snapshot.items) == "table"
                  and type(remotePayload.snapshot.items[itemKey]) == "table"
                  and remotePayload.snapshot.items[itemKey][key]
                  or nil
              ),
            },
          },
        }
      end
      return payload
    elseif kind == "keyed_entry" then
      local payload = {}
      local key = tostring(applySpec.key or "")
      if applySpec.tableField == "events" or applySpec.tableField == "voteEvents" then
        payload[applySpec.tableField] = {
          [key] = copy(type(remotePayload[applySpec.tableField]) == "table" and remotePayload[applySpec.tableField][key] or nil),
        }
      elseif applySpec.tableField == "meta" then
        payload.meta = {
          [key] = copy(type(remotePayload.meta) == "table" and remotePayload.meta[key] or nil),
        }
      elseif applySpec.tableField == "session.members" then
        payload.session = {
          members = {
            [key] = copy(
              type(remotePayload.session) == "table"
                and type(remotePayload.session.members) == "table"
                and remotePayload.session.members[key]
                or nil
            ),
          },
        }
      end
      return payload
    elseif kind == "row_item" or kind == "roll_option" or kind == "live_board_selection" then
      local identityKey = tostring(applySpec.identityKey or "")
      local identity = tostring(applySpec.identity or "")
      local function buildEntry(list)
        return select(1, buildIdentityMaps(list, identityKey))[identity]
      end
      if applySpec.tableField == "row.items" then
        return {
          row = {
            items = {
              copy(buildEntry(type(remotePayload.row) == "table" and remotePayload.row.items or {})),
            },
          },
        }
      elseif applySpec.tableField == "session.rollOptions" then
        return {
          session = {
            rollOptions = {
              copy(buildEntry(type(remotePayload.session) == "table" and remotePayload.session.rollOptions or {})),
            },
          },
        }
      elseif applySpec.tableField == "snapshot.items_selections" then
        local itemKey = tostring(applySpec.itemKey or "")
        return {
          snapshot = {
            items = {
              [itemKey] = {
                selections = {
                  copy(
                    buildEntry(
                      type(remotePayload.snapshot) == "table"
                        and type(remotePayload.snapshot.items) == "table"
                        and type(remotePayload.snapshot.items[itemKey]) == "table"
                        and remotePayload.snapshot.items[itemKey].selections
                        or {}
                    )
                  ),
                },
              },
            },
          },
        }
      end
    elseif kind == "sequential_order" then
      return {
        __sequentialOrder = copy(type(applySpec.remoteEntries) == "table" and applySpec.remoteEntries or {}),
      }
    elseif kind == "live_board_vote" then
      local itemKey = tostring(applySpec.itemKey or "")
      local voterKey = tostring(applySpec.voterKey or "")
      local remoteVotes = buildVoteStateMap(
        type(remotePayload.snapshot) == "table"
          and type(remotePayload.snapshot.items) == "table"
          and remotePayload.snapshot.items[itemKey]
          or nil
      )
      local remoteState = remoteVotes[voterKey]
      local payload = {
        snapshot = {
          items = {
            [itemKey] = {
              votesByVoter = {},
              voteUpdatedAtByVoter = {},
              voteSequenceByVoter = {},
            },
          },
        },
      }
      if type(remoteState) == "table" then
        if trim(remoteState.target) ~= "" then
          payload.snapshot.items[itemKey].votesByVoter[voterKey] = trim(remoteState.target)
        end
        if tonumber(remoteState.updatedAt) then
          payload.snapshot.items[itemKey].voteUpdatedAtByVoter[voterKey] = tonumber(remoteState.updatedAt)
        end
        if tonumber(remoteState.sequence) then
          payload.snapshot.items[itemKey].voteSequenceByVoter[voterKey] = tonumber(remoteState.sequence)
        end
      end
      return payload
    elseif kind == "live_board_member_selection" then
      local itemKey = tostring(applySpec.itemKey or "")
      local memberKey = tostring(applySpec.memberKey or "")
      return {
        snapshot = {
          items = {
            [itemKey] = {
              selections = {
                [memberKey] = copy(
                  type(remotePayload.snapshot) == "table"
                    and type(remotePayload.snapshot.items) == "table"
                    and type(remotePayload.snapshot.items[itemKey]) == "table"
                    and type(remotePayload.snapshot.items[itemKey].selections) == "table"
                    and remotePayload.snapshot.items[itemKey].selections[memberKey]
                    or nil
                ),
              },
            },
          },
        },
      }
    elseif kind == "live_board_item" then
      local itemKey = tostring(applySpec.itemKey or "")
      return {
        snapshot = {
          items = {
            [itemKey] = copy(
              type(remotePayload.snapshot) == "table"
                and type(remotePayload.snapshot.items) == "table"
                and remotePayload.snapshot.items[itemKey]
                or nil
            ),
          },
        },
      }
    elseif kind == "snapshot_item_order" then
      return {
        snapshot = {
          itemOrder = copy(type(remotePayload.snapshot) == "table" and remotePayload.snapshot.itemOrder or {}),
        },
      }
    end
    return {}
  end

  local function BuildSelectedRemoteRowPayloads(subdomain, remotePayload, selectedRowIds, rowMap)
    local rows = {}
    local selectedLookup = {}
    for _, rowId in ipairs(type(selectedRowIds) == "table" and selectedRowIds or {}) do
      selectedLookup[tostring(rowId)] = true
    end
    for rowId, row in pairs(type(rowMap) == "table" and rowMap or {}) do
      if selectedLookup[tostring(rowId)] then
        local applySpec = type(row) == "table" and type(row.applySpec) == "table" and row.applySpec or {}
        rows[#rows + 1] = {
          id = tostring(rowId),
          subdomain = tostring(subdomain or ""),
          applySpec = copy(applySpec),
          remotePayload = buildSyntheticRemotePayload(applySpec, remotePayload),
        }
      end
    end
    table.sort(rows, function(left, right)
      return tostring(left.id or "") < tostring(right.id or "")
    end)
    return rows
  end

  local function ApplySelectedRemoteRowPayloads(subdomain, localPayload, selectedRows)
    local merged = copy(localPayload)
    for _, selectedRow in ipairs(type(selectedRows) == "table" and selectedRows or {}) do
      local applySpec = type(selectedRow) == "table" and type(selectedRow.applySpec) == "table" and selectedRow.applySpec or {}
      local remotePayload = type(selectedRow) == "table" and type(selectedRow.remotePayload) == "table" and selectedRow.remotePayload or {}
      local kind = tostring(applySpec.kind or "")
      if kind == "whole_payload" then
        merged = applyWholePayload(merged, remotePayload)
      elseif kind == "table_field" then
        merged = applyTableField(merged, applySpec, remotePayload)
      elseif kind == "keyed_entry" then
        merged = applyKeyedEntry(merged, applySpec, remotePayload)
      elseif kind == "row_item" or kind == "roll_option" or kind == "live_board_selection" then
        merged = applySequentialEntry(merged, applySpec, remotePayload)
      elseif kind == "sequential_order" then
        local syntheticSpec = copy(applySpec)
        syntheticSpec.remoteEntries = type(remotePayload.__sequentialOrder) == "table" and remotePayload.__sequentialOrder or {}
        merged = applySequentialOrderSelection(merged, syntheticSpec, remotePayload)
      elseif kind == "live_board_vote" then
        merged = applyLiveBoardVote(merged, applySpec, remotePayload)
      elseif kind == "live_board_member_selection" then
        merged = applyLiveBoardMemberSelection(merged, applySpec, remotePayload)
      elseif kind == "live_board_item" then
        merged = applyLiveBoardItem(merged, applySpec, remotePayload)
      elseif kind == "snapshot_item_order" then
        merged = applySnapshotItemOrder(merged, remotePayload)
      end
    end
    return merged
  end

  local function ApplySelectedRemoteRows(subdomain, localPayload, remotePayload, selectedRowIds, rowMap)
    local merged = copy(localPayload)
    local selections = {}
    for _, rowId in ipairs(type(selectedRowIds) == "table" and selectedRowIds or {}) do
      selections[tostring(rowId)] = true
    end
    for rowId, row in pairs(type(rowMap) == "table" and rowMap or {}) do
      if selections[tostring(rowId)] then
        local applySpec = type(row) == "table" and type(row.applySpec) == "table" and row.applySpec or {}
        local kind = tostring(applySpec.kind or "")
        if kind == "whole_payload" then
          merged = applyWholePayload(merged, remotePayload)
        elseif kind == "table_field" then
          merged = applyTableField(merged, applySpec, remotePayload)
        elseif kind == "keyed_entry" then
          merged = applyKeyedEntry(merged, applySpec, remotePayload)
        elseif kind == "row_item" or kind == "roll_option" or kind == "live_board_selection" then
          merged = applySequentialEntry(merged, applySpec, remotePayload)
        elseif kind == "sequential_order" then
          merged = applySequentialOrderSelection(merged, applySpec, remotePayload)
        elseif kind == "live_board_vote" then
          merged = applyLiveBoardVote(merged, applySpec, remotePayload)
        elseif kind == "live_board_member_selection" then
          merged = applyLiveBoardMemberSelection(merged, applySpec, remotePayload)
        elseif kind == "live_board_item" then
          merged = applyLiveBoardItem(merged, applySpec, remotePayload)
        elseif kind == "snapshot_item_order" then
          merged = applySnapshotItemOrder(merged, remotePayload)
        end
      end
    end
    return merged
  end

  return {
    BuildSubdomainConflictRows = BuildSubdomainConflictRows,
    BuildSelectedRemoteRowPayloads = BuildSelectedRemoteRowPayloads,
    ApplySelectedRemoteRowPayloads = ApplySelectedRemoteRowPayloads,
    ApplySelectedRemoteRows = ApplySelectedRemoteRows,
  }
end
