// Generated by CoffeeScript 1.9.0
var APIOpts, Adapter, ApiHero, ClientRenderer, Router, Routes, RoutingParams, Singleton, Util, _,
  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  __hasProp = {}.hasOwnProperty;

_ = require('lodash')._;

Util = require('../utils');

Singleton = require('../base_class/Singleton');

Adapter = require('../request_adapters/Adapter');

Routes = require('./routes');

RoutingParams = require('./RoutingParams');

ClientRenderer = require('../client/ClientRenderer');

APIOpts = require('../config/APIOptions');

Adapter = require('../request_adapters/Adapter');

Router = (function(_super) {
  __extends(Router, _super);

  function Router() {
    var ApiHero, client;
    ApiHero = require('../..');
    if ((this.__adapter = new Adapter({
      app: ApiHero.getApp()
    })) == null) {
      throw "Routing Adapter not defined.";
    }
    this.__api_path = this.__adapter.params.app.get('restApiRoot');
    client = new ClientRenderer;
    this.__routes = new Routes(this.__adapter);
    this.intializeRoutes();
    setTimeout(((function(_this) {
      return function() {
        return _this.__client = client.toSource();
      };
    })(this)), 50);
  }

  Router.prototype.getAdapter = function() {
    return this.__adapter;
  };

  Router.prototype.getClient = function() {
    return this.__client;
  };

  Router.prototype.intializeRoutes = function() {
    var operation, route, _i, _len, _ref, _results;
    _ref = ['index', 'create'];
    _results = [];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      operation = _ref[_i];
      this.createRoute(route = new RoutingParams(this.__api_path + "/:collection", operation));
      if (APIOpts.get('debug')) {
        _results.push(logger.log('debug', (route.method.toUpperCase()) + " " + route.path + " -> " + route.operation));
      } else {
        _results.push(void 0);
      }
    }
    return _results;
  };

  Router.prototype.createRoute = function(params) {
    var handler;
    if (!Util.Object.isOfType(params, RoutingParams)) {
      params = new RoutingParams(params.path, params.operation);
    }
    if ((handler = this.__routes.createRoute(params.method, params.path, params.operation)) != null) {
      return this.addRoute(params.path, params.method, handler);
    }
  };

  Router.prototype.addRoute = function(path, method, handler) {
    return this.__adapter.addRoute(path, method, handler);
  };

  return Router;

})(Singleton);

ApiHero = require('../..');

module.exports = Router;
