// =====================================================================================================================
//  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_TRANSFORMS_H
#define FENNEC_LANG_DETAIL_TYPE_TRANSFORMS_H

#include <fennec/lang/types.h>
#include <fennec/lang/detail/_type_traits.h>

namespace fennec::detail
{
	template<typename TypeT> struct _add_pointer            : type_identity<TypeT*> {};
	template<typename TypeT> struct _add_pointer<TypeT&>    : type_identity<TypeT*> {};
	template<typename TypeT> struct _remove_pointer         : type_identity<TypeT> {};
	template<typename TypeT> struct _remove_pointer<TypeT*> : type_identity<TypeT> {};

	template<typename TypeT> struct _add_const                 : type_identity<const TypeT> {};
	template<typename TypeT> struct _remove_const              : type_identity<TypeT> {};
	template<typename TypeT> struct _remove_const<const TypeT> : type_identity<TypeT> {};

	template<typename TypeT> struct _add_volatile                    : type_identity<volatile TypeT> {};
	template<typename TypeT> struct _remove_volatile                 : type_identity<TypeT> {};
	template<typename TypeT> struct _remove_volatile<volatile TypeT> : type_identity<TypeT> {};

	template<typename TypeT> struct _add_cv                          : _add_const<typename _add_volatile<TypeT>::type> {};
	template<typename TypeT> struct _remove_cv                       : type_identity<TypeT> {};
	template<typename TypeT> struct _remove_cv<const          TypeT> : type_identity<TypeT> {};
	template<typename TypeT> struct _remove_cv<volatile       TypeT> : type_identity<TypeT> {};
	template<typename TypeT> struct _remove_cv<const volatile TypeT> : type_identity<TypeT> {};

	template<typename TypeT>
	struct _decay_selector : conditional_t<_is_const<const TypeT>{}, _remove_cv<TypeT>, _add_pointer<TypeT>>  {};

	template<typename TypeT> requires requires { typename TypeT::decay_t; }
	struct _decay_selector<TypeT> : type_identity<typename TypeT::decay_t> {};

	template<typename TypeT, size_t N>
	struct _decay_selector<TypeT[N]> : type_identity<TypeT*> {};

	template<typename TypeT>
	struct _decay_selector<TypeT[]> : type_identity<TypeT*> {};

	template<typename TypeT> struct _decay {
		using type = typename _decay_selector<TypeT>::type;
	};

	template<typename TypeT> struct _decay<TypeT&> {
		using type = typename _decay_selector<TypeT>::type;
	};

	template<typename TypeT> struct _decay<TypeT&&> {
		using type = typename _decay_selector<TypeT>::type;
	};

	template<typename TypeT, typename = void>
	struct _add_lvalue_reference {
		using type = TypeT;
	};

	template<typename TypeT>
	struct _add_lvalue_reference<TypeT, void_t<TypeT&>> {
		using type = TypeT&;
	};


	template<typename TypeT, typename = void>
	struct _add_rvalue_reference {
		using type = TypeT;
	};

	template<typename TypeT>
	struct _add_rvalue_reference<TypeT, void_t<TypeT&&>> {
		using type = TypeT&&;
	};

}

#endif // FENNEC_LANG_DETAIL_TYPE_TRANSFORMS_H
