local function cd_to() {
    setopt localoptions pipefail no_aliases 2> /dev/null
    local dir=$1
    if [[ -z "$dir" ]]; then
        zle redisplay
        return 0
    fi
    zle push-line # Clear buffer. Auto-restored on next prompt.
    cd $dir
    zle accept-line
    local ret=$?
    unset dir # ensure this doesn't end up appearing in prompt expansion
    zle reset-prompt
}

local function get_entries() {
    ls ~/Workspace/ > /dev/null # Ensure the workspace is mounted, slightly ugly hack
    file_type=$1
    if [ ! -d "$XDG_CONFIG_HOME/fzgo/entries" ]; then
        return
    fi
    for entry in "$XDG_CONFIG_HOME/fzgo/entries/$file_type"/*; do
        if [ -L "$entry" ]; then
            entry=$(realpath "$entry")
        fi
        if [ -d "$entry" ]; then
            echo $entry
            fd . "$entry" -t "$file_type"
        else
            cat "$entry"
        fi
    done
}

function fzgo_update_cache() {
    # Check if the directory exists and it contains entries
    if [ ! -d "$XDG_CONFIG_HOME/fzgo/entries" ]; then
        return
    fi

    for file_type_dir in "$XDG_CONFIG_HOME/fzgo/entries"/*(N); do
        mkdir -p "$XDG_CACHE_HOME/fzgo/entries"
        for entry in "$file_type_dir"/*; do
            file_type=$(basename "$file_type_dir")
            mkdir -p "$XDG_CACHE_HOME/fzgo/entries/$file_type"
            if [ -L "$entry" ]; then
                entry=$(realpath "$entry")
            fi
            if [ -d "$entry" ]; then
                out_file="$(echo "$entry" | sed 's\/\|\g')"
                out_file="$XDG_CACHE_HOME/fzgo/entries/$file_type/$out_file"
                mkdir -p "$(dirname $out_file)"
                fd . "$entry" -t "$file_type" > "$out_file"
            fi
        done
    done
}


local function find_global() {
    file_type=$1
    FZGO_RECENTS=$XDG_CACHE_HOME/fzgo/recent_paths_$file_type
    mkdir -p $(dirname $FZGO_RECENTS)
    touch $FZGO_RECENTS
    FZGO_RECENT_ENTRIES=$(cat $FZGO_RECENTS)
    FZGO_ENTRIES=$(get_entries $file_type)
    file="$(echo $FZGO_RECENT_ENTRIES'\n'$FZGO_ENTRIES | awk '!x[$0]++' | fzgo)"
    echo "$file" | cat - $FZGO_RECENTS | awk '!x[$0]++' > /tmp/fzgo_paths_recents_$file_type && mv /tmp/fzgo_paths_recents_$file_type $FZGO_RECENTS
    echo "$file"
}

local function find_local() {
    fd . -t $1 | fzgo
}

local function open_path() {
    file_type=$1
    search_fn=$2
    selection="$($search_fn $file_type)"
    if [ "$selection" = "" ]; then
        zle reset-prompt
        return
    fi
    if [ "$LBUFFER" = "" ]; then
        if [ "$file_type" = "d" ]; then
            cd_to "$selection"
        else
            selection=$(realpath "$selection")
            parent=$(dirname "$selection")
            cd_to "$parent"
            $EDITOR "$selection"
        fi
    else
        emulate -L zsh
        zle -I
        LBUFFER="${LBUFFER}\"$selection\""
        zle reset-prompt
    fi
}

function fzgo_global_d() { open_path d find_global }
function fzgo_global_f() { open_path f find_global }
function fzgo_local_f() { open_path f find_local }

function fzgo_register_key() {
    zle -N $2
    bindkey "^$1" $2
    bindkey -M emacs "^$1" $2
    bindkey -M vicmd "^$1" $2
    bindkey -M viins "^$1" $2
}
