/**
 * Bulk Get Helpers for `specweave get`
 *
 * Wires fetchGitHubRepos + matchByGlob + launchCloneJob into a bulk clone path.
 */
import { execFileNoThrow } from '../../../utils/execFileNoThrow.js';
import { fetchGitHubRepos } from '../init/github-repo-cloning.js';
import { matchByGlob } from '../selection-strategy.js';
import { REPO_FETCH_LIMITS } from '../init/types.js';
/**
 * Parse source into org + optional glob pattern.
 * Returns null if source should be handled by the single-repo path.
 */
export function parseBulkSource(source, options) {
    const trimmed = source.trim();
    // Skip local paths and full URLs — not bulk targets
    if (trimmed.startsWith('./') ||
        trimmed.startsWith('../') ||
        trimmed.startsWith('/') ||
        trimmed.startsWith('~') ||
        trimmed.startsWith('http') ||
        trimmed.startsWith('git@')) {
        return null;
    }
    const slashIdx = trimmed.indexOf('/');
    if (slashIdx === -1) {
        // Bare org name — only bulk if --all is set
        if (!options.all)
            return null;
        return { org: trimmed, pattern: options.pattern ?? null };
    }
    const org = trimmed.slice(0, slashIdx);
    const repoOrPattern = trimmed.slice(slashIdx + 1);
    // Contains glob chars → bulk mode
    if (repoOrPattern.includes('*') || repoOrPattern.includes('?')) {
        const pattern = repoOrPattern === '*' ? null : repoOrPattern;
        return { org, pattern };
    }
    // --all flag with org/something form — treat something as pattern override
    if (options.all) {
        return { org, pattern: options.pattern ?? repoOrPattern };
    }
    return null;
}
/**
 * Resolve GitHub auth token.
 * Priority: GH_TOKEN env → `gh auth token` CLI → throw helpful error.
 */
export async function getAuthToken() {
    if (process.env.GH_TOKEN?.trim())
        return process.env.GH_TOKEN.trim();
    const result = await execFileNoThrow('gh', ['auth', 'token'], {});
    if (result.exitCode === 0 && result.stdout.trim()) {
        return result.stdout.trim();
    }
    throw new Error('GitHub auth required for bulk clone.\n' +
        'Fix: run `gh auth login` or set the GH_TOKEN environment variable.');
}
/**
 * Fetch repos from a GitHub org, apply pattern + filters, and return
 * the CloneLaunchOptions.repositories array ready for launchCloneJob().
 */
export async function buildBulkRepoList(org, token, pattern, options) {
    const limit = options.limit ?? REPO_FETCH_LIMITS.DEFAULT_MAX_REPOS;
    const fetchResult = await fetchGitHubRepos(org, token, limit);
    if (fetchResult.error) {
        throw new Error(fetchResult.error);
    }
    let repos = fetchResult.repos;
    // Apply pre-filter on archived/forks before pattern matching
    if (options.noArchived)
        repos = repos.filter(r => !r.archived);
    if (options.noForks)
        repos = repos.filter(r => !r.fork);
    // Apply glob pattern
    if (pattern !== null) {
        const matched = matchByGlob(repos.map(r => ({ name: r.name, value: r.name })), pattern, 'name');
        const matchedNames = new Set(matched.map(m => m.name));
        repos = repos.filter(r => matchedNames.has(r.name));
    }
    return repos.map(r => ({
        owner: r.owner.login,
        name: r.name,
        path: `repositories/${r.owner.login}/${r.name}`,
        cloneUrl: r.clone_url,
    }));
}
//# sourceMappingURL=bulk-get.js.map