"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SPMService = void 0;
const yok_1 = require("../../common/yok");
const trapezedev_project_1 = require("@rigor789/trapezedev-project");
const path = require("path");
class SPMService {
    constructor($logger, $projectConfigService, $xcodebuildCommandService, $xcodebuildArgsService) {
        this.$logger = $logger;
        this.$projectConfigService = $projectConfigService;
        this.$xcodebuildCommandService = $xcodebuildCommandService;
        this.$xcodebuildArgsService = $xcodebuildArgsService;
    }
    getSPMPackages(projectData, platform) {
        const spmPackages = this.$projectConfigService.getValue(`${platform}.SPMPackages`, []);
        return spmPackages;
    }
    async applySPMPackages(platformData, projectData, pluginSpmPackages) {
        var _a;
        try {
            const spmPackages = this.getSPMPackages(projectData, platformData.platformNameLowerCase);
            if (pluginSpmPackages === null || pluginSpmPackages === void 0 ? void 0 : pluginSpmPackages.length) {
                spmPackages.push(...pluginSpmPackages);
            }
            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,
            });
            await project.load();
            if (!project.ios) {
                this.$logger.trace("SPM: no iOS project found via trapeze.");
                return;
            }
            for (const pkg of spmPackages) {
                if ("path" in pkg) {
                    this.$logger.trace("SPM: resolving path for package: ", pkg.path);
                    pkg.path = path.resolve(projectData.projectDir, pkg.path);
                }
                this.$logger.trace(`SPM: adding package ${pkg.name} to project.`, pkg);
                await project.ios.addSPMPackage(projectData.projectName, pkg);
                if ((_a = pkg.targets) === null || _a === void 0 ? void 0 : _a.length) {
                    for (const target of pkg.targets) {
                        await project.ios.addSPMPackage(target, pkg);
                    }
                }
            }
            await project.commit();
            await this.resolveSPMDependencies(platformData, projectData);
        }
        catch (err) {
            this.$logger.trace("SPM: error applying SPM packages: ", err);
        }
    }
    async resolveSPMDependencies(platformData, projectData) {
        await this.$xcodebuildCommandService.executeCommand(this.$xcodebuildArgsService
            .getXcodeProjectArgs(platformData, projectData)
            .concat([
            "-destination",
            "generic/platform=iOS",
            "-resolvePackageDependencies",
        ]), {
            cwd: projectData.projectDir,
            message: "Resolving SPM dependencies...",
        });
    }
}
exports.SPMService = SPMService;
yok_1.injector.register("spmService", SPMService);
