local remap = vim.keymap.set
local opts  = { noremap=true, silent=true, unique=true }

-- Comment lines
require('mini.comment').setup()
-- Diff hunks
require('mini.diff').setup({
  view = {
    style = 'sign',
    signs = { add = '+', change = '~', delete = '-' },
  },
  mappings = {
    textobject = 'gh',
    goto_prev  = '[c',
    goto_next  = ']c',
    goto_first = '<Leader>hf',
    goto_last  = '<Leader>hl',
    apply      = '<Leader>hs',
    reset      = '<Leader>hr',
  },
})
-- File System
require('mini.files').setup({
  mappings = {
    close       = 'q'      ,
    go_in       = 'e'      ,
    go_in_plus  = '<Right>',
    go_out      = 'n'      ,
    go_out_plus = '<Left>' ,
    reset       = '<BS>'   ,
    show_help   = 'g?'     ,
    synchronize = '='      ,
    trim_left   = ''       ,
    trim_right  = ''       ,
  }
})
-- Vertical jumps/movement
require('mini.jump2d').setup({
  allowed_lines = {
    blank         = true,
    cursor_before = true,
    cursor_at     = true,
    cursor_after  = true,
    fold          = true,
  },
  allowed_windows = {
    current     = true ,
    not_current = false,
  },
  labels   = 'arstgmneioqwfpbjluyzxcdvkh',
  mappings = { start_jumping = '' },
  silent   = true,
  view     = {
    dim           = true,
    n_steps_ahead = 0,
  },
})
-- Surround actions/vim-surround replacement
require('mini.surround').setup({})
-- Highlight and remove white space
require('mini.trailspace').setup({})

-- Key mappings for all mini modules we use
remap('n', '-', function()
  return require('mini.files').open(vim.api.nvim_buf_get_name(0))
end, opts)
remap({ 'n', 'o', 'x' }, 'gS', function()
  return require('mini.jump2d').start({
    allowed_lines = {
      blank         = false,
      cursor_before = true ,
      cursor_at     = false,
      cursor_after  = false,
      fold          = false,
    }
  })
end, opts)
remap({ 'n', 'o', 'x' }, 'gs', function()
  return require('mini.jump2d').start({
    allowed_lines = {
      blank         = false,
      cursor_before = false,
      cursor_at     = false,
      cursor_after  = true ,
      fold          = false,
    }
  })
end, opts)
remap({ 'n', 'o', 'x' }, 'gl', function()
  local mini_jump2d = require('mini.jump2d')
  local line_start  = mini_jump2d.builtin_opts.line_start

  return mini_jump2d.start(line_start)
end, opts)
-- Replacement for f, F, t, T motions/Horizontal movement
remap({ 'n', 'o', 'x' }, 'f', function()
  local mini_jump2d = require('mini.jump2d')
  local single_char = mini_jump2d.builtin_opts.single_character

  single_char.allowed_lines = {
    blank         = false,
    cursor_before = false,
    cursor_at     = true ,
    cursor_after  = false,
    fold          = false,
  }

  return mini_jump2d.start(single_char)
end, opts)
remap('n', '<Leader>ws', function()
  return require('mini.trailspace').trim()
end, opts)
