"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) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrepareNativePlatformService = void 0;
const helpers_1 = require("../../common/helpers");
const decorators_1 = require("../../common/decorators");
const yok_1 = require("../../common/yok");
class PrepareNativePlatformService {
    constructor($hooksService, $nodeModulesBuilder, $projectChangesService, $metadataFilteringService) {
        this.$hooksService = $hooksService;
        this.$nodeModulesBuilder = $nodeModulesBuilder;
        this.$projectChangesService = $projectChangesService;
        this.$metadataFilteringService = $metadataFilteringService;
    }
    prepareNativePlatform(platformData, projectData, prepareData) {
        return __awaiter(this, void 0, void 0, function* () {
            const { nativePrepare, release } = prepareData;
            const changesInfo = yield this.$projectChangesService.checkForChanges(platformData, projectData, prepareData);
            if (nativePrepare && nativePrepare.skipNativePrepare) {
                return changesInfo.hasChanges;
            }
            const hasNativeModulesChange = !changesInfo || changesInfo.nativeChanged;
            const hasConfigChange = !changesInfo || changesInfo.configChanged;
            const hasChangesRequirePrepare = !changesInfo || changesInfo.changesRequirePrepare;
            const hasChanges = hasNativeModulesChange || hasConfigChange || hasChangesRequirePrepare;
            if (changesInfo.hasChanges) {
                yield this.cleanProject(platformData, { release });
            }
            platformData.platformProjectService.prepareAppResources(projectData);
            if (hasChangesRequirePrepare) {
                yield platformData.platformProjectService.prepareProject(projectData, prepareData);
            }
            if (hasNativeModulesChange) {
                yield this.$nodeModulesBuilder.prepareNodeModules({
                    platformData,
                    projectData,
                });
            }
            if (hasNativeModulesChange || hasConfigChange) {
                yield platformData.platformProjectService.processConfigurationFilesFromAppResources(projectData, { release });
                yield platformData.platformProjectService.handleNativeDependenciesChange(projectData, { release });
                this.$metadataFilteringService.generateMetadataFilters(projectData, platformData.platformNameLowerCase);
            }
            platformData.platformProjectService.interpolateConfigurationFile(projectData);
            yield this.$projectChangesService.setNativePlatformStatus(platformData, projectData, { nativePlatformStatus: "3" });
            return hasChanges;
        });
    }
    cleanProject(platformData, 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);
            }
        });
    }
}
__decorate([
    (0, decorators_1.performanceLog)(),
    (0, helpers_1.hook)("prepareNativeApp")
], PrepareNativePlatformService.prototype, "prepareNativePlatform", null);
exports.PrepareNativePlatformService = PrepareNativePlatformService;
yok_1.injector.register("prepareNativePlatformService", PrepareNativePlatformService);
