const SAMPLE_STATE_CREATE = 'create';
const SAMPLE_STATE_USE = 'use';
const SAMPLE_STATE_TRANSFORM = 'transform';

class MCProcessActionsComponentController {
    /*@ngInject*/
    constructor(selectItems, $stateParams) {
        this.selectItems = selectItems;
        this.projectId = $stateParams.project_id;

        this.state = {
            createSampleName: '',
            process: null,
            items: [
                {
                    action: 'create',
                    kind: 'sample',
                    files: [],
                    samples: [],
                }
            ],
        };
    }

    $onChanges(changes) {
        if (changes.process) {
            this.state.process = angular.copy(changes.process.currentValue);
            console.log('process', this.state.process);
            if (this.state.process.samples.length || this.state.process.files.length) {
                this.state.items = [];
                this.buildExistingActions();
            }
        }
    }

    buildExistingActions() {
        this.buildExistingSamplesActions();
        this.buildExistingFilesActions();
    }

    buildExistingSamplesActions() {
        // Map the in/out direction for the samples. Samples will either appear once with a direction of
        // in (use sample) or out (create sample). If a sample appears twice then one of the version has
        // a in direction and the other an our direction. These are transformed samples. The code below
        // is for consolidating samples to a particular set of directions.
        let sampleStates = {};
        this.state.process.samples.forEach(s => {
            if (!_.has(sampleStates, s.sample_id)) {
                sampleStates[s.sample_id] = {
                    action: s.direction === 'out' ? SAMPLE_STATE_CREATE : SAMPLE_STATE_USE,
                    name: s.name,
                };
            } else {
                // We have already seen this sample. If a sample appears twice then it must be
                // a sample that is transformed.
                sampleStates[s.sample_id].direction = SAMPLE_STATE_TRANSFORM;
            }
        });

        // Now that we have all sample directions fill out the items list to contain these
        // entries.
        _.values(sampleStates).forEach(s => {
            this.state.items.push({
                action: s.action,
                kind: 'sample',
                samples: [s],
                files: [],
            });
        });
    }

    buildExistingFilesActions() {
        this.state.process.files.forEach(f => {
            this.state.items.push({
                action: this.mapFileDirectionToAction(f.direction),
                kind: 'file',
                samples: [],
                files: [f],
            });
        });
    }

    mapFileDirectionToAction(direction) {
        switch (direction) {
            case 'in':
                return 'use';
            case 'out':
                return 'create';
            default:
                // direction should be blank, default to use
                return 'use';
        }
    }

    addNewAction() {
        this.state.items.push({
            action: 'create',
            kind: 'sample',
            item: '',
            files: [],
            samples: [],
        });
    }

    delete(index) {
        if (index === 0) {
            this.state.items[0].action = 'create';
            this.state.items[0].kind = 'sample';
            this.state.items[0].files.length = 0;
            this.state.items[0].samples = [];
        } else {
            this.state.items.splice(index, 1);
        }
    }

    addCreateSample(item) {
        if (this.state.createSampleName !== '') {
            item.samples.push({name: angular.copy(this.state.createSampleName)});
            this.state.createSampleName = '';
        }
    }

    selectFiles(item) {
        this.selectItems.fileTree(true).then(selected => {
            console.log('selected files =', selected.files);
            item.files = [];
            selected.files.forEach(file => {
                item.files.push(file);
            });
        });
    }

    selectSamples(item) {
        this.selectItems.samplesFromProject(this.projectId).then(selected => {
            item.samples = [];
            selected.samples.forEach(sample => {
                sample.versions.filter(s => s.selected).forEach(s => {
                    // Name in versions is the process name, we want the sample name
                    s.name = sample.name;
                    item.samples.push(s);
                });
            });
        });
    }

}

angular.module('materialscommons').component('mcProcessActions', {
    controller: MCProcessActionsComponentController,
    template: require('./process-actions.html'),
    bindings: {
        process: '<',
    }
});
@@ -1,119 +1,4 @@
-const SAMPLE_STATE_CREATE = 'create';%0Aconst SAMPLE_STATE_USE = 'use';%0Aconst SAMPLE_STATE_TRANSFORM = 'transform';%0A%0A
 clas
@@ -1679,46 +1679,24 @@
 ' ? 
-SAMPLE_STATE_CREATE : SAMPLE_STATE_USE
+'create' : 'use'
 ,%0A  
@@ -1957,20 +1957,17 @@
 id%5D.
-dire
+a
 ction = 
 SAMP
@@ -1966,30 +1966,19 @@
 n = 
-SAMPLE_STATE_TRANSFORM
+'transform'
 ;%0A  

Remove unneeded constant definitions