
// Copyright (C) 2026  EDF
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#pragma once
#include "StOpt/core/grids/InterpolatorSpectral.h"
#include "StOpt/core/grids/LinearInterpolatorSpectral.h"
#include "StOpt/core/grids/LegendreInterpolatorSpectral.h"


///  \brief Cast interpolators
namespace StOpt
{
template<typename TT, typename T>
std::shared_ptr<InterpolatorSpectral<TT>>
cloneInterpolator(const InterpolatorSpectral<T>& src)
{
    if (auto p = dynamic_cast<
        const LinearInterpolatorSpectral<T>*>(&src))
    {
      Eigen::Array< TT, Eigen::Dynamic,1> val = p->getValues().template cast<TT>();
      return std::make_shared<LinearInterpolatorSpectral<TT>>(static_cast<const FullGrid *>(p->getGrid()),val);
    }
    if ( auto p = dynamic_cast<
        const LegendreInterpolatorSpectral<T>*>(&src))
    {
        std::shared_ptr<InterpolatorSpectral<TT>>  ret = std::make_shared<  LegendreInterpolatorSpectral<TT>>(p->getSpectral().template cast<TT>(),
                                                                                                              p->getFuncBaseExp(),
                                                                                                              p->getMin(),
                                                                                                              p->getMax());
        ret->setGrid(p->getGrid());
        return ret;
        
    }

    throw std::runtime_error("Unknown interpolator type");
}
}
