#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call URLDecode(uCalcBase::Callback cb) {
   // In a real application, this would be a full URL decoding implementation.
   // For this example, we'll just handle spaces (%20) and plus signs (+).
   uCalc::String s = cb.ArgStr(1);
   s.Replace("%20", " ").Replace("+", " ");
   cb.ReturnStr(s.Text());
}

int main() {
   uCalc uc;
   // 1. Define the custom URLDecode function in the uCalc engine
   uc.DefineFunction("URLDecode(s As String) As String", URLDecode);

   // 2. Create the transformer and configure its tokenizer
   {
      uCalc::Transformer t(uc);
      t.Owned(); // Causes t to be released when it goes out of scope
      // Treat '&' as a statement separator to process each pair individually
      t.Tokens().Add("&", TokenType::StatementSep);

      // 3. Define the rule to capture key-value pairs and decode the value
      t.FromTo("{@Alphanumeric:key}={value}", "- {key}: '{@Eval: URLDecode(value)}'");

      // 4. Process a real-world query string
      auto queryString = "name=John%20Doe&role=user+admin&id=123";

      // Use Filter() to get a clean, newline-separated list of the results
      cout << t.Transform(queryString).Matches() << endl;
   }
}