#!/usr/bin/env bash

# The accent palette, for the shell scripts that need it.
#
# Sourced, not run:
#
#   source "$(dirname "$0")/panama-palette"
#   accent_hex orchid dark      # -> c099ff
#   hex_to_rgb c099ff           # -> 192, 153, 255
#
# Why this exists: the same eight accents were written out by hand in five
# places -- Theme.qml through ThemeProfileModel.js, hypr/looks.lua,
# panama-theme-apps and panama-lock -- and panama-theme-apps said so in a
# comment admitting "there is no shared source between QML and a shell script".
# Adding a ninth accent meant editing five files, and the one most likely to be
# missed was the lock screen, which fails silently: the machine locks with a
# stale color and nothing says why.
#
# config/palette.json is now that shared source. QML still carries the table in
# ThemeProfileModel.js, because a .js module imported into QML cannot read a
# file -- but the two are checked against each other by
# tests/quickshell/palette-contract, so drift is a failing test rather than a
# lock screen in last season's colour.

# The palette lives beside the shell config. Resolved from this script's own
# location so it is correct wherever the repository is checked out.
PANAMA_PALETTE="${PANAMA_PALETTE:-$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/.." && pwd)/config/palette.json}"

# The hex for one accent in one scheme, without the leading '#'.
#
# Falls back to blue for an unknown or omitted name, matching Theme.qml's own
# accentPair fallback: a caller that only ever knew about the scheme still gets
# a real accent rather than an empty string reaching sed.
accent_hex() {
    local name="${1:-}" scheme="${2:-dark}" key value
    [[ "$scheme" == "light" ]] && key="light" || key="dark"

    value="$(jq -r --arg n "$name" --arg k "$key" \
        '.accents[$n][$k] // empty' "$PANAMA_PALETTE" 2>/dev/null)"
    if [[ -z "$value" ]]; then
        value="$(jq -r --arg k "$key" \
            '.accents[.default][$k] // empty' "$PANAMA_PALETTE" 2>/dev/null)"
    fi
    # Last resort, for a palette file that is missing or unreadable. Shipping
    # blue is better than shipping an empty string into a config generator.
    [[ -n "$value" ]] || value="$([[ "$key" == light ]] && echo 2e7de9 || echo 82aaff)"
    printf '%s' "$value"
}

# The second half of the pair, for the two-stop gradient the window border uses.
accent_secondary_hex() {
    local name="${1:-}" scheme="${2:-dark}" key value
    [[ "$scheme" == "light" ]] && key="lightSecondary" || key="darkSecondary"

    value="$(jq -r --arg n "$name" --arg k "$key" \
        '.accents[$n][$k] // empty' "$PANAMA_PALETTE" 2>/dev/null)"
    if [[ -z "$value" ]]; then
        value="$(jq -r --arg k "$key" \
            '.accents[.default][$k] // empty' "$PANAMA_PALETTE" 2>/dev/null)"
    fi
    [[ -n "$value" ]] || value="$([[ "$key" == lightSecondary ]] && echo 9854f1 || echo b172b0)"
    printf '%s' "$value"
}

# GNOME's accent-color is a FIXED enum of nine names -- there is no hex to
# match, only a nearest member to pick -- so palette.json records which one
# each Panama accent maps to. libadwaita applications are told this, so
# choosing an accent here recolours Files and Papers too.
accent_gnome() {
    local name="${1:-}" value
    value="$(jq -r --arg n "$name" '.accents[$n].gnome // empty' "$PANAMA_PALETTE" 2>/dev/null)"
    [[ -n "$value" ]] || value="$(jq -r '.accents[.default].gnome // empty' "$PANAMA_PALETTE" 2>/dev/null)"
    [[ -n "$value" ]] || value="blue"
    printf '%s' "$value"
}

# hyprlock wants "R, G, B" decimal, not hex.
hex_to_rgb() {
    local hex="${1#\#}"
    printf '%d, %d, %d' "0x${hex:0:2}" "0x${hex:2:2}" "0x${hex:4:2}"
}
