"use strict";
/**
 * Package Manager Utilities
 * Handles package manager-specific commands and configurations
 * Now supports multiple languages and their package managers
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackageManager = getPackageManager;
exports.getPackageManagerCommands = getPackageManagerCommands;
exports.generateInstallInstructions = generateInstallInstructions;
exports.getLockFileName = getLockFileName;
exports.getCISetup = getCISetup;
const technology_registry_1 = require("./technology-registry");
/**
 * Get package manager from workflow config (technology-aware)
 */
function getPackageManager(workflow, componentName) {
    let technology;
    let configuredPm;
    // Get technology and configured package manager for this component
    if (componentName && workflow.stack?.[componentName]) {
        const component = workflow.stack[componentName];
        technology = component.technology;
        configuredPm = component.config?.packageManager;
    }
    // If no component-specific config, check workflow-level
    if (!configuredPm && workflow.workflow?.config?.packageManager) {
        configuredPm = workflow.workflow.config.packageManager;
    }
    // If we have a technology, use technology-aware resolution
    if (technology) {
        return (0, technology_registry_1.getPackageManagerForTechnology)(technology, configuredPm);
    }
    // Fallback to configured or default
    return (configuredPm || 'npm');
}
/**
 * Get package manager commands for a given package manager
 */
function getPackageManagerCommands(pm) {
    const commands = {
        npm: {
            install: 'npm install',
            installGlobal: (pkg) => `npm install -g ${pkg}`,
            run: (script) => `npm run ${script}`,
            test: 'npm test',
            build: 'npm run build',
            add: (pkg) => `npm install ${pkg}`,
            addDev: (pkg) => `npm install --save-dev ${pkg}`,
            link: 'npm link',
            exec: (cmd) => `npx ${cmd}`,
        },
        yarn: {
            install: 'yarn install',
            installGlobal: (pkg) => `yarn global add ${pkg}`,
            run: (script) => `yarn ${script}`,
            test: 'yarn test',
            build: 'yarn build',
            add: (pkg) => `yarn add ${pkg}`,
            addDev: (pkg) => `yarn add --dev ${pkg}`,
            link: 'yarn link',
            exec: (cmd) => `yarn ${cmd}`,
        },
        pnpm: {
            install: 'pnpm install',
            installGlobal: (pkg) => `pnpm add -g ${pkg}`,
            run: (script) => `pnpm ${script}`,
            test: 'pnpm test',
            build: 'pnpm build',
            add: (pkg) => `pnpm add ${pkg}`,
            addDev: (pkg) => `pnpm add -D ${pkg}`,
            link: 'pnpm link',
            exec: (cmd) => `pnpm exec ${cmd}`,
        },
        bun: {
            install: 'bun install',
            installGlobal: (pkg) => `bun add -g ${pkg}`,
            run: (script) => `bun ${script}`,
            test: 'bun test',
            build: 'bun build',
            add: (pkg) => `bun add ${pkg}`,
            addDev: (pkg) => `bun add -d ${pkg}`,
            link: 'bun link',
            exec: (cmd) => `bun ${cmd}`,
        },
        // Python
        pip: {
            install: 'pip install -r requirements.txt',
            installGlobal: (pkg) => `pip install ${pkg}`,
            run: (script) => `python -m ${script}`,
            test: 'pytest',
            build: 'python setup.py build',
            add: (pkg) => `pip install ${pkg}`,
            addDev: (pkg) => `pip install ${pkg}`,
            link: 'pip install -e .',
            exec: (cmd) => `python -m ${cmd}`,
        },
        poetry: {
            install: 'poetry install',
            installGlobal: (pkg) => `pipx install ${pkg}`,
            run: (script) => `poetry run ${script}`,
            test: 'poetry run pytest',
            build: 'poetry build',
            add: (pkg) => `poetry add ${pkg}`,
            addDev: (pkg) => `poetry add --group dev ${pkg}`,
            link: 'poetry install',
            exec: (cmd) => `poetry run ${cmd}`,
        },
        pipenv: {
            install: 'pipenv install',
            installGlobal: (pkg) => `pip install ${pkg}`,
            run: (script) => `pipenv run ${script}`,
            test: 'pipenv run pytest',
            build: 'pipenv run python setup.py build',
            add: (pkg) => `pipenv install ${pkg}`,
            addDev: (pkg) => `pipenv install --dev ${pkg}`,
            link: 'pipenv install -e .',
            exec: (cmd) => `pipenv run ${cmd}`,
        },
        conda: {
            install: 'conda install --file requirements.txt',
            installGlobal: (pkg) => `conda install ${pkg}`,
            run: (script) => `python ${script}`,
            test: 'pytest',
            build: 'python setup.py build',
            add: (pkg) => `conda install ${pkg}`,
            addDev: (pkg) => `conda install ${pkg}`,
            link: 'conda develop .',
            exec: (cmd) => `${cmd}`,
        },
        // Go
        go: {
            install: 'go mod download',
            installGlobal: (pkg) => `go install ${pkg}@latest`,
            run: (script) => `go run ${script}`,
            test: 'go test ./...',
            build: 'go build',
            add: (pkg) => `go get ${pkg}`,
            addDev: (pkg) => `go get ${pkg}`, // No distinction in Go
            link: 'go install',
            exec: (cmd) => `go run ${cmd}`,
        },
        // Rust
        cargo: {
            install: 'cargo fetch',
            installGlobal: (pkg) => `cargo install ${pkg}`,
            run: (_script) => `cargo run`, // Cargo doesn't use script names
            test: 'cargo test',
            build: 'cargo build',
            add: (pkg) => `cargo add ${pkg}`,
            addDev: (pkg) => `cargo add --dev ${pkg}`,
            link: 'cargo install --path .',
            exec: (cmd) => `cargo ${cmd}`,
        },
        // Ruby
        gem: {
            install: 'gem install',
            installGlobal: (pkg) => `gem install ${pkg}`,
            run: (script) => `ruby ${script}`,
            test: 'rake test',
            build: 'gem build',
            add: (pkg) => `gem install ${pkg}`,
            addDev: (pkg) => `gem install ${pkg}`,
            link: 'gem install --local',
            exec: (cmd) => `${cmd}`,
        },
        bundler: {
            install: 'bundle install',
            installGlobal: (pkg) => `gem install ${pkg}`,
            run: (script) => `bundle exec ${script}`,
            test: 'bundle exec rspec',
            build: 'gem build',
            add: (pkg) => `bundle add ${pkg}`,
            addDev: (pkg) => `bundle add ${pkg} --group development`,
            link: 'bundle install --local',
            exec: (cmd) => `bundle exec ${cmd}`,
        },
        // PHP
        composer: {
            install: 'composer install',
            installGlobal: (pkg) => `composer global require ${pkg}`,
            run: (script) => `composer run ${script}`,
            test: 'composer test',
            build: 'composer build',
            add: (pkg) => `composer require ${pkg}`,
            addDev: (pkg) => `composer require --dev ${pkg}`,
            link: 'composer install',
            exec: (cmd) => `composer exec ${cmd}`,
        },
        // Java
        maven: {
            install: 'mvn install',
            installGlobal: (pkg) => `mvn install ${pkg}`,
            run: (_script) => `mvn exec:java`, // Maven uses pom.xml config
            test: 'mvn test',
            build: 'mvn package',
            add: (pkg) => `# Add to pom.xml: ${pkg}`,
            addDev: (pkg) => `# Add to pom.xml: ${pkg}`,
            link: 'mvn install',
            exec: (cmd) => `mvn ${cmd}`,
        },
        gradle: {
            install: 'gradle build',
            installGlobal: (pkg) => `gradle install ${pkg}`,
            run: (_script) => `gradle run`, // Gradle uses build.gradle config
            test: 'gradle test',
            build: 'gradle build',
            add: (pkg) => `# Add to build.gradle: ${pkg}`,
            addDev: (pkg) => `# Add to build.gradle: ${pkg}`,
            link: 'gradle install',
            exec: (cmd) => `gradle ${cmd}`,
        },
        // .NET
        nuget: {
            install: 'nuget restore',
            installGlobal: (pkg) => `nuget install ${pkg}`,
            run: (_script) => `dotnet run`, // NuGet uses project file config
            test: 'dotnet test',
            build: 'dotnet build',
            add: (pkg) => `nuget install ${pkg}`,
            addDev: (pkg) => `nuget install ${pkg}`,
            link: 'nuget install',
            exec: (cmd) => `dotnet ${cmd}`,
        },
        dotnet: {
            install: 'dotnet restore',
            installGlobal: (pkg) => `dotnet tool install -g ${pkg}`,
            run: (_script) => `dotnet run`, // .NET uses project file config
            test: 'dotnet test',
            build: 'dotnet build',
            add: (pkg) => `dotnet add package ${pkg}`,
            addDev: (pkg) => `dotnet add package ${pkg}`,
            link: 'dotnet build',
            exec: (cmd) => `dotnet ${cmd}`,
        },
    };
    return commands[pm];
}
/**
 * Generate installation instructions for all package managers
 */
