-- Code generated from fnl/leap/beacons.fnl - do not edit directly.

local hl = require("leap.highlight")
local opts = require("leap.opts")
local api = vim.api
local function set_beacon_to_match_hl(target)
  local _let_1_ = target["chars"]
  local ch1 = _let_1_[1]
  local ch2 = _let_1_[2]
  local _let_2_ = target["pos"]
  local _ = _let_2_[1]
  local col = _let_2_[2]
  local extmark_opts
  if (ch1 == "") then
    extmark_opts = {virt_text = {{" ", hl.group.match}}}
  else
    extmark_opts = {hl_group = hl.group.match, end_col = (col + ch1:len() + ch2:len() + -1)}
  end
  target.beacon = {0, extmark_opts}
  return nil
end
local function get_label_offset(target)
  local _let_4_ = target["chars"]
  local ch1 = _let_4_[1]
  local ch2 = _let_4_[2]
  local _5_
  if target["win-edge?"] then
    _5_ = 0
  else
    _5_ = ch2:len()
  end
  return (ch1:len() + _5_)
end
local function set_beacon_for_labeled(target, _3fgroup_offset, _3fphase)
  local offset
  if (target.chars and _3fphase) then
    offset = get_label_offset(target)
  else
    offset = 0
  end
  local has_ch2_3f = (target.chars and (target.chars[2] ~= ""))
  local pad
  if (has_ch2_3f and not _3fphase) then
    pad = " "
  else
    pad = ""
  end
  local label = (opts.substitute_chars[target.label] or target.label)
  local relative_group = (target.group - (_3fgroup_offset or 0))
  local show_all_3f = (_3fphase and not opts.highlight_unlabeled_phase_one_targets)
  local vtext
  if (relative_group == 1) then
    vtext = {{(label .. pad), hl.group.label}}
  elseif (relative_group == 2) then
    vtext = {{(opts.concealed_label .. pad), hl.group["label-dimmed"]}}
  elseif ((relative_group > 2) and show_all_3f) then
    vtext = {{(opts.concealed_label .. pad), hl.group["label-dimmed"]}}
  else
    vtext = nil
  end
  if vtext then
    target.beacon = {offset, {virt_text = vtext}}
  else
    target.beacon = nil
  end
  return nil
end
local function set_beacons(targets, _11_)
  local group_offset = _11_["group-offset"]
  local use_no_labels_3f = _11_["use-no-labels?"]
  local phase = _11_["phase"]
  if use_no_labels_3f then
    if targets[1].chars then
      for _, target in ipairs(targets) do
        set_beacon_to_match_hl(target)
      end
      return nil
    else
      for _, target in ipairs(targets) do
        target.beacon = nil
      end
      return nil
    end
  else
    for _, target in ipairs(targets) do
      if target.label then
        if ((phase ~= 1) or target["previewable?"]) then
          set_beacon_for_labeled(target, group_offset, phase)
        else
        end
      elseif ((phase == 1) and target["previewable?"] and opts.highlight_unlabeled_phase_one_targets) then
        set_beacon_to_match_hl(target)
      else
      end
    end
    return nil
  end
end
local function resolve_conflicts(targets)
  local function set_beacon_to_concealed_label(target)
    local vtext = target.beacon[2].virt_text
    if vtext then
      vtext[1][1] = opts.concealed_label
      return nil
    else
      return nil
    end
  end
  local unlabeled_match_positions = {}
  local label_positions = {}
  for _, target in ipairs(targets) do
    local empty_line_3f = ((target.chars[1] == "") and (target.pos[2] == 0))
    if not empty_line_3f then
      local buf = target.wininfo["bufnr"]
      local win = target.wininfo["winid"]
      local lnum = target.pos[1]
      local col_ch1 = target.pos[2]
      local col_ch2 = (col_ch1 + string.len(target.chars[1]))
      local key_prefix = (buf .. " " .. win .. " " .. lnum .. " ")
      if (target.label and target.beacon) then
        local label_offset = target.beacon[1]
        local col_label = (col_ch1 + label_offset)
        local shifted_label_3f = (col_label == col_ch2)
        do
          local _17_
          local or_18_ = label_positions[(key_prefix .. col_label)]
          if not or_18_ then
            if shifted_label_3f then
              or_18_ = unlabeled_match_positions[(key_prefix .. col_ch1)]
            else
              or_18_ = nil
            end
          end
          if not or_18_ then
            or_18_ = unlabeled_match_positions[(key_prefix .. col_label)]
          end
          _17_ = or_18_
          if (nil ~= _17_) then
            local other = _17_
            other.beacon = nil
            set_beacon_to_concealed_label(target)
          else
          end
        end
        label_positions[(key_prefix .. col_label)] = target
      else
        local col_ch3 = (col_ch2 + string.len(target.chars[2]))
        do
          local _21_
          local or_22_ = label_positions[(key_prefix .. col_ch1)]
          if not or_22_ then
            or_22_ = label_positions[(key_prefix .. col_ch2)]
          end
          if not or_22_ then
            or_22_ = label_positions[(key_prefix .. col_ch3)]
          end
          _21_ = or_22_
          if (nil ~= _21_) then
            local other = _21_
            target.beacon = nil
            set_beacon_to_concealed_label(other)
          else
          end
        end
        unlabeled_match_positions[(key_prefix .. col_ch1)] = target
        unlabeled_match_positions[(key_prefix .. col_ch2)] = target
      end
    else
    end
  end
  return nil
end
local light_up_beacons
do
  local ns = api.nvim_create_namespace("")
  local extmarks = {}
  local function light_up_beacon(target, endpos_3f)
    local _let_26_ = ((endpos_3f and target.endpos) or target.pos)
    local lnum = _let_26_[1]
    local col = _let_26_[2]
    local buf = target.wininfo.bufnr
    local offset = target.beacon[1]
    local opts_2a = target.beacon[2]
    local opts0 = vim.tbl_extend("keep", opts_2a, {virt_text_pos = (opts.virt_text_pos or "overlay"), hl_mode = "combine", priority = hl.priority.label, strict = false})
    local id = api.nvim_buf_set_extmark(buf, ns, (lnum - 1), (col + -1 + offset), opts0)
    return table.insert(extmarks, {buf, id})
  end
  local function _27_(targets, _3fstart, _3fend)
    if (not opts.on_beacons or (opts.on_beacons(targets, _3fstart, _3fend) ~= false)) then
      for i = (_3fstart or 1), (_3fend or #targets) do
        local target = targets[i]
        if target.beacon then
          light_up_beacon(target)
          if target.endpos then
            light_up_beacon(target, true)
          else
          end
        else
        end
      end
      local function _30_()
        for _, _31_ in ipairs(extmarks) do
          local buf = _31_[1]
          local id = _31_[2]
          if api.nvim_buf_is_valid(buf) then
            api.nvim_buf_del_extmark(buf, ns, id)
          else
          end
        end
        extmarks = {}
        return nil
      end
      return api.nvim_create_autocmd("User", {pattern = {"LeapRedraw", "LeapLeave"}, once = true, callback = _30_})
    else
      return nil
    end
  end
  light_up_beacons = _27_
end
return {["set-beacons"] = set_beacons, ["resolve-conflicts"] = resolve_conflicts, ["light-up-beacons"] = light_up_beacons}
