#!/bin/sh

_fzf() {
  fzf --layout reverse --exact --cycle --height 50% --info hidden --prompt ' ' --border rounded --color light "$@"
}

# f-cargo-test() {
#     local test
#     test="$(rust-list-tests | _fzf)"
#     [[ -n "$test" ]] || return
#     echo cargo test "$test"
#     print -s "cargo test $test" # zsh
#     cargo test "$test"
# }
#
# f-cat() {
#     bat --style="header,grid" "$(fd . "$1" | _fzf)"
# }

# f-docker-exec() {
#     local container
#     container="$(
#         docker ps --format '{{.ID}}\t{{.Names}}\t{{.Image}}' |
#             _fzf |
#             awk '{print $1}'
#     )"
#     docker exec -it "$container" bash
# }

f-git-branch() {
    git-branch-by-date | rg -v '^z-' | _fzf | awk '{print $1}'
}

f-git-checkout-branch() {
    git checkout --quiet "$(f-git-branch)"
}

f-git-checkout-commit() {
    git checkout --quiet "$(_f-git-log)"
}

f-git-diff() {
    git diff "$(_f-git-log)"
}

f-git-rebase() {
    git rebase "$(f-git-branch)"
}

f-git-rebase-interactive() {
    git rebase --interactive "$(_f-git-log)"
}

f-git-reset() {
    git reset "$(_f-git-log)"
}

f-git-reset-hard() {
    git reset --hard "$(_f-git-log)"
}

f-git-revert() {
    git revert --no-edit "$(_f-git-log)"
}

f-git-cherry-pick() {
    git cherry-pick "$(f-git-branch)"
}

f-git-log-branch() {
    git log --stat --decorate "$(f-git-branch)"
}

fopen() {
  app="$(command fd -d 1 '.+\.app' /Applications /System/Applications /System/Applications/Utilities \
        | rg -r '$2$1' '^(.*/([^/]+)\.app)/?$' \
        | fzf '--with-nth' 1 '-d' / \
        | sed -E 's,[^/]+/,/,')"
  [ -n "$app" ] && open "$app"
}

f-wormhole-open() {
    local project
    project="$(command fd -d 1 . ~/src ~/src/temporalio |
        rg -r '$2$1' '^(.*/([^/]+))/?$' |
        _fzf '--with-nth' 1 '-d' / |
        sed -E 's,[^/]+/,/,')"
    echo "$project"
    [ -n "$project" ] && wormhole-open "$project"
}

f_preview_jq() {
  # https://github.com/pawelduda/fzf-live-repl
  file="$1"
  echo | fzf --print-query --preview "jq '{q}' < '$file'" --preview-window "top:95%"
}

f-preview-regex-python() {
    local input="$1"
    echo | _fzf --print-query --preview-window up --preview "python -c \"
import re
print('Input: $input\n')
m = re.match({q}, '$input')
print(m.groups() if m else '<no match>')\""
}

f-preview-regex-sed() {
    local input="$1"
    echo | _fzf \
        --print-query \
        --preview-window up \
        --preview "printf \"$input\n\n\n─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n\n\n\"; echo \"$input\" | sed -E 's/\x1b\[[0-9;]*[mK]//g' | sed -E '{q}'"
}

f-rg2() {
    local INITIAL_QUERY=""
    local RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
    FZF_DEFAULT_COMMAND="$RG_PREFIX '$INITIAL_QUERY'" \
        fzf --bind "change:reload:$RG_PREFIX {q} || true" \
        --ansi --query "$INITIAL_QUERY" \
        --height=100% --layout=reverse
}

fzf-set-environment-variables() {
    export FZF_DEFAULT_COMMAND="fd --type file --color=always"
    export FZF_DEFAULT_OPTS="--ansi"
}

f-tmux() {
    if [ -n "$1" ]; then
        local next_window="$1"
    else
        local next_window
        next_window="$(
            tmux list-panes -a -F '#{session_name},#{window_index},#{pane_title}' |
                xsv table |
                _fzf --exact |
                awk '{print $1":"$2}'
        )"
    fi
    tmux display-message -p -F '#{session_name}:#{window_index}' >/tmp/tmux-last-window
    tmux switch-client -t "$next_window"
}

tmux-back() {
    f-tmux "$(cat /tmp/tmux-last-window)"
}
