"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
const events_1 = require("events");
const constants_1 = require("../constants");
const helpers_1 = require("../common/helpers");
class LocalBuildService extends events_1.EventEmitter {
    constructor($projectData, $platformService) {
        super();
        this.$projectData = $projectData;
        this.$platformService = $platformService;
    }
    build(platform, platformBuildOptions, platformTemplate) {
        return __awaiter(this, void 0, void 0, function* () {
            this.$projectData.initializeProjectData(platformBuildOptions.projectDir);
            yield this.$platformService.preparePlatform(platform, platformBuildOptions, platformTemplate, this.$projectData, {
                provision: platformBuildOptions.provision,
                sdk: null,
                frameworkPath: null,
                ignoreScripts: false
            });
            const handler = (data) => {
                data.projectDir = platformBuildOptions.projectDir;
                this.emit(constants_1.BUILD_OUTPUT_EVENT_NAME, data);
            };
            platformBuildOptions.buildOutputStdio = "pipe";
            yield helpers_1.attachAwaitDetach(constants_1.BUILD_OUTPUT_EVENT_NAME, this.$platformService, handler, this.$platformService.buildPlatform(platform, platformBuildOptions, this.$projectData));
            return this.$platformService.lastOutputPath(platform, platformBuildOptions, this.$projectData);
        });
    }
}
exports.LocalBuildService = LocalBuildService;
$injector.register("localBuildService", LocalBuildService);
