// DO NOT EDIT! This file was auto-generated by crates/build/re_types_builder/src/codegen/cpp/mod.rs
// Based on "crates/store/re_sdk_types/definitions/rerun/datatypes/tensor_data.fbs".

#include "tensor_data.hpp"

#include "tensor_buffer.hpp"

#include <arrow/builder.h>
#include <arrow/type_fwd.h>

namespace rerun::datatypes {}

namespace rerun {
    const std::shared_ptr<arrow::DataType>& Loggable<datatypes::TensorData>::arrow_datatype() {
        static const auto datatype = arrow::struct_({
            arrow::field("shape", arrow::list(arrow::field("item", arrow::uint64(), false)), false),
            arrow::field("names", arrow::list(arrow::field("item", arrow::utf8(), false)), true),
            arrow::field(
                "buffer",
                Loggable<rerun::datatypes::TensorBuffer>::arrow_datatype(),
                true
            ),
        });
        return datatype;
    }

    Result<std::shared_ptr<arrow::Array>> Loggable<datatypes::TensorData>::to_arrow(
        const datatypes::TensorData* instances, size_t num_instances
    ) {
        // TODO(andreas): Allow configuring the memory pool.
        arrow::MemoryPool* pool = arrow::default_memory_pool();
        auto datatype = arrow_datatype();

        ARROW_ASSIGN_OR_RAISE(auto builder, arrow::MakeBuilder(datatype, pool))
        if (instances && num_instances > 0) {
            RR_RETURN_NOT_OK(Loggable<datatypes::TensorData>::fill_arrow_array_builder(
                static_cast<arrow::StructBuilder*>(builder.get()),
                instances,
                num_instances
            ));
        }
        std::shared_ptr<arrow::Array> array;
        ARROW_RETURN_NOT_OK(builder->Finish(&array));
        return array;
    }

    rerun::Error Loggable<datatypes::TensorData>::fill_arrow_array_builder(
        arrow::StructBuilder* builder, const datatypes::TensorData* elements, size_t num_elements
    ) {
        if (builder == nullptr) {
            return rerun::Error(ErrorCode::UnexpectedNullArgument, "Passed array builder is null.");
        }
        if (elements == nullptr) {
            return rerun::Error(
                ErrorCode::UnexpectedNullArgument,
                "Cannot serialize null pointer to arrow array."
            );
        }

        {
            auto field_builder = static_cast<arrow::ListBuilder*>(builder->field_builder(0));
            auto value_builder = static_cast<arrow::UInt64Builder*>(field_builder->value_builder());
            ARROW_RETURN_NOT_OK(field_builder->Reserve(static_cast<int64_t>(num_elements)));
            ARROW_RETURN_NOT_OK(value_builder->Reserve(static_cast<int64_t>(num_elements * 2)));

            for (size_t elem_idx = 0; elem_idx < num_elements; elem_idx += 1) {
                const auto& element = elements[elem_idx];
                ARROW_RETURN_NOT_OK(field_builder->Append());
                ARROW_RETURN_NOT_OK(value_builder->AppendValues(
                    element.shape.data(),
                    static_cast<int64_t>(element.shape.size()),
                    nullptr
                ));
            }
        }
        {
            auto field_builder = static_cast<arrow::ListBuilder*>(builder->field_builder(1));
            auto value_builder = static_cast<arrow::StringBuilder*>(field_builder->value_builder());
            ARROW_RETURN_NOT_OK(field_builder->Reserve(static_cast<int64_t>(num_elements)));
            ARROW_RETURN_NOT_OK(value_builder->Reserve(static_cast<int64_t>(num_elements * 1)));

            for (size_t elem_idx = 0; elem_idx < num_elements; elem_idx += 1) {
                const auto& element = elements[elem_idx];
                if (element.names.has_value()) {
                    ARROW_RETURN_NOT_OK(field_builder->Append());
                    for (size_t item_idx = 0; item_idx < element.names.value().size();
                         item_idx += 1) {
                        ARROW_RETURN_NOT_OK(value_builder->Append(element.names.value()[item_idx]));
                    }
                } else {
                    ARROW_RETURN_NOT_OK(field_builder->AppendNull());
                }
            }
        }
        {
            auto field_builder = static_cast<arrow::DenseUnionBuilder*>(builder->field_builder(2));
            ARROW_RETURN_NOT_OK(field_builder->Reserve(static_cast<int64_t>(num_elements)));
            for (size_t elem_idx = 0; elem_idx < num_elements; elem_idx += 1) {
                RR_RETURN_NOT_OK(Loggable<rerun::datatypes::TensorBuffer>::fill_arrow_array_builder(
                    field_builder,
                    &elements[elem_idx].buffer,
                    1
                ));
            }
        }
        ARROW_RETURN_NOT_OK(builder->AppendValues(static_cast<int64_t>(num_elements), nullptr));

        return Error::ok();
    }
} // namespace rerun
