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

const projectSections: TerminalLine[] = openSourceProjects.flatMap(project => {
    const hasImage = !!project.image;

    const infoChildren: TerminalLine[] = [
        { type: 'output' as const, content: `(&icon, mdi:source-branch) (&bg-blue,black) OPEN SOURCE (&)` },
        { type: 'output' as const, content: `(&text)${project.description}(&)` },
        {
            type: 'output' as const,
            content: project.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: [
                ...(project.github
                    ? [{ type: 'button' as const, content: 'View on GitHub', href: project.github, icon: 'mdi:github', style: 'accent' as const, flex: true }]
                    : []),
                ...(project.live
                    ? [{ type: 'button' as const, content: 'View Live Demo', href: project.live, icon: 'mdi:open-in-new', style: 'primary' as const, flex: true }]
                    : [])
            ]
        }
    ];

    return [
        { type: 'divider' as const, content: project.name.toUpperCase() },
        ...(hasImage
            ? [{ type: 'image' as const, content: '', image: project.image as string, imageAlt: project.name, imageWidth: 350, inline: true }]
            : []),
        { type: 'group' as const, content: '', inline: hasImage, 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/opensource' },
    { type: 'blank' as const, content: '', height: '1rem' },
    projectNavLinks,
    { type: 'blank' as const, content: '', height: '1rem' },
    { type: 'header' as const, content: 'Open Source' },
    { type: 'output' as const, content: `(&muted)Libraries and tools I build and maintain in the open.(&)` },
    { type: 'blank' as const, content: '' },
    ...projectSections
];
