/**
 * Pure formatting helpers for resource display (extensions, skills, prompts, themes).
 *
 * These helpers are shell-owned presentation utilities shared by interactive
 * mode and slash-command UI. They format metadata into human-readable strings
 * without depending on interactive mode state.
 */
import * as os from 'node:os';
import { theme } from '../theme/runtime.js';
/** Replace home directory prefix with ~ for display. */
export function formatDisplayPath(p) {
    const home = os.homedir();
    if (p.startsWith(home)) {
        return `~${p.slice(home.length)}`;
    }
    return p;
}
/** Extract a short display path from npm/git package paths, falling back to formatDisplayPath. */
export function getShortPath(fullPath, source) {
    const npmMatch = fullPath.match(/node_modules\/(@?[^/]+(?:\/[^/]+)?)\/(.*)/);
    if (npmMatch && source.startsWith('npm:')) {
        return npmMatch[2];
    }
    const gitMatch = fullPath.match(/git\/[^/]+\/[^/]+\/(.*)/);
    if (gitMatch && source.startsWith('git:')) {
        return gitMatch[1];
    }
    return formatDisplayPath(fullPath);
}
/** Resolve display label and color for a resource's source/scope combination. */
export function getDisplaySourceInfo(source, scope) {
    if (source === 'local') {
        if (scope === 'user')
            return { label: 'user', color: 'muted' };
        if (scope === 'project')
            return { label: 'project', color: 'muted' };
        if (scope === 'temporary')
            return { label: 'path', scopeLabel: 'temp', color: 'muted' };
        return { label: 'path', color: 'muted' };
    }
    if (source === 'cli') {
        return {
            label: 'path',
            scopeLabel: scope === 'temporary' ? 'temp' : undefined,
            color: 'muted'
        };
    }
    const scopeLabel = scope === 'user'
        ? 'user'
        : scope === 'project'
            ? 'project'
            : scope === 'temporary'
                ? 'temp'
                : undefined;
    return { label: source, scopeLabel, color: 'accent' };
}
function getScopeGroup(source, scope) {
    if (source === 'cli' || scope === 'temporary')
        return 'path';
    if (scope === 'user')
        return 'user';
    if (scope === 'project')
        return 'project';
    return 'path';
}
function isPackageSource(source) {
    return source.startsWith('npm:') || source.startsWith('git:');
}
export function findMetadata(p, metadata) {
    const exact = metadata.get(p);
    if (exact)
        return exact;
    let current = p;
    while (current.includes('/')) {
        current = current.substring(0, current.lastIndexOf('/'));
        const parent = metadata.get(current);
        if (parent)
            return parent;
    }
    return undefined;
}
/** Group resource paths by scope (project/user/path), separating packages from local files. */
export function buildScopeGroups(paths, metadata) {
    const groups = {
        user: { scope: 'user', paths: [], packages: new Map() },
        project: { scope: 'project', paths: [], packages: new Map() },
        path: { scope: 'path', paths: [], packages: new Map() }
    };
    for (const p of paths) {
        const meta = findMetadata(p, metadata);
        const source = meta?.source ?? 'local';
        const scope = meta?.scope ?? 'project';
        const groupKey = getScopeGroup(source, scope);
        const group = groups[groupKey];
        if (isPackageSource(source)) {
            const list = group.packages.get(source) ?? [];
            list.push(p);
            group.packages.set(source, list);
        }
        else {
            group.paths.push(p);
        }
    }
    return [groups.project, groups.user, groups.path].filter((group) => group.paths.length > 0 || group.packages.size > 0);
}
/** Format scope groups into a multi-line display string with indentation. */
export function formatScopeGroups(groups, options) {
    const lines = [];
    for (const group of groups) {
        lines.push(`  ${theme.fg('accent', group.scope)}`);
        const sortedPaths = [...group.paths].sort((a, b) => a.localeCompare(b));
        for (const p of sortedPaths) {
            lines.push(theme.fg('dim', `    ${options.formatPath(p)}`));
        }
        const sortedPackages = Array.from(group.packages.entries()).sort(([a], [b]) => a.localeCompare(b));
        for (const [source, paths] of sortedPackages) {
            lines.push(`    ${theme.fg('mdLink', source)}`);
            const sortedPackagePaths = [...paths].sort((a, b) => a.localeCompare(b));
            for (const p of sortedPackagePaths) {
                lines.push(theme.fg('dim', `      ${options.formatPackagePath(p, source)}`));
            }
        }
    }
    return lines.join('\n');
}
export function formatPathWithSource(p, metadata) {
    const meta = findMetadata(p, metadata);
    if (meta) {
        const shortPath = getShortPath(p, meta.source);
        const { label, scopeLabel } = getDisplaySourceInfo(meta.source, meta.scope);
        const labelText = scopeLabel ? `${label} (${scopeLabel})` : label;
        return `${labelText} ${shortPath}`;
    }
    return formatDisplayPath(p);
}
/** Format resource diagnostics (collisions, errors, warnings) for display. */
export function formatDiagnostics(diagnostics, metadata) {
    const lines = [];
    const collisions = new Map();
    const otherDiagnostics = [];
    for (const d of diagnostics) {
        if (d.type === 'collision' && d.collision) {
            const list = collisions.get(d.collision.name) ?? [];
            list.push(d);
            collisions.set(d.collision.name, list);
        }
        else {
            otherDiagnostics.push(d);
        }
    }
    for (const [name, collisionList] of collisions) {
        const first = collisionList[0]?.collision;
        if (!first)
            continue;
        lines.push(theme.fg('warning', `  "${name}" collision:`));
        lines.push(theme.fg('dim', `    ${theme.fg('success', '✓')} ${formatPathWithSource(first.winnerPath, metadata)}`));
        for (const d of collisionList) {
            if (d.collision) {
                lines.push(theme.fg('dim', `    ${theme.fg('warning', '✗')} ${formatPathWithSource(d.collision.loserPath, metadata)} (skipped)`));
            }
        }
    }
    for (const d of otherDiagnostics) {
        if (d.path) {
            const sourceInfo = formatPathWithSource(d.path, metadata);
            lines.push(theme.fg(d.type === 'error' ? 'error' : 'warning', `  ${sourceInfo}`));
            lines.push(theme.fg(d.type === 'error' ? 'error' : 'warning', `    ${d.message}`));
        }
        else {
            lines.push(theme.fg(d.type === 'error' ? 'error' : 'warning', `  ${d.message}`));
        }
    }
    return lines.join('\n');
}
/** Create the standard ResourceDisplayHelpers bundle for use in interactive mode. */
export function createResourceDisplayHelpers() {
    return {
        formatDisplayPath,
        buildScopeGroups,
        formatScopeGroups,
        formatDiagnostics,
        formatPathWithSource,
        getShortPath
    };
}
//# sourceMappingURL=resource-display.js.map