/*jshint node:true*/

var path = require('path');
var InRepoAddon = require('ember-cli/blueprints/in-repo-addon/index');
var stringUtil = require('ember-cli-string-utils');
var fs = require('fs-extra');

module.exports = {
  description: 'Creates an Engine within the current repository.',

  locals: function(options) {
    var entity = options.entity;
    var rawName = entity.name;
    var name = stringUtil.dasherize(rawName);

    return {
      name: name,
      modulePrefix: name,
      hasLazyFlag: typeof options.lazy !== 'undefined',
      isLazy: !!options.lazy
    };
  },

  availableOptions: [
    {
      name: 'type',
      type: [ 'routable', 'routeless' ],
      default: 'routable',
      aliases: [
        { 'routable': 'routable' },
        { 'routeless': 'routeless' }
      ]
    },
    {
      name: 'lazy',
      type: Boolean,
      description: 'Whether this Engine should load lazily or not'
    }
  ],

  _setupRouteBlueprint: function() {
    this.routeBlueprint = this.lookupBlueprint('route');
    this.shouldTouchRouter = this.routeBlueprint.shouldTouchRouter;
    this.shouldEntityTouchRouter = this.routeBlueprint.shouldEntityTouchRouter;
  },

  install: function(options) {
    this.options = options;
    var superResult = this._super.install.apply(this, arguments);
    this._setupRouteBlueprint();
    return superResult;
  },

  uninstall: function(options) {
    this.options = options;
    var superResult = this._super.uninstall.apply(this, arguments);
    this._setupRouteBlueprint();
    return superResult;
  },

  filesPath: function() {
    var type = this.options && this.options.type || 'routable';
    return path.join(this.path, type + '-files');
  },

  // TODO: supportsAddon normally calls `files` which means that `filesPath`
  // gets called before options get set. Likely needs to be fixed upstream.
  supportsAddon: function() {
    return false;
  },

  beforeInstall: InRepoAddon.beforeInstall,

  afterInstall: function(options) {
    options.identifier = 'mount';
    InRepoAddon.afterInstall.call(this, options);
    this.routeBlueprint.afterInstall.call(this, options);
  },

  afterUninstall: function(options) {
    options.identifier = 'mount';
    this.routeBlueprint.afterUninstall.call(this, options);
  },

  _generatePackageJson: function(options, isInstall) {
    InRepoAddon._generatePackageJson.apply(this, arguments);
  },

  _readJsonSync(path) {
    return fs.readJsonSync(path);
  },

  _writeFileSync(path, content) {
    fs.writeFileSync(path, content);
  }
};

@@ -170,55 +170,56 @@
 ');%0A
-var fs = require('fs-extra');%0A%0Amodule.exports =
+%0Amodule.exports = Object.assign(%7B%7D, InRepoAddon,
  %7B%0A 
@@ -2399,149 +2399,9 @@
 %0A  %7D
-,%0A%0A  _readJsonSync(path) %7B%0A    return fs.readJsonSync(path);%0A  %7D,%0A%0A  _writeFileSync(path, content) %7B%0A    fs.writeFileSync(path, content);%0A  %7D
 %0A%7D
+)
 ;%0A

Extend from EmberCLI's in-repo-addon blueprint