package util

import (
	"fmt"
	"strconv"
)

// ParseHex parses a hexadecimal value from the hash string
// This implementation is shared between engine and jdenticon packages for consistency
func ParseHex(hash string, startPosition, octets int) (int, error) {
	// Handle negative indices (count from end like JavaScript)
	if startPosition < 0 {
		startPosition = len(hash) + startPosition
	}
	
	// Ensure we don't go out of bounds
	if startPosition < 0 || startPosition >= len(hash) {
		return 0, fmt.Errorf("parseHex: position %d out of bounds for hash length %d", startPosition, len(hash))
	}
	
	// If octets is 0 or negative, read from startPosition to end (like JavaScript default)
	end := len(hash)
	if octets > 0 {
		end = startPosition + octets
		if end > len(hash) {
			end = len(hash)
		}
	}
	
	// Extract substring and parse as hexadecimal
	substr := hash[startPosition:end]
	if len(substr) == 0 {
		return 0, fmt.Errorf("parseHex: empty substring at position %d", startPosition)
	}
	
	result, err := strconv.ParseInt(substr, 16, 64)
	if err != nil {
		return 0, fmt.Errorf("parseHex: failed to parse hex '%s' at position %d: %w", substr, startPosition, err)
	}
	
	return int(result), nil
}

// IsValidHash checks if a hash string is valid for jdenticon generation
// This implementation is shared between engine and jdenticon packages for consistency
func IsValidHash(hash string) bool {
	if len(hash) < 11 {
		return false
	}
	
	// Check if all characters are valid hexadecimal
	for _, r := range hash {
		if !((r >= '0' && r <= '9') || (r >= 'a' && r <= 'f') || (r >= 'A' && r <= 'F')) {
			return false
		}
	}
	
	return true
}