#!/usr/bin/env bash
PANEL="$1"

# Atomic lock: flock acquires exclusively or exits immediately.
# Released automatically when the process exits (no trap needed).
exec 9>"/tmp/eww_panel_toggle.lock"
flock -n 9 || exit 0

CURRENT=$(eww get active-panel 2>/dev/null | tr -d '"')

if [ "$CURRENT" = "$PANEL" ]; then
  eww update active-panel=""
  eww close popup 2>/dev/null
else
  eww update active-panel="$PANEL"
  eww close popup 2>/dev/null
  eww-open-on-current-screen popup
fi
