import { SwiftBuild } from './SwiftBuild.js';
import { SwiftPackageInfo } from './SwiftPackageInfo.js';
import { createModuleLogger } from '../../logger.js';
import * as pathModule from 'path';
import { existsSync } from 'fs';
const logger = createModuleLogger('SwiftPackage');
/**
 * Represents a Swift package (Package.swift)
 */
export class SwiftPackage {
    path;
    name;
    build;
    info;
    constructor(path, components) {
        this.path = path;
        // Validate that Package.swift exists
        const packageSwiftPath = pathModule.join(this.path, 'Package.swift');
        if (!existsSync(packageSwiftPath)) {
            throw new Error(`No Package.swift found at: ${this.path}`);
        }
        // Extract name from directory
        this.name = pathModule.basename(this.path);
        // Initialize components
        this.build = components?.build || new SwiftBuild();
        this.info = components?.info || new SwiftPackageInfo();
        logger.debug({ path: this.path, name: this.name }, 'SwiftPackage created');
    }
    /**
     * Build the package
     */
    async buildPackage(options = {}) {
        logger.info({ path: this.path, options }, 'Building Swift package');
        return await this.build.build(this.path, options);
    }
    /**
     * Run an executable from the package
     */
    async run(options = {}) {
        logger.info({ path: this.path, options }, 'Running Swift package');
        return await this.build.run(this.path, options);
    }
    /**
     * Test the package
     */
    async test(options = {}) {
        logger.info({ path: this.path, options }, 'Testing Swift package');
        return await this.build.test(this.path, options);
    }
    /**
     * Clean build artifacts
     */
    async clean() {
        logger.info({ path: this.path }, 'Cleaning Swift package');
        await this.build.clean(this.path);
    }
    /**
     * Get list of products (executables and libraries)
     */
    async getProducts() {
        return await this.info.getProducts(this.path);
    }
    /**
     * Get list of targets
     */
    async getTargets() {
        return await this.info.getTargets(this.path);
    }
    /**
     * Get list of dependencies
     */
    async getDependencies() {
        return await this.info.getDependencies(this.path);
    }
    /**
     * Add a dependency
     */
    async addDependency(url, options = {}) {
        logger.info({ path: this.path, url, options }, 'Adding dependency');
        await this.info.addDependency(this.path, url, options);
    }
    /**
     * Remove a dependency
     */
    async removeDependency(name) {
        logger.info({ path: this.path, name }, 'Removing dependency');
        await this.info.removeDependency(this.path, name);
    }
    /**
     * Update all dependencies
     */
    async updateDependencies() {
        logger.info({ path: this.path }, 'Updating dependencies');
        await this.info.updateDependencies(this.path);
    }
    /**
     * Resolve dependencies
     */
    async resolveDependencies() {
        logger.info({ path: this.path }, 'Resolving dependencies');
        await this.info.resolveDependencies(this.path);
    }
    /**
     * Get the package directory
     */
    getDirectory() {
        return this.path;
    }
    /**
     * Check if this is an executable package
     */
    async isExecutable() {
        const products = await this.getProducts();
        return products.some(p => p.type === 'executable');
    }
    /**
     * Get executable products
     */
    async getExecutables() {
        const products = await this.getProducts();
        return products
            .filter(p => p.type === 'executable')
            .map(p => p.name);
    }
}
//# sourceMappingURL=SwiftPackage.js.map