#!/usr/bin/env bash

# Check if an argument is provided
if [[ -z $1 ]]; then
  # echo "Error: Number argument is missing."
  exit 1
fi

# Retrieve the argument value
new_workspace=$1

if [[ $new_workspace == special:* ]]; then
	hyprctl dispatch togglespecialworkspace "${new_workspace:8}"
	exit 0
fi

# Validate if the argument is a number
if ! [[ $new_workspace =~ ^-?[0-9]+$ ]]; then
  # echo "Error: Invalid number argument."
  exit 1
fi

while read -r n a f;do 
	workspace[$a]=$n
	if [[ $f == true ]]; then
		active=$n
	fi
done < <(hyprctl monitors -j | jq -r '.[]|"\(.name) \(.activeWorkspace.id) \(.focused)"')

if [[ ${workspace[$new_workspace]} ]]; then
	hyprctl dispatch swapactiveworkspaces $active ${workspace[$new_workspace]}
else
	hyprctl --batch "dispatch moveworkspacetomonitor $new_workspace $active ; dispatch workspace $new_workspace"
fi