#ifndef DK_UTILS_H
#define DK_UTILS_H

#include "dk_common.h"

#define ARRAY_CONCATN(TYPE, N, ...) \
    (TYPE *)concatn(sizeof(TYPE), N, __VA_ARGS__)

#define ARRAY_CONCAT(TYPE, A, An, B, Bn) \
    (TYPE *)concatn(sizeof(TYPE), 2, (const void*)A, An, (const void*)B, Bn)

// void* concat(size_t s, const void* a, size_t a_len, const void* b, size_t b_len);

/**
 * Concates at N arrays of a given type of size S. The variadic arguments
 * must contain N tuples of a pointer and a size_t. If N is 0, a NULL pointer
 * is returned. If N is 1, a copy of the single array is returned.
 * 
 * @param s Size of the array type
 * @param n The number of arrays being concatenated
 * @param ... One or more byte arrays followed by it's size
 * @return An array containing the concatened contents of the N arrays, which size is the sum of the size of the N arrays
 */
void* concatn(size_t s, size_t n, ...);

/**
 * Converts an unsigned long integer to an array of bytes in Big-Endian format.
 * 
 * @param x The integer
 * @param a The array that will contain the integer in byte format
 * @param a_len The length of array `a`, must be of size sizeof(unsigned long)
 * @return The error code for the operation
 */
DK_Result long_to_byte_array(unsigned long x, byte* a, int a_len);

/**
 * Prints a byte array in the format [byte0, byte1, ..., byteN].
 * 
 * @param arr Pointer to the array.
 * @param arr_len Length of the array.
 */
void print_array(byte* arr, size_t arr_len);

/**
 * Convert a hex string to byte array. s should have even length.
 * 
 * @param s even-length hexadecimal string
 * @param slen string length
 * @param buf output buffer
 * @param buflen output buffer length
 */
void hexstr2barray(char* s, size_t slen, byte* buf, size_t buflen);

#endif