// =====================================================================================================================
//  fennec, a free and open source game engine
//  Copyright © 2025 - 2026  Medusa Slockbower
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================

#ifndef FENNEC_LANG_DETAIL_TYPE_SEQUENCES_H
#define FENNEC_LANG_DETAIL_TYPE_SEQUENCES_H

#include <fennec/math/common.h>
#include <fennec/lang/type_transforms.h>

namespace fennec::detail
{
	template<typename...TypesT> struct _type_sequence {};

	template<typename FirstT, typename... RestT> struct _first_element : type_identity<FirstT> {};



// fennec::nth_element =================================================================================================

	template<size_t n, size_t i, typename...TypesT> struct _nth_element;

	template<size_t n, size_t i> struct _nth_element<n, i> : type_identity<void> {};

	template<size_t n, size_t i, typename HeadT, typename...RestT>
	struct _nth_element<n, i, HeadT, RestT...> : conditional<
		n == i, HeadT,
		typename _nth_element<n, i + 1, RestT...>::type
	> {};



// fennec::max_element_size ============================================================================================

	template<size_t, typename...> struct _max_element_size;

	template<size_t M, typename HeadT>
	struct _max_element_size<M, HeadT>
		: integral_constant<size_t, fennec::max(M, sizeof(HeadT))> {
	};

	template<size_t M, typename HeadT, typename...RestT>
	struct _max_element_size<M, HeadT, RestT...>
		: _max_element_size<fennec::max(M, sizeof(HeadT)), RestT...> {
	};



// fennec::find_element ================================================================================================

	template<size_t N, typename, typename...> struct _find_element;

	template<size_t N, typename FindT, typename HeadT>
	struct _find_element<N, FindT, HeadT> : integral_constant<size_t, is_same_v<FindT, HeadT> ? N : N + 1> {};

	template<size_t N, typename FindT, typename HeadT, typename...RestT>
	struct _find_element<N, FindT, HeadT, RestT...>
		: conditional_t<is_same_v<FindT, HeadT>, integral_constant<size_t, N>, _find_element<N + 1, FindT, RestT...>> {};


// fennec::search_element ==============================================================================================

	template<template<typename> typename, typename...> struct _search_element;

	template<template<typename> typename SearchT> struct _search_element<SearchT> : type_identity<void> {};

	template<template<typename> typename SearchT, typename HeadT, typename...RestT> requires(SearchT<HeadT>{})
	struct _search_element<SearchT, HeadT, RestT...>
		: conditional_t<SearchT<HeadT>{}, type_identity<HeadT>, _search_element<SearchT, RestT...>> {
	};

	template<template<typename, typename...> typename, typename, typename...> struct _search_element_args;

	template<template<typename, typename...> typename SearchT, typename...ArgsT>
	struct _search_element_args<SearchT, _type_sequence<ArgsT...>> : type_identity<void> {};

	template<template<typename, typename...> typename SearchT, typename HeadT, typename...RestT, typename...ArgsT>
	struct _search_element_args<SearchT, _type_sequence<ArgsT...>, HeadT, RestT...>
		: conditional_t<SearchT<HeadT, ArgsT...>{}, type_identity<HeadT>, _search_element_args<SearchT, _type_sequence<ArgsT...>, RestT...>> {};
}

#endif // FENNEC_LANG_DETAIL_TYPE_SEQUENCES_H
