import { XcodeProject } from './xcode/XcodeProject.js';
import { SwiftPackage } from './swift-package/SwiftPackage.js';
import { createModuleLogger } from '../logger.js';
import { existsSync } from 'fs';
import { readdir } from 'fs/promises';
import path from 'path';
const logger = createModuleLogger('ProjectDiscovery');
/**
 * Discovers and creates the appropriate project type
 * (Single Responsibility: Project Discovery and Factory)
 */
export class ProjectDiscovery {
    /**
     * Discover and create a project from the specified path
     * Automatically detects whether it's an Xcode project or Swift package
     */
    async discover(projectPath) {
        // Check if it's an Xcode project or workspace
        if (projectPath.endsWith('.xcodeproj') || projectPath.endsWith('.xcworkspace')) {
            if (!existsSync(projectPath)) {
                throw new Error(`Xcode project not found at: ${projectPath}`);
            }
            const type = projectPath.endsWith('.xcworkspace') ? 'workspace' : 'project';
            logger.debug({ projectPath, type }, 'Discovered Xcode project');
            return new XcodeProject(projectPath, type);
        }
        // Check if it's a Swift package (Package.swift)
        if (projectPath.endsWith('Package.swift')) {
            // Extract directory path from Package.swift path
            const packageDir = path.dirname(projectPath);
            if (!existsSync(projectPath)) {
                throw new Error(`Package.swift not found at: ${projectPath}`);
            }
            logger.debug({ projectPath: packageDir }, 'Discovered Swift package');
            return new SwiftPackage(packageDir);
        }
        // Check if it's a directory containing Package.swift
        if (existsSync(projectPath)) {
            const packageSwiftPath = path.join(projectPath, 'Package.swift');
            if (existsSync(packageSwiftPath)) {
                logger.debug({ projectPath }, 'Discovered Swift package in directory');
                return new SwiftPackage(projectPath);
            }
        }
        // Try to auto-discover project in directory
        if (existsSync(projectPath)) {
            const files = await readdir(projectPath);
            // Look for .xcworkspace first (higher priority)
            const workspace = files.find(f => f.endsWith('.xcworkspace'));
            if (workspace) {
                const workspacePath = path.join(projectPath, workspace);
                logger.debug({ workspacePath }, 'Auto-discovered workspace');
                return new XcodeProject(workspacePath, 'workspace');
            }
            // Look for .xcodeproj
            const xcodeproj = files.find(f => f.endsWith('.xcodeproj'));
            if (xcodeproj) {
                const xcodeprojPath = path.join(projectPath, xcodeproj);
                logger.debug({ xcodeprojPath }, 'Auto-discovered Xcode project');
                return new XcodeProject(xcodeprojPath, 'project');
            }
            // Look for Package.swift
            if (files.includes('Package.swift')) {
                logger.debug({ projectPath }, 'Auto-discovered Swift package');
                return new SwiftPackage(projectPath);
            }
        }
        throw new Error(`No Xcode project or Swift package found at: ${projectPath}`);
    }
    /**
     * Check if a project supports a specific interface
     */
    isBuildable(project) {
        return 'build' in project;
    }
    isTestable(project) {
        return 'test' in project;
    }
    isRunnable(project) {
        return 'run' in project;
    }
    isCleanable(project) {
        return 'clean' in project;
    }
}
//# sourceMappingURL=ProjectDiscovery.js.map