#ifndef KIT_UNIVAL_H
#define KIT_UNIVAL_H

#include "string_builder.h"
#include "input_stream.h"

#ifdef __cplusplus
extern "C" {
#endif

enum {
  KIT_UV_NONE,
  KIT_UV_BOOLEAN,
  KIT_UV_INTEGER,
  KIT_UV_FLOAT,
  KIT_UV_STRING,
  KIT_UV_BYTES,
  KIT_UV_ARRAY,
  KIT_UV_COMPOSITE,
};

enum {
  KIT_UV_ENCODE_BZIP  = 1,
  KIT_UV_PRINT_PRETTY = 1,
  KIT_UV_PRINT_JSON   = 2,
};

typedef struct kit_unival_ kit_unival_t;

typedef KIT_DA(kit_unival_t) kit_da_unival_t;
typedef KIT_DA(u8) kit_uv_bytes_t;

struct kit_unival_ {
  i8 type;
  union {
    i8                boolean;
    i64               integer;
    f64               float_;
    kit_str_builder_t string;
    kit_uv_bytes_t    bytes;
    kit_da_unival_t   array;
    struct {
      kit_da_unival_t keys;
      kit_da_unival_t values;
    } composite;
  };
};

typedef struct {
  s32           status;
  kit_unival_t *value;
} kit_uv_decode_result_t;

kit_uv_decode_result_t kit_uv_decode(kit_is_handle_t  is,
                                     kit_allocator_t *alloc);

typedef struct {
  s32            status;
  kit_uv_bytes_t value;
} kit_uv_encode_result_t;

kit_uv_encode_result_t kit_uv_encode(kit_unival_t    *value,
                                     kit_allocator_t *alloc,
                                     i32              flags);

kit_uv_decode_result_t kit_uv_parse(kit_is_handle_t  is,
                                    kit_allocator_t *alloc);

kit_uv_decode_result_t kit_uv_parse_json(kit_is_handle_t  is,
                                         kit_allocator_t *alloc);

typedef struct {
  s32               status;
  kit_str_builder_t value;
} kit_uv_print_result_t;

kit_uv_print_result_t kit_uv_print(kit_unival_t    *value,
                                   kit_allocator_t *alloc, i32 flags);

void kit_uv_destroy(kit_unival_t *value);

#ifdef __cplusplus
}
#endif

#endif