"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListColumn = ListColumn;
const jsx_runtime_1 = require("react/jsx-runtime");
const ink_1 = require("ink");
const Card_1 = require("./Card");
const NavigationContext_1 = require("../state/NavigationContext");
function ListColumn({ list, cards, listIndex, isSelectedList, selectedCardIndex, width, height, maxCardRows, }) {
    const { config } = (0, NavigationContext_1.useNavigation)();
    // Scroll cards if there are more than fit
    const visibleCards = getVisibleCards(cards, selectedCardIndex, maxCardRows);
    return ((0, jsx_runtime_1.jsxs)(ink_1.Box, { flexDirection: "column", width: width, height: height, borderStyle: isSelectedList ? "bold" : "single", borderColor: isSelectedList ? config.theme.primary : config.theme.muted, marginRight: 1, children: [(0, jsx_runtime_1.jsxs)(ink_1.Box, { paddingX: 1, justifyContent: "space-between", children: [(0, jsx_runtime_1.jsx)(ink_1.Text, { bold: true, color: isSelectedList ? config.theme.primary : undefined, children: truncate(list.name, width - 8) }), (0, jsx_runtime_1.jsxs)(ink_1.Text, { color: config.theme.muted, children: ["[", listIndex + 1, "]"] })] }), visibleCards.scrollTop > 0 && ((0, jsx_runtime_1.jsx)(ink_1.Box, { justifyContent: "center", children: (0, jsx_runtime_1.jsxs)(ink_1.Text, { color: config.theme.muted, children: ["-- ", visibleCards.scrollTop, " more --"] }) })), visibleCards.cards.length === 0 ? ((0, jsx_runtime_1.jsx)(ink_1.Box, { paddingX: 1, paddingY: 1, children: (0, jsx_runtime_1.jsx)(ink_1.Text, { color: config.theme.muted, italic: true, children: "No cards" }) })) : (visibleCards.cards.map((card, i) => ((0, jsx_runtime_1.jsx)(ink_1.Box, { children: (0, jsx_runtime_1.jsx)(Card_1.Card, { card: card, selected: isSelectedList && visibleCards.startIndex + i === selectedCardIndex, maxWidth: width - 2 }) }, card.id)))), visibleCards.scrollBottom > 0 && ((0, jsx_runtime_1.jsx)(ink_1.Box, { justifyContent: "center", children: (0, jsx_runtime_1.jsxs)(ink_1.Text, { color: config.theme.muted, children: ["-- ", visibleCards.scrollBottom, " more --"] }) })), isSelectedList && ((0, jsx_runtime_1.jsx)(ink_1.Box, { paddingX: 1, children: (0, jsx_runtime_1.jsx)(ink_1.Text, { color: config.theme.muted, children: "[n] + New card" }) }))] }));
}
function getVisibleCards(cards, selectedIndex, maxRows) {
    if (cards.length <= maxRows) {
        return {
            cards,
            startIndex: 0,
            scrollTop: 0,
            scrollBottom: 0,
        };
    }
    // Keep selected card visible with some context
    let start = Math.max(0, selectedIndex - Math.floor(maxRows / 2));
    let end = start + maxRows;
    if (end > cards.length) {
        end = cards.length;
        start = Math.max(0, end - maxRows);
    }
    return {
        cards: cards.slice(start, end),
        startIndex: start,
        scrollTop: start,
        scrollBottom: cards.length - end,
    };
}
function truncate(str, maxLen) {
    if (maxLen < 4)
        return str.slice(0, maxLen);
    if (str.length <= maxLen)
        return str;
    return str.slice(0, maxLen - 3) + "...";
}