function generateInstallInstructions(packageName, preferredPm) {
    const npm = `npm install -g ${packageName}`;
    const yarn = `yarn global add ${packageName}`;
    const pnpm = `pnpm add -g ${packageName}`;
    const bun = `bun add -g ${packageName}`;
    if (preferredPm) {
        const pmCommands = getPackageManagerCommands(preferredPm);
        return `\`\`\`bash
# Using ${preferredPm} (recommended)
${pmCommands.installGlobal(packageName)}

# Or using other package managers:
# npm
${npm}

# yarn
${yarn}

# pnpm
${pnpm}

# bun
${bun}
\`\`\``;
    }
    return `\`\`\`bash
# Using npm
${npm}

# Using yarn
${yarn}

# Using pnpm
${pnpm}

# Using bun
${bun}
\`\`\``;
}
/**
 * Get lock file name for package manager
 */
function getLockFileName(pm) {
    const lockFiles = {
        npm: 'package-lock.json',
        yarn: 'yarn.lock',
        pnpm: 'pnpm-lock.yaml',
        bun: 'bun.lockb',
        poetry: 'poetry.lock',
        pipenv: 'Pipfile.lock',
        bundler: 'Gemfile.lock',
        composer: 'composer.lock',
        cargo: 'Cargo.lock',
        // go, pip, conda, gem, maven, gradle, nuget, dotnet don't use lock files or use different mechanisms
    };
    return lockFiles[pm];
}
/**
 * Get package manager setup for CI/CD (GitHub Actions)
 */
