/**
 * Repertoire package removal helpers.
 *
 * Provides:
 * - findScopeReferences: scan YAML files for @scope references (for pre-removal warning)
 * - shouldRemoveOwnerDir: determine if the @owner directory should be deleted
 */
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { createLogger } from '../../shared/utils/debug.js';
const log = createLogger('repertoire-remove');
/**
 * Recursively scan a directory for YAML files containing the given @scope substring.
 */
function scanYamlFilesInDir(dir, scope, results) {
    if (!existsSync(dir))
        return;
    for (const entry of readdirSync(dir)) {
        const filePath = join(dir, entry);
        let stats;
        try {
            stats = statSync(filePath);
        }
        catch (err) {
            log.debug('Failed to stat file', { filePath, err });
            continue;
        }
        if (stats.isDirectory()) {
            scanYamlFilesInDir(filePath, scope, results);
            continue;
        }
        if (!entry.endsWith('.yaml') && !entry.endsWith('.yml'))
            continue;
        let content;
        try {
            content = readFileSync(filePath, 'utf-8');
        }
        catch (err) {
            log.debug('Failed to read file', { filePath, err });
            continue;
        }
        if (content.includes(scope)) {
            results.push({ filePath });
        }
    }
}
/**
 * Find all files that reference a given @scope package.
 *
 * Scans the 3 spec-defined locations:
 * 1. workflowDirs entries recursively (e.g. ~/.takt/workflows, .takt/workflows)
 * 2. categoriesFiles entries individually (e.g. ~/.takt/preferences/workflow-categories.yaml)
 *
 * @param scope  - e.g. "@nrslib/takt-fullstack"
 * @param config - explicit scan targets (workflowDirs + categoriesFiles)
 */
export function findScopeReferences(scope, config) {
    const results = [];
    const scannedDirs = new Set();
    for (const dir of config.workflowDirs) {
        if (!scannedDirs.has(dir)) {
            scanYamlFilesInDir(dir, scope, results);
            scannedDirs.add(dir);
        }
    }
    for (const filePath of config.categoriesFiles) {
        if (!existsSync(filePath))
            continue;
        try {
            const content = readFileSync(filePath, 'utf-8');
            if (content.includes(scope)) {
                results.push({ filePath });
            }
        }
        catch (err) {
            log.debug('Failed to read categories file', { filePath, err });
        }
    }
    return results;
}
/**
 * Determine whether the @owner directory can be removed after deleting a repo.
 *
 * Returns true if the owner directory would have no remaining subdirectories
 * once the given repo is removed.
 *
 * @param ownerDir         - absolute path to the @owner directory
 * @param repoBeingRemoved - repo name that will be deleted (excluded from check)
 */
export function shouldRemoveOwnerDir(ownerDir, repoBeingRemoved) {
    if (!existsSync(ownerDir))
        return false;
    const remaining = readdirSync(ownerDir).filter((entry) => {
        if (entry === repoBeingRemoved)
            return false;
        const entryPath = join(ownerDir, entry);
        try {
            return statSync(entryPath).isDirectory();
        }
        catch (err) {
            log.debug('Failed to stat entry', { entryPath, err });
            return false;
        }
    });
    return remaining.length === 0;
}
//# sourceMappingURL=remove.js.map