"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Card = Card;
const jsx_runtime_1 = require("react/jsx-runtime");
const ink_1 = require("ink");
const NavigationContext_1 = require("../state/NavigationContext");
const LABEL_COLORS = {
    green: "green",
    yellow: "yellow",
    orange: "#FFA500",
    red: "red",
    purple: "magenta",
    blue: "blue",
    sky: "cyan",
    lime: "greenBright",
    pink: "magentaBright",
    black: "white",
};
function Card({ card, selected, maxWidth }) {
    const { config } = (0, NavigationContext_1.useNavigation)();
    const truncatedName = truncate(card.name, maxWidth - 4);
    const dueInfo = getDueInfo(card.due);
    const hasLabels = card.labels && card.labels.length > 0;
    return ((0, jsx_runtime_1.jsxs)(ink_1.Box, { flexDirection: "column", paddingX: 1, children: [(0, jsx_runtime_1.jsx)(ink_1.Box, { children: (0, jsx_runtime_1.jsxs)(ink_1.Text, { color: selected ? config.theme.primary : undefined, bold: selected, children: [selected ? "> " : "  ", truncatedName] }) }), (hasLabels || dueInfo) && ((0, jsx_runtime_1.jsxs)(ink_1.Box, { paddingLeft: 2, children: [hasLabels &&
                        card.labels.map((label) => ((0, jsx_runtime_1.jsxs)(ink_1.Text, { color: LABEL_COLORS[label.color] || config.theme.muted, children: ["#", label.name || "", " "] }, label.id))), dueInfo && ((0, jsx_runtime_1.jsxs)(ink_1.Text, { color: dueInfo.color, children: [dueInfo.icon, " ", dueInfo.text, " "] }))] }))] }));
}
function truncate(str, maxLen) {
    if (str.length <= maxLen)
        return str;
    return str.slice(0, maxLen - 3) + "...";
}
function getDueInfo(due) {
    if (!due)
        return null;
    const dueDate = new Date(due);
    const now = new Date();
    const diffMs = dueDate.getTime() - now.getTime();
    const diffDays = Math.ceil(diffMs / (1000 * 60 * 60 * 24));
    if (diffDays < 0) {
        return { text: "Overdue", color: "red", icon: "!!" };
    }
    if (diffDays === 0) {
        return { text: "Today", color: "yellow", icon: ">>" };
    }
    if (diffDays === 1) {
        return { text: "Tomorrow", color: "yellow", icon: ">>" };
    }
    if (diffDays <= 7) {
        return { text: `${diffDays}d`, color: "green", icon: ".." };
    }
    return { text: formatShortDate(dueDate), color: "gray", icon: ".." };
}
function formatShortDate(date) {
    const months = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ];
    return `${months[date.getMonth()]} ${date.getDate()}`;
}
