'use strict';

const dasherize = require('ember-cli-string-utils').dasherize;
const stringUtils = require('ember-cli-string-utils');

module.exports = {
  description: 'Ember CLI blueprint for initializing a new Glimmer application',

  // filesToRemove: [
  //   'app/styles/.gitkeep',
  //   'app/templates/.gitkeep',
  // ],

  locals(options) {
    let name = options.entity.name;
    let component = componentize(name);
    let className = stringUtils.classify(options.entity.name);

    return { name, className, component };
  },

  fileMapTokens(options) {
    return {
      __component__() { return options.locals.component }
    }
  },

  afterInstall(options) {
    if (options.webComponent) {
      return this._installWebComponentSupport(options);
    }
  },

  _installWebComponentSupport(options) {
    let name = options.entity.name;
    let component = componentize(name);

    let addPackagePromise = this.addPackageToProject('@glimmer/web-component');
    let indexTSPromise = this.insertIntoFile(
      'src/index.ts',
      "import initializeCustomElements from '@glimmer/web-component';",
      { after: "import { ComponentManager, setPropertyDidChange } from '@glimmer/component';\n" }
    ).then(() => {
      return this.insertIntoFile(
        'src/index.ts',
        `initializeCustomElements(app, ['${component}']);\n`
      );
    });

    return Promise.all([
      addPackagePromise,
      indexTSPromise
    ]);
  }
};

// Component names must have at least one dash, so we prefix the component name
// if it doesn't have one. E.g.: `avatar` -> `-avatar`
function componentize(name) {
  let dasherized = dasherize(name);
  return hasDash(dasherized) ? dasherized : `-${dasherized}`;
}

function hasDash(string) {
  return string.indexOf('-') > -1;
}

@@ -1504,11 +1504,11 @@
  we 
-pre
+suf
 fix 
@@ -1528,16 +1528,28 @@
  name%0A//
+ with %60-app%60
  if it d
@@ -1588,15 +1588,18 @@
 -%3E %60
--
 avatar
+-app
 %60%0Afu
@@ -1707,17 +1707,16 @@
 ized : %60
--
 $%7Bdasher
@@ -1720,16 +1720,20 @@
 herized%7D
+-app
 %60;%0A%7D%0A%0Afu

Add an -app suffix if app name doesn't contain a dash