#!/bin/bash

# omarchy:summary=Cycle focused Hyprland monitor scaling through 1x, 1.25x, 1.6x, 2x, 3x, and 4x
# omarchy:args=[--reverse]
# omarchy:examples=omarchy-hyprland-monitor-scaling-cycle | omarchy-hyprland-monitor-scaling-cycle --reverse

CURRENT_SCALE=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true) | .scale')

SCALES=(1 1.25 1.6 2 3 4)

# Find the index of the scale closest to the current one (Hyprland may
# snap fractional scales to nearby values, so we can't match exactly).
CURRENT_IDX=$(awk -v s="$CURRENT_SCALE" -v list="${SCALES[*]}" 'BEGIN {
  n = split(list, arr, " ")
  best = 0; best_diff = 1e9
  for (i = 1; i <= n; i++) {
    d = s - arr[i]; if (d < 0) d = -d
    if (d < best_diff) { best_diff = d; best = i - 1 }
  }
  print best
}')

if [[ $1 == "--reverse" ]]; then
  NEW_IDX=$(( (CURRENT_IDX - 1 + ${#SCALES[@]}) % ${#SCALES[@]} ))
else
  NEW_IDX=$(( (CURRENT_IDX + 1) % ${#SCALES[@]} ))
fi

exec omarchy-hyprland-monitor-scaling-set "${SCALES[$NEW_IDX]}"