function getCISetup(pm) {
    const setups = {
        npm: {
            setup: `- uses: actions/setup-node@v3
      with:
        node-version: '20'
        cache: 'npm'`,
            cache: 'npm',
        },
        yarn: {
            setup: `- uses: actions/setup-node@v3
      with:
        node-version: '20'
        cache: 'yarn'`,
            cache: 'yarn',
        },
        pnpm: {
            setup: `- uses: pnpm/action-setup@v2
      with:
        version: 8
    - uses: actions/setup-node@v3
      with:
        node-version: '20'
        cache: 'pnpm'`,
            cache: 'pnpm',
        },
        bun: {
            setup: `- uses: oven-sh/setup-bun@v1
      with:
        bun-version: latest`,
            cache: 'bun',
        },
        pip: {
            setup: `- uses: actions/setup-python@v4
      with:
        python-version: '3.11'
        cache: 'pip'`,
            cache: 'pip',
        },
        poetry: {
            setup: `- uses: actions/setup-python@v4
      with:
        python-version: '3.11'
        cache: 'poetry'`,
            cache: 'poetry',
        },
        pipenv: {
            setup: `- uses: actions/setup-python@v4
      with:
        python-version: '3.11'
        cache: 'pipenv'`,
            cache: 'pipenv',
        },
        go: {
            setup: `- uses: actions/setup-go@v4
      with:
        go-version: '1.21'
        cache: true`,
            cache: 'go',
        },
        cargo: {
            setup: `- uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        profile: minimal
    - uses: Swatinem/rust-cache@v2`,
            cache: 'cargo',
        },
        bundler: {
            setup: `- uses: ruby/setup-ruby@v1
      with:
        ruby-version: '3.2'
        bundler-cache: true`,
            cache: 'bundler',
        },
        composer: {
            setup: `- uses: shivammathur/setup-php@v2
      with:
        php-version: '8.2'
        tools: composer`,
            cache: 'composer',
        },
        maven: {
            setup: `- uses: actions/setup-java@v3
      with:
        java-version: '17'
        distribution: 'temurin'
        cache: 'maven'`,
            cache: 'maven',
        },
        gradle: {
            setup: `- uses: actions/setup-java@v3
      with:
        java-version: '17'
        distribution: 'temurin'
        cache: 'gradle'`,
            cache: 'gradle',
        },
        dotnet: {
            setup: `- uses: actions/setup-dotnet@v3
      with:
        dotnet-version: '7.0.x'`,
            cache: 'dotnet',
        },
        // conda, gem, nuget don't have standard cache setups or use different mechanisms
    };
    return setups[pm];
}
//# sourceMappingURL=package-manager.js.map