# On web SDL is provided by emsdk itself
if(NOT PSEMEK_PLATFORM_WEB)
	find_package(SDL2 REQUIRED)
endif()

file(GLOB_RECURSE PSEMEK_SDL2_HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "include/*.hpp")
file(GLOB_RECURSE PSEMEK_SDL2_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "source/*.cpp")

set(PSEMEK_SDL2_PLATFORM_SOURCES)

if(APPLE)
	file(GLOB_RECURSE PSEMEK_SDL2_PLATFORM_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "platform/apple/*.m")
endif()

psemek_add_library(psemek-sdl2 ${PSEMEK_SDL2_HEADERS} ${PSEMEK_SDL2_SOURCES} ${PSEMEK_SDL2_PLATFORM_SOURCES})
target_include_directories(psemek-sdl2 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(psemek-sdl2 PUBLIC psemek-log psemek-util psemek-audio psemek-app)
if(NOT PSEMEK_PLATFORM_WEB)
	target_link_libraries(psemek-sdl2 PUBLIC SDL2)
endif()

if(PSEMEK_GRAPHICS_API STREQUAL OPENGL)
	target_link_libraries(psemek-sdl2 PUBLIC psemek-gfx)
elseif(PSEMEK_GRAPHICS_API STREQUAL WEBGPU)
	target_link_libraries(psemek-sdl2
		PUBLIC
			psemek-wgpu
	)

	if(APPLE)
		# TODO: shouldn't these come from SDL2 dependencies?
		find_library(FOUNDATION_FRAMEWORK Foundation REQUIRED)
		find_library(METAL_FRAMEWORK Metal REQUIRED)
		find_library(QUARTZCORE_FRAMEWORK QuartzCore REQUIRED)

		target_link_libraries(psemek-sdl2
			PRIVATE
				${FOUNDATION_FRAMEWORK}
				${METAL_FRAMEWORK}
				${QUARTZCORE_FRAMEWORK}
		)
	endif()
endif()
