"use strict";
var file = require("./file.utils");
var utils = require("./task.utils");
var file_utils_1 = require("./file.utils");
var reporter_resolve_1 = require("./reporter.resolve");
var Rx = require('rx');
var path_1 = require("path");
var config_1 = require("./config");
function createHashes(tasks, trigger) {
    var ifLookups = utils.concatProps(tasks, [], "ifChanged");
    if (!ifLookups.length)
        return Rx.Observable.empty();
    var existingFile = file.readOrCreateJsonFile(path_1.join('.crossbow', 'history.json'), trigger.config.cwd);
    if (!existingFile.data.hashes) {
        existingFile.data.hashes = [];
    }
    return file.hashItems(ifLookups, trigger.config.cwd, existingFile.data.hashes)
        .do(function (hashResults) {
        // Write the hashes to disk
        trigger.config.signalObserver.onNext({
            type: config_1.SignalTypes.FileWrite,
            data: {
                file: existingFile,
                content: JSON.stringify({ hashes: hashResults.output }, null, 2)
            }
        });
    })
        .map(function (hashResults) {
        // Send in the marked hashes to the run context
        // so that matching tasks can be ignored
        return {
            "ifChanged": hashResults.markedHashes
        };
    })
        .take(1)
        .catch(function (e) {
        if (e.code === 'ENOTDIR')
            e.type = file_utils_1.HashDirErrorTypes.HashNotADirectory;
        if (e.code === 'ENOENT')
            e.type = file_utils_1.HashDirErrorTypes.HashPathNotFound;
        trigger.reporter({
            type: reporter_resolve_1.ReportTypes.HashDirError,
            data: {
                error: e,
                cwd: trigger.config.cwd
            }
        });
        return Rx.Observable.just({});
    });
}
exports.createHashes = createHashes;
//# sourceMappingURL=command.run.pre-execution.js.map