local _M = {}
local common = require "interactionmanip.common"

---
-- Massages the interactions that will be sent to the agent in the initial load.
-- Takes each one and removes the *heavy* things like 'events', 'readers', 'analysisTimes' and  'steps'
-- Note that since we lose that info, we need to precompute some stuff in order to have certain things 
-- available nonetheless, so may want to call NormalizeInteractions before calling this function.
--
-- @tparam table interactions an array of interactions, beware because it will be mutated
--
function _M.StripUnwantedMembers (interactions, excludeAttributes)
	--LogDebug("[INTERACTIONMAINIP.StripUnwantedMembers] 1------> ENTRA. interactions: '%s'.", json.encode(interactions))
	--LogDebug("[INTERACTIONMAINIP.StripUnwantedMembers] 1-1------>  excludeAttributes: '%s'.", json.encode(excludeAttributes or {}))

	local function CanStrip (attrName)
		if not excludeAttributes then
			--LogDebug("[INTERACTIONMAINIP.StripUnwantedMembers]    2 Exclude attribute '%s' of interaction.", attrName)
			return true
		end
		if excludeAttributes[attrName] then
			--LogDebug("[INTERACTIONMAINIP.StripUnwantedMembers]    3 NOT Exclude attribute '%s' of interaction.", attrName)
			return false
		end
		--LogDebug("[INTERACTIONMAINIP.StripUnwantedMembers]    4 Exclude attribute '%s' of interaction.", attrName)
		return true
	end
	local attributesToExclude = {
		analysisTimes = true,
		events        = true,
		indices       = true,
		readers       = true,
		steps         = true
	}
	for _, interaction in ipairs(interactions) do
		for attrName in pairs(attributesToExclude) do
			if CanStrip(attrName) then
				interaction[attrName] = nil
			end
		end
	end
	--LogDebug("[INTERACTIONMAINIP.StripUnwantedMembers] 5------> SALE. interactions: '%s'.", json.encode(interactions))
end

---
-- Play 'fill in the blanks' with the interactions. Precomputing stuff, normalizing data which should be
-- there but isn't, and on and on...
--
-- @tparam table interactions an array of interactions, beware because it will be mutated
--
function _M.NormalizeInteractions (interactions)
	--LogDebug("[INTERACTIONMAINIP.NormalizeInteractions] 1------> ENTRA. interactions: '%s'.", json.encode(interactions))
	for _, interaction in ipairs(interactions) do
		common.updateInteraction(interaction)
	end
	--LogDebug("[INTERACTIONMAINIP.NormalizeInteractions] 2------> SALE. interactions: '%s'.", json.encode(interactions))
end

function _M.RequireChannelsModules ()
	--LogDebug("[INTERACTIONMAINIP.RequireChannelsModules] ------> ENTRA")
	local channels = {
		APPSTORE   = true,
		CALL       = true,
		CHAT       = true,
		FACEBOOK   = true,
		GOOGLEPLAY = true,
		INSTAGRAM  = true,
		MAIL       = true,
		RCS        = true,
		SMS        = true,
		TWITTER    = true,
		VOICEMAIL  = true,
		WHATSAPP   = true,
		WEBCONTACT = true,
		YOUTUBE    = true
	}
	for channel in pairs(channels) do
		--LogDebug("[INTERACTIONMAINIP.RequireChannelsModules]   Voy por el require del channel: %s", channel)
		local moduleInstance = common.require_module({type = channel})
		--LogDebug("[INTERACTIONMAINIP.RequireChannelsModules]     %s moduleInstance: %s", channel, moduleInstance)
		channels[channel] = moduleInstance
	end
	--LogDebug("[INTERACTIONMAINIP.RequireChannelsModules] ------> SALE")
	return channels
end

return _M
