const mendelConfig = require('../../mendel-config');
const CacheClient = require('./cache/client');
const MendelGenerators = require('./generators');
const MendelOutletRegistry = require('./registry/outlet');
const fs = require('fs');
const path = require('path');
const debug = require('debug')('mendel:outlets');
const mkdirp = require('mkdirp');

class MendelOutlets {
    constructor(options) {
        this.config = mendelConfig(options);

        const registry = new MendelOutletRegistry(this.config);
        const client = new CacheClient(this.config, registry);
        const generators = new MendelGenerators(this.config, registry);

        client.on('sync', () => {

            generators.perform();

            const manifest = this.getV1Manifest(generators.doneBundles[0].entries);
            // TODO: mendel-config v2 not parsing output build path
            const dest = path.join(
                this.config.projectRoot, 'build/test.manifest.json'
            );
            mkdirp.sync(path.dirname(dest));
            fs.writeFileSync(
                dest,
                JSON.stringify(manifest, null, 2)
            );

            process.exit(0);

        });
    }

    dataFromItem(item) {
        return {
            id: item.normalizedId,
            deps: Object.keys(item.deps).reduce((newDeps, key) => {
                newDeps[key] = item.deps[key].browser;
                return newDeps;
            }, {}),
            variation: item.variation || this.config.baseConfig.dir,
            source: item.source,
        };
    }

    getV1Manifest(entries) {
        const manifest = {
            indexes: {},
            bundles: [],
        };

        entries.forEach(item => {
            const id = item.normalizedId;

            if (!manifest.indexes[id]) {
                const data = this.dataFromItem(item);
                const newEntry = {
                    variations: [data.variation],
                    file: item.id,
                    id: item.normalizedId,
                    data: [data],
                };

                if (item.entry) newEntry.entry = item.entry;
                manifest.bundles.push(newEntry);
                const index = manifest.bundles.indexOf(newEntry);

                manifest.indexes[id] = index;
                newEntry.index = index;
            } else {
                const index = manifest.indexes[id];
                const existing = manifest.bundles[index];
                const newData = this.dataFromItem(item);

                if (existing.variations.includes(newData.variation)) {
                    return debug(
                        `${existing.file}|${existing.variations}  `,
                        `${item.id}|${item.variation}`,
                        'WARN: normalizedId and variation collision'
                    );
                }
                existing.variations.push(newData.variation);
                existing.data.push(newData);
            }
        });

        return manifest;
    }
}

module.exports = MendelOutlets;

@@ -1290,163 +1290,44 @@
 ps: 
-Object.keys(item.deps).reduce((newDeps, key) =%3E %7B%0A                newDeps%5Bkey%5D = item.deps%5Bkey%5D.browser;%0A                return newDeps;%0A            %7D, %7B%7D)
+item.deps,%0A            file: item.id
 ,%0A  
@@ -1793,36 +1793,29 @@
     
-variations: %5Bdata.variation%5D
+id: item.normalizedId
 ,%0A  
@@ -1828,37 +1828,35 @@
             
-file: item.id
+index: null
 ,%0A          
@@ -1865,37 +1865,44 @@
         
-id: item.normalizedId
+variations: %5Bdata.variation%5D
 ,%0A      

Fix Manifest creation after change of deps