/*
 * (C) Copyright 2026- ECMWF and individual contributors.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

///
/// @file Grib2Mars.cc
/// @brief Implementation of the public Grib2Mars conversion API.
///
/// The implementation forwards each supported conversion through the rule
/// layer and wraps the result in the common API error-handling policy.
///


#include "Grib2Mars.h"

#include "eckit/config/LocalConfiguration.h"
#include "eckit/exception/Exceptions.h"
#include "metkit/grib2mars/mappings/mappings.h"
#include "metkit/mars/MarsRequest.h"

// dictionary access traits
#include "metkit/grib2mars/utils/dictionary_traits/dictaccess_eckit_configuration.h"
#include "metkit/grib2mars/utils/dictionary_traits/dictaccess_mars_request.h"
#include "metkit/grib2mars/utils/dictionary_traits/dictionary_access_traits.h"

// Error handling utilities
#include "Grib2MarsApiErrorHandling.h"

namespace metkit::grib2mars {

// -----------------------------------------------------------------------------
// Grib2Mars construction
// -----------------------------------------------------------------------------

/// @brief Default construct a Grib2Mars converter.
Grib2Mars::Grib2Mars() {}

/// @brief Convert an `eckit::LocalConfiguration` request.
template <>
Grib2MarsResult<eckit::LocalConfiguration> Grib2Mars::convert<eckit::LocalConfiguration>(
    const metkit::codes::CodesHandle& grib) {

    using metkit::grib2mars::utils::exceptions::withGrib2MarsApiErrorHandling;

    return withGrib2MarsApiErrorHandling<Grib2MarsResult<eckit::LocalConfiguration>>(
        "Grib2Mars::convert<eckit::LocalConfiguration>",
        [&]() { return rules::convertAll<eckit::LocalConfiguration>(grib); }, Here());
}

/// @brief Convert a `metkit::mars::MarsRequest` request.
template <>
Grib2MarsResult<metkit::mars::MarsRequest> Grib2Mars::convert<metkit::mars::MarsRequest>(
    const metkit::codes::CodesHandle& grib) {

    using metkit::grib2mars::utils::exceptions::withGrib2MarsApiErrorHandling;

    return withGrib2MarsApiErrorHandling<Grib2MarsResult<metkit::mars::MarsRequest>>(
        "Grib2Mars::convert<metkit::mars::MarsRequest>",
        [&]() { return rules::convertAll<metkit::mars::MarsRequest>(grib); }, Here());
}

}  // namespace metkit::grib2mars
