"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");
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;
    }
    // note: this is not used anywhere at the moment.
    // public hasSPMPackages(projectData: IProjectData): boolean {
    // 	return this.getSPMPackages(projectData).length > 0;
    // }
    applySPMPackages(platformData, projectData) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const spmPackages = this.getSPMPackages(projectData, platformData.platformNameLowerCase);
                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();
                // note: in trapeze both visionOS and iOS are handled by the ios project.
                if (!project.ios) {
                    this.$logger.trace("SPM: no iOS project found via trapeze.");
                    return;
                }
                // todo: handle removing packages? Or just warn and require a clean?
                for (const pkg of spmPackages) {
                    if ("path" in pkg) {
                        // resolve the path relative to the project root
                        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);
                    yield project.ios.addSPMPackage(projectData.projectName, pkg);
                }
                yield project.commit();
                // finally resolve the dependencies
                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, projectData)
                .concat([
                "-destination",
                "generic/platform=iOS",
                "-resolvePackageDependencies",
            ]), {
                cwd: projectData.projectDir,
                message: "Resolving SPM dependencies...",
            });
        });
    }
}
exports.SPMService = SPMService;
yok_1.injector.register("spmService", SPMService);
//# sourceMappingURL=spm-service.js.map