
// Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto.  Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#include <OgnArrayGetSizeDatabase.h>
#include <omni/graph/core/ogn/ComputeHelpers.h>
#include <omni/graph/core/ogn/string.h>
#include <omni/graph/core/ogn/Types.h>

#include <algorithm>

namespace omni {
namespace graph {
namespace nodes {

class OgnArrayGetSize
{
public:
    static bool compute(OgnArrayGetSizeDatabase& db)
    {
        try
        {
            db.outputs.size() = int(db.inputs.array().size());
            return true;
        }
        catch (ogn::compute::InputError const &error)
        {
            db.logError(error.what());
        }
        return false;
    }

    static void onConnectionTypeResolve(const NodeObj& node){
        auto const inputArray = node.iNode->getAttributeByToken(node, inputs::array.token());
        auto const inputArrayType = inputArray.iAttribute->getResolvedType(inputArray);

        if (inputArrayType.baseType != BaseDataType::eUnknown)
        {
            std::array<AttributeObj, 1> attrs { inputArray };
            // all should have the same tuple count
            std::array<uint8_t, 1> tupleCounts {
                inputArrayType.componentCount
            };
            // value type can not be an array because we don't support arrays-of-arrays
            std::array<uint8_t, 1> arrayDepths {
                1
            };
            std::array<AttributeRole, 1> rolesBuf {
                inputArrayType.role
            };
            node.iNode->resolvePartiallyCoupledAttributes(node, attrs.data(), tupleCounts.data(),
                                                        arrayDepths.data(), rolesBuf.data(), attrs.size());
        }
    }
};

REGISTER_OGN_NODE()

} // namespace nodes
} // namespace graph
} // namespace omni