#ifndef _ABSTRACTION_HELPERS_HPP_
#define _ABSTRACTION_HELPERS_HPP_

#include <alib/tuple>
#include <alib/array>
#include <alib/typeinfo>

#include <abstraction/OperationAbstraction.hpp>
#include <abstraction/ValueHolderInterface.hpp>

namespace abstraction {

namespace detail {

template < class ... ParamTypes, class F, class Tuple, std::size_t... I >
constexpr decltype ( auto ) apply_impl ( F && f, Tuple && t, std::index_sequence < I ... > ) {
	return std::invoke ( std::forward < F > ( f ), abstraction::retrieveValue < ParamTypes > ( std::get < I > ( std::forward < Tuple > ( t ) ).first->getProxyAbstraction ( ), std::get < I > ( std::forward < Tuple > ( t ) ).second ) ... );
}

}  // namespace detail

template < class ... ParamTypes, class F, class Tuple >
constexpr decltype ( auto ) apply ( F && f, Tuple && t ) {
	return detail::apply_impl < ParamTypes ... > ( std::forward < F > ( f ), std::forward < Tuple > ( t ), std::make_index_sequence < sizeof ... ( ParamTypes ) > { } );
}

template < class ... Params >
static ext::type_index paramType ( unsigned index ) {
	ext::type_index res ( typeid ( void ) );

	auto lambda = [ & ] ( auto I ) {
		res = ext::type_index ( typeid ( std::tuple_element_t < decltype ( I )::value, std::tuple < Params ... > > ) );
	};

	ext::constexpr_switch < sizeof ... ( Params ) > ( index, lambda );

	return res;
}

template < class ... Params >
ext::set < abstraction::ParamQualifiers::ParamQualifier > paramTypeQualifiers ( unsigned index ) {
	ext::set < abstraction::ParamQualifiers::ParamQualifier > res;

	auto lambda = [ & ] ( auto I ) {
		res = abstraction::ParamQualifiers::paramQualifiers < std::tuple_element_t < decltype ( I )::value, std::tuple < Params ... > > > ( );
	};

	ext::constexpr_switch < sizeof ... ( Params ) > ( index, lambda );

	return res;
}

} /* namespace abstraction */

#endif // _ABSTRACTION_HELPERS_HPP_
