import type { TerminalLine } from '$lib/components/tui/types';
import { packageProjects } from '$lib/config';
import { projectNavLinks } from './projectNav';
import { tagColor } from './palette';

const orderedPackages = [...packageProjects].sort((a, b) => Number(b.featured) - Number(a.featured));

const packageSections: TerminalLine[] = orderedPackages.flatMap(pkg => {
    const infoChildren: TerminalLine[] = [
        {
            type: 'output' as const,
            content: `(&icon, mdi:package-variant, 32) (&bg-orange,black) PACKAGE (&)${pkg.featured ? ' (&bg-accent,black) FEATURED (&)' : ''}`
        },
        { type: 'output' as const, content: `(&text)${pkg.description}(&)` },
        {
            type: 'output' as const,
            content: pkg.tech.map((tech, i) => `(&bg-${tagColor(i)},black) ${tech} (&)`).join(' ')
        },
        {
            type: 'group' as const,
            content: '',
            groupDirection: 'row' as const,
            groupGap: '0.75rem',
            groupExpand: true,
            children: [
                ...(pkg.github
                    ? [{ type: 'button' as const, content: 'View on GitHub', href: pkg.github, icon: 'mdi:github', style: 'accent' as const, flex: true }]
                    : []),
                ...(pkg.live
                    ? [{ type: 'button' as const, content: 'View Package', href: pkg.live, icon: 'mdi:package-variant', style: 'primary' as const, flex: true }]
                    : [])
            ]
        }
    ];

    return [
        { type: 'divider' as const, content: pkg.name.toUpperCase() },
        { type: 'group' as const, content: '', groupDirection: 'column' as const, groupAlign: 'stretch' as const, children: infoChildren },
        { type: 'blank' as const, content: '' }
    ];
});

export const lines: TerminalLine[] = [
    { type: 'command' as const, content: 'ls ~/projects/packages' },
    { type: 'blank' as const, content: '', height: '1rem' },
    projectNavLinks,
    { type: 'blank' as const, content: '', height: '1rem' },
    { type: 'header' as const, content: 'Packages' },
    { type: 'output' as const, content: `(&muted)Published packages and CLI tools you can install directly.(&)` },
    { type: 'blank' as const, content: '' },
    ...packageSections
];
