/* jshint node: true */
'use strict';

var fs = require( 'fs' );
var mergeTrees = require( 'broccoli-merge-trees' );
var path = require( 'path' );
var writeFile = require( 'broccoli-file-creator' );

module.exports = {

    /**
     * Name of addon
     *
     * @type {String}
     */
    name: 'ember-forge-ui',

    /**
     * Name of companion ember-forge-ui addon
     *
     * @type {?String}
     */
    emberForgeUiCompanionAddonName: null,

    /**
     * Set name of ember-forge-ui companion addon to local context if exists
     *
     * @param {String} environment
     * @param {Object} appConfig
     * @returns {undefined}
     */
    config: function( environment, appConfig ) {
        if ( appConfig[this.name] && appConfig[this.name].addonName ) {
            this.emberForgeUiCompanionAddonName = appConfig[this.name].addonName;
        }
    },

    /**
     * Return templates if ember-forge-ui companion addon is installed, default ones if not
     *
     * @param {Object} tree
     * @returns {Object}
     */
    treeForAddonTemplates: function() {
        if ( this.emberForgeUiCompanionAddonName ) {
            return this.treeGenerator(
                path.join( this.nodeModulesPath, this.emberForgeUiCompanionAddonName, 'addon', 'templates' )
            );

        } else {
            var componentTemplateTrees = [];
            var files = fs.readdirSync( path.join( this.nodeModulesPath, this.name, 'addon', 'components' ) );
            var placeholderContentPath = path.join(
                this.nodeModulesPath,
                this.name,
                'addon',
                'templates',
                'components'
            ) + path.sep + 'placeholder-content.hbs';
            var placeholderContent = require( placeholderContentPath );

            files.forEach( function( element ) {
                componentTemplateTrees.push(
                    writeFile( '/components/' + element.replace( '.js', '' ) + '.hbs', placeholderContent() )
                );
            });

            return mergeTrees( componentTemplateTrees );
        }
    }
};

@@ -2088,21 +2088,1093 @@
 ;%0A        %7D%0A
+    %7D,%0A%0A    /**%0A     * Add initializer exports to the app tree for each component that has been redefined by the companion addon%0A     *%0A     * @param %7BObject%7D tree%0A     * @returns %7BObject%7D%0A     */%0A    treeForApp: function( tree ) %7B%0A        var emberForgeUiCompanionAddonName = this.emberForgeUiCompanionAddonName;%0A        var files = fs.readdirSync(%0A            path.join( this.nodeModulesPath, emberForgeUiCompanionAddonName, 'addon', 'components' )%0A        );%0A        var initializerTrees = %5B%5D;%0A%0A        files.forEach( function( element ) %7B%0A            var content = %22export %7B default, initialize %7D from '%22;%0A            content += emberForgeUiCompanionAddonName + '/initializers/' + emberForgeUiCompanionAddonName;%0A            content += '-' + element.substring( 0, element.length - 3 ) + %22';%22;%0A%0A            initializerTrees.push(%0A                writeFile( 'initializers/' + emberForgeUiCompanionAddonName + '-' + element, content )%0A            );%0A        %7D);%0A%0A        return mergeTrees(%0A            tree ? initializerTrees.concat( tree ) : initializerTrees%0A        );%0A
     %7D%0A%7D;%0A

Add treeForApp() to export initializers into app tree