import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { Box, Text } from 'ink';
import React from 'react';
import { TitledBoxWithPreferences } from '../components/ui/titled-box.js';
import { useTerminalWidth } from '../hooks/useTerminalWidth.js';
import { useTheme } from '../hooks/useTheme.js';
import { generateKey } from '../session/key-generator.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function getContributors() {
    try {
        const content = await readFile(path.join(__dirname, './contributors.json'), 'utf8');
        const data = JSON.parse(content);
        return data.contributors;
    }
    catch {
        return [];
    }
}
async function getDependencies() {
    try {
        const content = await readFile(path.join(__dirname, '../../package.json'), 'utf8');
        const packageJson = JSON.parse(content);
        const deps = packageJson.dependencies ?? {};
        return Object.entries(deps)
            .map(([name, version]) => ({ name, version: version.replace(/^\^/, '') }))
            .sort((a, b) => a.name.localeCompare(b.name));
    }
    catch {
        return [];
    }
}
function Credits({ contributors, dependencies, }) {
    const boxWidth = useTerminalWidth();
    const { colors } = useTheme();
    return (_jsxs(TitledBoxWithPreferences, { title: "Credits", width: boxWidth, borderColor: colors.primary, paddingX: 2, paddingY: 1, flexDirection: "column", marginBottom: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: colors.text, children: "Nanocoder is built by the community, for the community. Every contribution matters and we want to credit everyone who has helped make this project what it is." }) }), _jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: colors.primary, bold: true, children: "Contributors" }) }), contributors.length === 0 ? (_jsx(Text, { color: colors.secondary, children: " No contributor data available." })) : (contributors.map((name, index) => (_jsxs(Text, { color: colors.text, children: [' ', "\u2022 ", name] }, index)))), _jsx(Box, { marginTop: 1, marginBottom: 1, children: _jsx(Text, { color: colors.primary, bold: true, children: "Dependencies" }) }), dependencies.map((dep, index) => (_jsxs(Text, { color: colors.text, children: [' ', "\u2022 ", _jsx(Text, { color: colors.secondary, children: dep.name }), " ", dep.version] }, index)))] }));
}
export const creditsCommand = {
    name: 'credits',
    description: 'Show project contributors and dependencies',
    handler: async () => {
        const [contributors, dependencies] = await Promise.all([
            getContributors(),
            getDependencies(),
        ]);
        return React.createElement(Credits, {
            key: generateKey('credits'),
            contributors,
            dependencies,
        });
    },
};
//# sourceMappingURL=credits.js.map