"use strict";

import inflect from "inflect";

class Router {
  constructor(settings) {
    this.app = settings.app;
    this.controllers = settings.controllers;
    this.loader = settings.loader;
  }

  resource(name) {
    const Controller = this.loader.controllers.get(name);
    const controller = new Controller();

    this._bindRoutes(name, controller);
  }

  _bindRoutes(resource, controller) {
    const singularResource = inflect.singularize(resource);
    const pluralResource = inflect.pluralize(singularResource);

    this._bindRoute(`/${pluralResource}`, `get`, controller.index.bind(controller));
    this._bindRoute(`/${pluralResource}`, `get`, controller.show.bind(controller));
    this._bindRoute(`/${pluralResource}`, `post`, controller.create.bind(controller));
    this._bindRoute(`/${pluralResource}`, `put`, controller.update.bind(controller));
    this._bindRoute(`/${pluralResource}`, `del`, controller.destroy.bind(controller));
  }

  _bindRoute(endpoint, method, action) {
    this.app[method](endpoint, action);
  }

  static map(settings, callback) {
    const router = new Router(settings);

    callback.call(router);
    return router;
  }
}

const restActionMapper = new Map([
    ["index", "get"],
    ["show", "get"],
    ["create", "post"],
    ["update", "put"],
    ["destroy", "delete"]
]);

export default Router;

@@ -40,16 +40,318 @@
 lect%22;%0A%0A
+const restActionMapper = new Map(%5B%0A    %5B%22index%22, %22get%22%5D,%0A    %5B%22show%22, %22get%22%5D,%0A    %5B%22create%22, %22post%22%5D,%0A    %5B%22update%22, %22put%22%5D,%0A    %5B%22destroy%22, %22del%22%5D%0A%5D);%0A%0Aconst restPathMapper = new Map(%5B%0A    %5B%22index%22, %22/%22%5D,%0A    %5B%22show%22, %22/:id%22%5D,%0A    %5B%22create%22, %22/%22%5D,%0A    %5B%22update%22, %22/:id%22%5D,%0A    %5B%22destroy%22, %22/:id%22%5D%0A%5D);%0A%0A
 class Ro
@@ -624,62 +624,143 @@
     
-this._bindRoutes(name, controller);%0A  %7D%0A%0A  _bindRoutes
+for (const %5Baction%5D of restActionMapper) %7B%0A      this._mapControllerAction(name, controller, action);%0A    %7D%0A  %7D%0A%0A  _mapControllerAction
 (res
@@ -776,20 +776,126 @@
 ntroller
+, action
 ) %7B%0A
+    const controllerAction = controller%5Baction%5D;%0A    const method = restActionMapper.get(action);%0A
     cons
@@ -1014,813 +1014,309 @@
 e);%0A
-%0A
     
-this._bindRoute(%60/$%7BpluralResource%7D%60, %60get%60, controller.index.bind(controller));%0A    this._bindRoute(%60/$%7BpluralResource%7D%60, %60get%60, controller.show.bind(controller));%0A    this._bindRoute(%60/$%7BpluralResource%7D%60, %60post%60, controller.create.bind(controller));%0A    this._bindRoute(%60/$%7BpluralResource%7D%60, %60put%60, controller.update.bind(controller));%0A    this._bindRoute(%60/$%7BpluralResource%7D%60, %60del%60, controller.destroy.bind(controller));%0A  %7D%0A%0A  _bindRoute(endpoint, method, action) %7B%0A    this.app%5Bmethod%5D(endpoint, action);%0A  %7D%0A%0A  static map(settings, callback) %7B%0A    const router = new Router(settings);%0A%0A    callback.call(router);%0A    return router;%0A  %7D%0A%7D%0A%0Aconst restActionMapper = new Map(%5B%0A    %5B%22index%22, %22get%22%5D,%0A    %5B%22show%22, %22get%22%5D,%0A    %5B%22create%22, %22post%22%5D,%0A    %5B%22update%22, %22put%22%5D,%0A    %5B%22destroy%22, %22delete%22%5D%0A%5D);
+const pathSegment = restPathMapper.get(action);%0A    const resourcePath = %60/$%7BpluralResource%7D$%7BpathSegment%7D%60;%0A%0A    this.app%5Bmethod%5D(resourcePath, controllerAction);%0A  %7D%0A%0A  static map(settings, callback) %7B%0A    const router = new Router(settings);%0A%0A    callback.call(router);%0A    return router;%0A  %7D%0A%7D
 %0A%0Aex

clean up controller mapping