local wezterm = require("wezterm")

local startup = function(env_var, base_project_module)
	local project = os.getenv(env_var)

	if project == nil then
		wezterm.log_error("Environment variable " .. env_var .. " is not set")
		return
	end

	local status, project_module = pcall(function()
		return require(base_project_module .. "." .. project)
	end)

	if not status then
		wezterm.log_error("Failed to load project config: " .. tostring(project_module))
		return
	end

	local project_startup = project_module.startup

	if project_startup == nil or type(project_startup) ~= "function" then
		wezterm.log_error(
			"Project " .. project .. " has no exported 'startup' function (type is " .. type(project_startup) .. ")"
		)
		return
	end

	project_startup(wezterm)
end

return {
	startup = startup,
}
