#!/bin/bash

if [[ $# -eq 1 ]]; then
	selected=$1
else
    selected=$(fd --min-depth 1 --max-depth 1 --type d . ~/repos/Rust/ ~/repos/ ~/neorg/University/3rd-semester/ ~/Nextcloud/repos/ ~/Nextcloud/Documents/LaTeX/ ~/Nextcloud/Documents/Typst/ ~/Nextcloud/repos/university/** ~/Nextcloud/repos/university/**/**/ | sk --height 16)
fi

if [[ -z $selected ]]; then
	exit 0
fi

selected_name=$(basename "$selected" | tr . _)
zellij_running=$(pgrep zellij)

if [[ -z $ZELLIJ ]] && [[ -z $zellij_running ]]; then
	# tmux new-session -s "$selected_name" -c "$selected"
    zellij action new-tab -n "$selected_name" -c "$selected"
	exit 0
fi

if ! zellij list-sessions | rg "$selected_name"; then
    zellij action 
fi

if ! tmux has-session -t="$selected_name" 2>/dev/null; then
	tmux new-session -ds "$selected_name" -c "$selected"
fi

tmux switch-client -t "$selected_name"
