"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;
};
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, $options) {
        this.$hooksService = $hooksService;
        this.$nodeModulesBuilder = $nodeModulesBuilder;
        this.$projectChangesService = $projectChangesService;
        this.$metadataFilteringService = $metadataFilteringService;
        this.$options = $options;
    }
    async prepareNativePlatform(platformData, projectData, prepareData) {
        const { nativePrepare, release } = prepareData;
        const changesInfo = await 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) {
            await this.cleanProject(platformData, { release });
        }
        if (!this.$options.hostProjectPath) {
            platformData.platformProjectService.prepareAppResources(projectData);
        }
        if (hasChangesRequirePrepare || this.$options.hostProjectPath) {
            await platformData.platformProjectService.prepareProject(projectData, prepareData);
        }
        if (hasNativeModulesChange) {
            await this.$nodeModulesBuilder.prepareNodeModules({
                platformData,
                projectData,
            });
        }
        if (hasNativeModulesChange || hasConfigChange) {
            await platformData.platformProjectService.processConfigurationFilesFromAppResources(projectData, { release });
            await platformData.platformProjectService.handleNativeDependenciesChange(projectData, { release });
            this.$metadataFilteringService.generateMetadataFilters(projectData, platformData.platformNameLowerCase);
        }
        platformData.platformProjectService.interpolateConfigurationFile(projectData);
        await this.$projectChangesService.setNativePlatformStatus(platformData, projectData, { nativePlatformStatus: "3" });
        return hasChanges;
    }
    async cleanProject(platformData, options) {
        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) {
            await platformData.platformProjectService.cleanProject(platformData.projectRoot);
        }
    }
}
exports.PrepareNativePlatformService = PrepareNativePlatformService;
__decorate([
    (0, decorators_1.performanceLog)(),
    (0, helpers_1.hook)("prepareNativeApp")
], PrepareNativePlatformService.prototype, "prepareNativePlatform", null);
yok_1.injector.register("prepareNativePlatformService", PrepareNativePlatformService);
