"use strict";
var command_run_1 = require('./command.run');
var Immutable = require('immutable');
var Rx = require('rx');
var file = require("./file.utils");
var path_1 = require("path");
var reporter_resolve_1 = require("./reporter.resolve");
var _ = require('../lodash.custom');
(function (InitConfigFileErrorTypes) {
    InitConfigFileErrorTypes[InitConfigFileErrorTypes["InitInputFileExists"] = "InitInputFileExists"] = "InitInputFileExists";
    InitConfigFileErrorTypes[InitConfigFileErrorTypes["InitInputFileTypeNotSupported"] = "InitInputFileTypeNotSupported"] = "InitInputFileTypeNotSupported";
})(exports.InitConfigFileErrorTypes || (exports.InitConfigFileErrorTypes = {}));
var InitConfigFileErrorTypes = exports.InitConfigFileErrorTypes;
(function (InitConfigFileTypes) {
    InitConfigFileTypes[InitConfigFileTypes["yaml"] = "yaml"] = "yaml";
    InitConfigFileTypes[InitConfigFileTypes["js"] = "js"] = "js";
    InitConfigFileTypes[InitConfigFileTypes["json"] = "json"] = "json";
    InitConfigFileTypes[InitConfigFileTypes["cbfile"] = "cbfile"] = "cbfile";
})(exports.InitConfigFileTypes || (exports.InitConfigFileTypes = {}));
var InitConfigFileTypes = exports.InitConfigFileTypes;
function execute(trigger) {
    var config = trigger.config, reporter = trigger.reporter;
    var templateDir = path_1.join(__dirname, '..', 'templates');
    var maybeExistingFileInputs = (_a = {},
        _a[InitConfigFileTypes.yaml] = 'crossbow.yaml',
        _a[InitConfigFileTypes.js] = 'crossbow.js',
        _a[InitConfigFileTypes.json] = 'crossbow.json',
        _a[InitConfigFileTypes.cbfile] = 'cbfile.js',
        _a
    );
    var outputFileName = maybeExistingFileInputs[config.type];
    if (outputFileName === undefined) {
        var errors_1 = [{
                type: InitConfigFileErrorTypes.InitInputFileTypeNotSupported,
                providedType: config.type,
                supportedTypes: maybeExistingFileInputs
            }];
        if (!config.handoff) {
            reporter({
                type: reporter_resolve_1.ReportTypes.InitInputFileTypeNotSupported,
                data: {
                    error: errors_1[0]
                }
            });
        }
        return Rx.Observable.just({
            setup: {
                existingFilesInCwd: [],
                matchingFiles: [],
                errors: errors_1
            }
        });
    }
    /**
     * Attempt to load existing config files from the CWD
     * @type {ExternalFile[]}
     */
    var existingFilesInCwd = file.readFilesFromDisk(_.values(maybeExistingFileInputs), config.cwd);
    /**
     * Now check if any of the existing files match the one the user
     * is attempting to create.
     *
     * eg:
     *  crossbow init --type js
     * -> crossbow.js already exists in cwd -> error
     *
     * eg:
     *  crossbow init --type yaml
     * -> crossbow.js already exists in cwd, which is ok because they want a .yaml file -> success
     *
     * @type {ExternalFile[]}
     */
    var matchingFiles = existingFilesInCwd
        .filter(function (x) { return x.errors.length === 0; })
        .filter(function (file) { return outputFileName === file.parsed.base; });
    var errors = (function () {
        if (matchingFiles.length) {
            return matchingFiles.map(function (file) {
                return { type: InitConfigFileErrorTypes.InitInputFileExists, file: file };
            });
        }
        return [];
    })();
    // /**
    //  * Allow consumer to handle executions
    //  */
    // if (config.handoff) {
    //     return Rx.Ob{existingFilesInCwd, matchingFiles, errors};
    // }
    if (errors.length) {
        reporter({
            type: reporter_resolve_1.ReportTypes.DuplicateInputFile,
            data: {
                error: errors[0]
            }
        });
        return Rx.Observable.just({
            setup: {
                existingFilesInCwd: existingFilesInCwd,
                matchingFiles: matchingFiles,
                errors: errors
            }
        });
    }
    var templateFilePath = path_1.join(templateDir, outputFileName);
    var outputFilePath = path_1.join(config.cwd, outputFileName);
    var output = {
        existingFilesInCwd: existingFilesInCwd,
        matchingFiles: matchingFiles,
        errors: errors,
        outputFilePath: outputFilePath,
        outputFileName: outputFileName,
        templateFilePath: templateFilePath
    };
    reporter({
        type: reporter_resolve_1.ReportTypes.InputFileCreated,
        data: {
            parsed: path_1.parse(outputFilePath)
        }
    });
    return Rx.Observable.just({ setup: output });
    var _a;
}
function handleIncomingInitCommand(cli, input, config, reporter) {
    return execute({
        shared: new Rx.BehaviorSubject(Immutable.Map({})),
        cli: cli,
        input: input,
        config: config,
        reporter: reporter,
        type: command_run_1.TriggerTypes.command
    });
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = handleIncomingInitCommand;
//# sourceMappingURL=command.init.js.map