-------------------------------------------------------------------------------------
-- Title: MSBT Suppressions
-- Author: Mik
-------------------------------------------------------------------------------------

-- Create module namespace.
MSBTSuppressions = {};

-------------------------------------------------------------------------------------
-- Private variables.
-------------------------------------------------------------------------------------

-- Holds a table of the suppressions hashed by their rarest words.
local suppressionsHash = {};


-------------------------------------------------------------------------------------
-- Suppression functions.
-------------------------------------------------------------------------------------

-- **********************************************************************************
-- Figures out the rarest word used in each suppression and hashes the suppression
-- against it.
-- **********************************************************************************
function MSBTSuppressions.HashSuppressions()
 -- Erase the suppressions hash table.
 MSBTTableRecycler.EraseTable(suppressionsHash);

 -- Create a table to hold the number of times each word has appeared.
 local wordCounts = {};

 -- Loop through all of the suppression entries.
 for _, suppressionSettings in pairs(MSBTProfiles.CurrentProfile.Suppressions) do
  -- Make sure the suppression is enabled.
  if (not suppressionSettings.Disabled) then
   -- Loop through the words in the suppression pattern and total them.
   for word in string.gmatch(suppressionSettings.SearchPattern, "[%a]+") do
    wordCounts[word] = (wordCounts[word] or 0) + 1;
   end
  end
 end

 -- Loop through all of the suppression entries.
 for _, suppressionSettings in pairs(MSBTProfiles.CurrentProfile.Suppressions) do
  -- Make sure the suppression is enabled.
  if (not suppressionSettings.Disabled) then
   -- Hold rarest word and the number of times it was seen.
   local rarestWord, timesSeen;

   -- Loop through the words in the suppression pattern.
   for word in string.gmatch(suppressionSettings.SearchPattern, "[%a]+") do
    -- Check if this word is more rare than the current one and update it.
    if (not timesSeen or wordCounts[word] < timesSeen) then
     timesSeen = wordCounts[word];
     rarestWord = word;
    end
   end

   -- Make sure the rarest word is valid.
   if (rarestWord) then
    -- Create an entry in the hash table for the rarest word if there is not already one.
    if (not suppressionsHash[rarestWord]) then
     suppressionsHash[rarestWord] = {};
    end

    -- Add the suppression search pattern to the hash table.
    table.insert(suppressionsHash[rarestWord], suppressionSettings.SearchPattern);
   end -- Rarest word is valid.
  end -- Suppression settings enabled.
 end -- Loop through suppression entries.
end


-- **********************************************************************************
-- Returns whether or not the passed text matches one of the suppression patterns.
-- **********************************************************************************
function MSBTSuppressions.IsSuppressed(text)
 -- Strip out all color codes.
 text = string.gsub(text, "|c%x%x%x%x%x%x%x%x(.-)|r", "%1");

 -- Loop through the words in the passed text.
 for word in string.gmatch(text, "%a+") do
  -- Check if the word is one of the hashed rare words.
  if (suppressionsHash[word]) then
   -- Loop through the suppression patterns hashed against the word.
   for _, suppressionPattern in pairs(suppressionsHash[word]) do
    -- Check if the text suppression pattern matches the text and return true.
    if (string.find(text, suppressionPattern)) then
     return true;
    end
   end
  end
 end

 return false;
end