// 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/archetypes/bar_chart.fbs".

#include "bar_chart.hpp"

#include "../collection_adapter_builtins.hpp"

namespace rerun::archetypes {
    BarChart BarChart::clear_fields() {
        auto archetype = BarChart();
        archetype.values = ComponentBatch::empty<rerun::components::TensorData>(Descriptor_values)
                               .value_or_throw();
        archetype.color =
            ComponentBatch::empty<rerun::components::Color>(Descriptor_color).value_or_throw();
        archetype.abscissa =
            ComponentBatch::empty<rerun::components::TensorData>(Descriptor_abscissa)
                .value_or_throw();
        archetype.widths =
            ComponentBatch::empty<rerun::components::Length>(Descriptor_widths).value_or_throw();
        return archetype;
    }

    Collection<ComponentColumn> BarChart::columns(const Collection<uint32_t>& lengths_) {
        std::vector<ComponentColumn> columns;
        columns.reserve(4);
        if (values.has_value()) {
            columns.push_back(values.value().partitioned(lengths_).value_or_throw());
        }
        if (color.has_value()) {
            columns.push_back(color.value().partitioned(lengths_).value_or_throw());
        }
        if (abscissa.has_value()) {
            columns.push_back(abscissa.value().partitioned(lengths_).value_or_throw());
        }
        if (widths.has_value()) {
            columns.push_back(widths.value().partitioned(lengths_).value_or_throw());
        }
        return columns;
    }

    Collection<ComponentColumn> BarChart::columns() {
        if (values.has_value()) {
            return columns(std::vector<uint32_t>(values.value().length(), 1));
        }
        if (color.has_value()) {
            return columns(std::vector<uint32_t>(color.value().length(), 1));
        }
        if (abscissa.has_value()) {
            return columns(std::vector<uint32_t>(abscissa.value().length(), 1));
        }
        if (widths.has_value()) {
            return columns(std::vector<uint32_t>(widths.value().length(), 1));
        }
        return Collection<ComponentColumn>();
    }
} // namespace rerun::archetypes

namespace rerun {

    Result<Collection<ComponentBatch>> AsComponents<archetypes::BarChart>::as_batches(
        const archetypes::BarChart& archetype
    ) {
        using namespace archetypes;
        std::vector<ComponentBatch> cells;
        cells.reserve(4);

        if (archetype.values.has_value()) {
            cells.push_back(archetype.values.value());
        }
        if (archetype.color.has_value()) {
            cells.push_back(archetype.color.value());
        }
        if (archetype.abscissa.has_value()) {
            cells.push_back(archetype.abscissa.value());
        }
        if (archetype.widths.has_value()) {
            cells.push_back(archetype.widths.value());
        }

        return rerun::take_ownership(std::move(cells));
    }
} // namespace rerun
