"use strict";
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.SPMService = void 0;
const yok_1 = require("../../common/yok");
const trapezedev_project_1 = require("@rigor789/trapezedev-project");
class SPMService {
    constructor($logger, $projectConfigService, $xcodebuildCommandService, $xcodebuildArgsService) {
        this.$logger = $logger;
        this.$projectConfigService = $projectConfigService;
        this.$xcodebuildCommandService = $xcodebuildCommandService;
        this.$xcodebuildArgsService = $xcodebuildArgsService;
    }
    getSPMPackages(projectData) {
        const spmPackages = this.$projectConfigService.getValue("ios.SPMPackages", []);
        return spmPackages;
    }
    hasSPMPackages(projectData) {
        return this.getSPMPackages(projectData).length > 0;
    }
    applySPMPackages(platformData, projectData) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const spmPackages = this.getSPMPackages(projectData);
                if (!spmPackages.length) {
                    this.$logger.trace("SPM: no SPM packages to apply.");
                    return;
                }
                const project = new trapezedev_project_1.MobileProject(platformData.projectRoot, {
                    ios: {
                        path: ".",
                    },
                    enableAndroid: false,
                });
                yield project.load();
                if (!project.ios) {
                    this.$logger.trace("SPM: no iOS project found via trapeze.");
                    return;
                }
                for (const pkg of spmPackages) {
                    this.$logger.trace(`SPM: adding package ${pkg.name} to project.`, pkg);
                    yield project.ios.addSPMPackage(projectData.projectName, pkg);
                }
                yield project.commit();
                yield this.resolveSPMDependencies(platformData, projectData);
            }
            catch (err) {
                this.$logger.trace("SPM: error applying SPM packages: ", err);
            }
        });
    }
    resolveSPMDependencies(platformData, projectData) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.$xcodebuildCommandService.executeCommand(this.$xcodebuildArgsService
                .getXcodeProjectArgs(platformData.projectRoot, projectData)
                .concat([
                "-destination",
                "generic/platform=iOS",
                "-resolvePackageDependencies",
            ]), {
                cwd: projectData.projectDir,
                message: "Resolving SPM dependencies...",
            });
        });
    }
}
exports.SPMService = SPMService;
yok_1.injector.register("spmService", SPMService);
