# Description:
#   Helper script to apply color schemes.
#   - Parse and validate parameters.
#   - Provide useful aliases and functions.
#   Should be sourced, not executed directly.

[ "$(basename "$0")" = "apply-color-helper" ] && {
     echo "This script is meant to be sourced, not executed directly."
     exit 1
}

[ -z "$1" ] && exit 1
palette="$1"
[ -z "$2" ] && exit 1
colorName="$2"
[ -z "$3" ] && exit 1
colorHex="$3"

# remove leading '#' if present
if [[ $colorHex == \#* ]]; then
    colorHex="${colorHex#\#}"
fi

# check if hex
if ! [[ $colorHex =~ ^[0-9A-Fa-f]{6}$ ]]; then
    log_error "Invalid color hex: $colorHex"
    exit 1
fi

function log_error {
    printf "\033[0;31mError:\033[0m $1\n" >&2
}

function log_info {
    printf "\033[0;32mInfo:\033[0m $1\n" >&2
}

function color_ansi {
    colorHex="$1"

    local r=$((16#${colorHex:0:2}))
    local g=$((16#${colorHex:2:2}))
    local b=$((16#${colorHex:4:2}))

    # 24-bit true color ANSI escape code
    printf "\033[38;2;%d;%d;%dm" $r $g $b
}

function log_success {
    log_info "Applied palette \033[1;34m${palette}\033[0m with primary color $(color_ansi $colorHex)${colorName} (#${colorHex})\033[0m to \033[1;34m$1\033[0m"
}

# relative path
path=$(dirname "$(readlink -f "$0")")