//+------------------------------------------------------------------+
//|                                                SymbolMapper.mqh  |
//|               Stateless mapping from a symbol to its currencies  |
//+------------------------------------------------------------------+
#ifndef CAL_SYMBOL_MAPPER_MQH
#define CAL_SYMBOL_MAPPER_MQH

//+------------------------------------------------------------------+
//| CSymbolMapper                                                    |
//| Stateless mapping from a symbol to its currencies. The fallback  |
//| table has priority, the terminal properties are used when the    |
//| table has no entry. The table is a convention that the reader    |
//| must review for the broker in use.                               |
//+------------------------------------------------------------------+
class CSymbolMapper
  {
private:
   //--- helpers
   string            Normalize(const string text) const;                      // trims and upper cases
   bool              AddUnique(string &list[], const string currency) const;  // skips empty and repeated values
public:
   //--- mapping, each method returns the number of currencies found
   int               MapFromTable(const string symbol, const string table, string &currencies[]) const;
   int               MapFromTerminal(const string symbol, string &currencies[]) const;
   int               Map(const string symbol, const string table, string &currencies[]) const;
  };

//+------------------------------------------------------------------+
//| Normalize                                                        |
//| Trims both ends and converts to upper case, so that comparisons  |
//| do not depend on spaces or letter case.                          |
//+------------------------------------------------------------------+
string CSymbolMapper::Normalize(const string text) const
  {
   string value = text;
   StringTrimLeft(value);
   StringTrimRight(value);
   StringToUpper(value);
   return(value);
  }

//+------------------------------------------------------------------+
//| AddUnique                                                        |
//| Appends a currency to the list. Returns false and changes        |
//| nothing for an empty string or a currency already in the list.   |
//+------------------------------------------------------------------+
bool CSymbolMapper::AddUnique(string &list[], const string currency) const
  {
   if(currency == "")
      return(false);
//--- linear search, the list has at most a few entries
   const int total = ArraySize(list);
   for(int i = 0; i < total; i++)
      if(list[i] == currency)
         return(false);
   ArrayResize(list, total + 1);
   list[total] = currency;
   return(true);
  }

//+------------------------------------------------------------------+
//| MapFromTable                                                     |
//| Reads the currencies of a symbol from the fallback table.        |
//| Format: "SYMBOL:CUR1,CUR2;SYMBOL2:CUR". The symbol must match    |
//| exactly after trimming and upper casing, so a broker suffix      |
//| needs its own entry. Returns 0 when the symbol is not listed.    |
//+------------------------------------------------------------------+
int CSymbolMapper::MapFromTable(const string symbol, const string table, string &currencies[]) const
  {
   ArrayResize(currencies, 0);
   const string wanted = Normalize(symbol);
//--- split the table into entries, one per symbol
   string entries[];
   const int count = StringSplit(table, ';', entries);
   for(int i = 0; i < count; i++)
     {
      //--- an entry without a colon or without a symbol name is ignored
      const int colon = StringFind(entries[i], ":");
      if(colon <= 0)
         continue;
      if(Normalize(StringSubstr(entries[i], 0, colon)) != wanted)
         continue;
      //--- the entry matches: split the currency list and stop searching
      string parts[];
      const int n = StringSplit(StringSubstr(entries[i], colon + 1), ',', parts);
      for(int j = 0; j < n; j++)
         AddUnique(currencies, Normalize(parts[j]));
      break;
     }
   return(ArraySize(currencies));
  }

//+------------------------------------------------------------------+
//| MapFromTerminal                                                  |
//| Reads the base currency and the profit currency of the symbol    |
//| from the terminal. For a currency pair these are the two         |
//| currencies. For an index, a metal or a crypto symbol the values  |
//| depend on the broker, so check them or use the fallback table.   |
//+------------------------------------------------------------------+
int CSymbolMapper::MapFromTerminal(const string symbol, string &currencies[]) const
  {
   ArrayResize(currencies, 0);
   AddUnique(currencies, Normalize(SymbolInfoString(symbol, SYMBOL_CURRENCY_BASE)));
   AddUnique(currencies, Normalize(SymbolInfoString(symbol, SYMBOL_CURRENCY_PROFIT)));
   return(ArraySize(currencies));
  }

//+------------------------------------------------------------------+
//| Map                                                              |
//| Maps a symbol to its currencies. The fallback table is tried     |
//| first, then the terminal. Returns 0 when both give nothing.      |
//+------------------------------------------------------------------+
int CSymbolMapper::Map(const string symbol, const string table, string &currencies[]) const
  {
   if(MapFromTable(symbol, table, currencies) > 0)
      return(ArraySize(currencies));
   return(MapFromTerminal(symbol, currencies));
  }

#endif // CAL_SYMBOL_MAPPER_MQH
//+------------------------------------------------------------------+