"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const helpers_1 = require("../../common/helpers");
const decorators_1 = require("../../common/decorators");
const path = require("path");
const constants_1 = require("../../constants");
class PrepareNativePlatformService {
    constructor($androidResourcesMigrationService, $fs, $hooksService, $nodeModulesBuilder, $projectChangesService) {
        this.$androidResourcesMigrationService = $androidResourcesMigrationService;
        this.$fs = $fs;
        this.$hooksService = $hooksService;
        this.$nodeModulesBuilder = $nodeModulesBuilder;
        this.$projectChangesService = $projectChangesService;
    }
    prepareNativePlatform(platformData, projectData, prepareData) {
        return __awaiter(this, void 0, void 0, function* () {
            const { nativePrepare, release } = prepareData;
            if (nativePrepare && nativePrepare.skipNativePrepare) {
                return false;
            }
            const changesInfo = yield this.$projectChangesService.checkForChanges(platformData, projectData, prepareData);
            const hasModulesChange = !changesInfo || changesInfo.modulesChanged;
            const hasConfigChange = !changesInfo || changesInfo.configChanged;
            const hasChangesRequirePrepare = !changesInfo || changesInfo.changesRequirePrepare;
            const hasChanges = hasModulesChange || hasConfigChange || hasChangesRequirePrepare;
            if (changesInfo.hasChanges) {
                yield this.cleanProject(platformData, projectData, { release });
            }
            this.prepareAppResources(platformData, projectData);
            if (hasChangesRequirePrepare) {
                yield platformData.platformProjectService.prepareProject(projectData, prepareData);
            }
            if (hasModulesChange) {
                yield this.$nodeModulesBuilder.prepareNodeModules(platformData, projectData);
            }
            if (hasModulesChange || hasConfigChange) {
                yield platformData.platformProjectService.processConfigurationFilesFromAppResources(projectData, { release });
                yield platformData.platformProjectService.handleNativeDependenciesChange(projectData, { release });
            }
            platformData.platformProjectService.interpolateConfigurationFile(projectData);
            this.$projectChangesService.setNativePlatformStatus(platformData, { nativePlatformStatus: "3" });
            return hasChanges;
        });
    }
    prepareAppResources(platformData, projectData) {
        const appDestinationDirectoryPath = path.join(platformData.appDestinationDirectoryPath, constants_1.APP_FOLDER_NAME);
        const appResourcesDestinationDirectoryPath = path.join(appDestinationDirectoryPath, constants_1.APP_RESOURCES_FOLDER_NAME);
        if (this.$fs.exists(appResourcesDestinationDirectoryPath)) {
            platformData.platformProjectService.prepareAppResources(appResourcesDestinationDirectoryPath, projectData);
            const appResourcesDestination = platformData.platformProjectService.getAppResourcesDestinationDirectoryPath(projectData);
            this.$fs.ensureDirectoryExists(appResourcesDestination);
            if (platformData.normalizedPlatformName.toLowerCase() === "android") {
                const appResourcesDirectoryPath = projectData.getAppResourcesDirectoryPath();
                const appResourcesDirStructureHasMigrated = this.$androidResourcesMigrationService.hasMigrated(appResourcesDirectoryPath);
                const appResourcesAndroid = path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName);
                if (appResourcesDirStructureHasMigrated) {
                    this.$fs.copyFile(path.join(appResourcesAndroid, "src", "*"), appResourcesDestination);
                    this.$fs.deleteDirectory(appResourcesDestinationDirectoryPath);
                    return;
                }
                this.$fs.copyFile(path.join(appResourcesDestinationDirectoryPath, platformData.normalizedPlatformName, "*"), appResourcesDestination);
                this.$fs.deleteDirectory(path.join(appResourcesDestination, "libs"));
                this.$fs.deleteDirectory(appResourcesDestinationDirectoryPath);
                return;
            }
            this.$fs.copyFile(path.join(appResourcesDestinationDirectoryPath, platformData.normalizedPlatformName, "*"), appResourcesDestination);
            this.$fs.deleteDirectory(appResourcesDestinationDirectoryPath);
        }
    }
    cleanProject(platformData, projectData, options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (platformData.platformNameLowerCase !== "android") {
                return;
            }
            const previousPrepareInfo = this.$projectChangesService.getPrepareInfo(platformData);
            if (!previousPrepareInfo || previousPrepareInfo.nativePlatformStatus !== "3") {
                return;
            }
            const { release: previousWasRelease } = previousPrepareInfo;
            const { release: currentIsRelease } = options;
            if (previousWasRelease !== currentIsRelease) {
                yield platformData.platformProjectService.cleanProject(platformData.projectRoot, projectData);
            }
        });
    }
}
__decorate([
    decorators_1.performanceLog(),
    helpers_1.hook('prepareNativeApp')
], PrepareNativePlatformService.prototype, "prepareNativePlatform", null);
exports.PrepareNativePlatformService = PrepareNativePlatformService;
$injector.register("prepareNativePlatformService", PrepareNativePlatformService);
