"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const opn = require('opn');
const path = require("path");
const deploy_1 = require("../../lib/speedray/deploy");
const Task = require('../../ember-cli/lib/models/task');
const LiveReload = require('../../lib/speedray/livereload');
exports.default = Task.extend({
    run: function (runTaskOptions, rebuildDoneCb) {
        const JarTask = require('./jar').default;
        const project = this.cliProject;
        const packageOptions = require(path.resolve(project.root, 'package.json'));
        return new Promise((resolve, reject) => {
            const jarTask = new JarTask({
                cliProject: project,
                ui: this.ui
            });
            let self = this;
            if (runTaskOptions.watch) {
                let firsttime = true;
                let livereload = new LiveReload({
                    portletName: packageOptions.name,
                    ui: self.ui
                });
                livereload.start();
                jarTask.run(runTaskOptions, function rebuildDone(stats) {
                    self.ui.writeLine('\nupdated jar\n');
                    deploy_1.deploy(project, runTaskOptions).subscribe(results => {
                        if (firsttime) {
                            firsttime = false;
                            opn('http://localhost:8080/');
                        }
                        if (rebuildDoneCb) {
                            rebuildDoneCb(results);
                        }
                        else {
                            self.ui.writeLine('\n' + results + '\n');
                        }
                        livereload.done(stats);
                    }, error => {
                        self.ui.writeError('\nAn error occured during the deployment:\n' + (error));
                        reject(error);
                    });
                }).catch((error) => {
                    reject(error);
                });
            }
            else {
                jarTask.run(runTaskOptions).then(() => {
                    deploy_1.deploy(project, runTaskOptions).subscribe(results => {
                        self.ui.writeLine(results);
                        resolve(results);
                    }, error => {
                        self.ui.writeError('\nAn error occured during the deployment:\n' + (error));
                        reject(error);
                    });
                }).catch((error) => {
                    reject(error);
                });
            }
        });
    }
});
//# sourceMappingURL=/home/ddavis/Projects/xtivia/speedray/speedray-cli/tasks/speedray/deploy.js.map