-- lx-bundle:true

--- Shared image file detection helpers
-- Keeps supported image extension logic out of the viewer and open flow
-- @module core.image.support

local image_support = {}

local SUPPORTED_EXTENSIONS = {
	bmp = true,
	cur = true,
	gif = true,
	ico = true,
	jfif = true,
	jpeg = true,
	jpg = true,
	pjp = true,
	pjpeg = true,
	png = true,
	qoi = true,
	svg = true,
	tga = true,
	xpm = true,
}

--- Return one normalized supported image extension for a path
-- @param path string|nil file path
-- @return boolean supported true when the extension is supported
-- @return string|nil extension normalized lowercase extension
function image_support.is_supported(path)
	if type(path) ~= "string" or path == "" then
		return false, nil
	end

	-- Pattern breakdown:
	-- '%.'
	--   matches the final literal dot before the extension
	-- '([^.]+)'
	--   captures one or more non-dot chars as the extension
	-- '$'
	--   anchors the capture to the end of the path
	local extension = path:match("%.([^.]+)$")

	if not extension then
		return false, nil
	end

	extension = extension:lower()

	return SUPPORTED_EXTENSIONS[extension] == true, extension
end

return image_support
