text: /**
   GPII Settings Component
  
   Copyright 2013 OCAD University
  
   Licensed under the New BSD license. You may not use this file except in
   compliance with this License.
  
   You may obtain a copy of the License at
   https://github.com/gpii/universal/LICENSE.txt
  */
  
  (function () {
  
      "use strict";
  
      var fluid = require("infusion");
      var gpii = fluid.registerNamespace("gpii");
      fluid.registerNamespace("gpii.request.flowManager");
  
      gpii.request.flowManager.onSettings = function (token, handler, device) {
          try {
              device = JSON.parse(device);
          } catch (ex) {
              handler.requestProxy.events.onError.fire({
                  isError: true,
                  message: "Cloud based flow manager requires device information."
              });
              return;
          } finally {
              handler.device = device;
              handler.userToken = token;
              handler.events.onUserListener.fire(token);
          }
      };
  
      gpii.request.flowManager.processMatch = function (match) {
          var settings = {};
          fluid.each(match, function processSolution(solution) {
              if (!solution.settingsHandlers) {
                  return;
              }
              fluid.each(solution.settingsHandlers,
                  function findSettings(settingsHandler) {
                      if (!settingsHandler.settings) {
                          return;
                      }
                      settings[solution.id] = settingsHandler.settings;
                  }
              );
          });
          return settings;
      };
  
      gpii.request.flowManager.getSettings = function (event, match) {
          gpii.request.flowManager.logAndNotify(
              "Flow manager matched the following settings/solutions: ", event,
              gpii.request.flowManager.processMatch)(match);
      };
  
      // TODO: This component is in an absurd namespace
      fluid.defaults("kettle.requests.request.handler.settings", {
          gradeNames: ["fluid.littleComponent", "gpii.request.flowManager.token", "autoInit"],
          invokers: {
              onMatchHandler: {
                  funcName: "gpii.request.flowManager.getSettings",
                  args: ["{requestProxy}.events.onSuccess", "{arguments}.0"]
              },
              handle: {
                  funcName: "gpii.request.flowManager.onSettings",
                  args: ["{request}.req.params.token", "{that}", "{request}.req.params.device"],
                  dynamic: true
              }
          },
          listeners: {
              onPreferences: {
                  listener: "{that}.events.onReadyToMatch.fire",
                  args: [
                      "{arguments}.0",
                      "{that}.device"
                  ]
              },
              onMatch: "{that}.onMatchHandler"
          }
      });
  
  })();
  