/**
 * SummaryDataTable - Read-only table renderer for data table fields
 *
 * Used by summary, print, and approval views to render DataTableRow[]
 * as a proper HTML table instead of "[object Object]".
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { cn } from '../../../utils/cn.js';
import { format_date_value } from '../../shared/summary_utils/index.js';
/**
 * Calculate aggregation value for a column
 */
function calculate_aggregation(column, rows) {
    if (!column.aggregation || column.aggregation.type === 'none') {
        return null;
    }
    const values = rows
        .map((row) => {
        const val = row[column.id];
        if (typeof val === 'number')
            return val;
        if (typeof val === 'string') {
            const parsed = parseFloat(val);
            return isNaN(parsed) ? null : parsed;
        }
        return null;
    })
        .filter((val) => val !== null);
    switch (column.aggregation.type) {
        case 'sum':
            return values.reduce((acc, val) => acc + val, 0);
        case 'average':
            return values.length > 0
                ? values.reduce((acc, val) => acc + val, 0) / values.length
                : 0;
        case 'count':
            return values.length;
        default:
            return null;
    }
}
/**
 * Format number for display with optional decimals
 */
function format_number(value, decimals) {
    return value.toLocaleString(undefined, {
        minimumFractionDigits: decimals ?? 2,
        maximumFractionDigits: decimals ?? 2,
    });
}
/**
 * Format a cell value based on column type
 */
function format_cell_value(value, column, date_format) {
    if (value === undefined || value === null || value === '') {
        return '';
    }
    switch (column.field_type) {
        case 'checkbox':
            return value === true || value === 'true' ? 'Yes' : 'No';
        case 'dropdown':
        case 'radiobutton': {
            const option = column.options?.find(opt => opt.value === String(value));
            return option?.label ?? String(value);
        }
        case 'date':
            return format_date_value(value, date_format);
        case 'numeric': {
            const num = typeof value === 'number' ? value : parseFloat(String(value));
            if (isNaN(num))
                return String(value);
            return format_number(num, column.constraints?.num_decimals);
        }
        case 'files':
            if (Array.isArray(value)) {
                return `${value.length} file${value.length !== 1 ? 's' : ''}`;
            }
            return '';
        default:
            return String(value);
    }
}
/**
 * SummaryDataTable - renders a read-only HTML table from DataTableRow[]
 */
export function SummaryDataTable({ table_config, rows, date_format = 'iso', empty_message, class_name, }) {
    const { columns, show_row_numbers, show_subtotal_row } = table_config;
    // Check if any column has aggregation configured
    const has_aggregation = columns.some(col => col.aggregation && col.aggregation.type !== 'none');
    const should_show_subtotal = (show_subtotal_row !== false) && has_aggregation;
    if (rows.length === 0) {
        return (_jsx("p", { className: "text-sm text-muted-foreground italic", children: empty_message || table_config.empty_message || 'No entries' }));
    }
    return (_jsx("div", { className: cn('overflow-x-auto', class_name), children: _jsxs("table", { className: "w-full text-sm font-semibold border-collapse border border-border rounded", children: [_jsx("thead", { children: _jsxs("tr", { className: "bg-muted/50", children: [show_row_numbers && (_jsx("th", { className: "px-3 py-2 text-left text-xs font-medium text-muted-foreground border-b border-border w-10", children: "#" })), columns.map(col => (_jsx("th", { className: cn('px-3 py-2 text-left text-xs font-medium text-muted-foreground border-b border-border', col.width), children: col.label }, col.id)))] }) }), _jsx("tbody", { children: rows.map((row, row_idx) => (_jsxs("tr", { className: "border-b border-border last:border-b-0", children: [show_row_numbers && (_jsx("td", { className: "px-3 py-2 text-muted-foreground text-xs", children: row_idx + 1 })), columns.map(col => (_jsx("td", { className: cn('px-3 py-2', col.field_type === 'numeric' && 'text-right font-mono', col.background_color), children: format_cell_value(row[col.id], col, date_format) }, col.id)))] }, row._row_id || `row_${row_idx}`))) }), should_show_subtotal && (_jsx("tfoot", { children: _jsxs("tr", { className: "bg-muted/30 font-bold border-t-2 border-border", children: [show_row_numbers && _jsx("td", { className: "px-3 py-2" }), columns.map(col => {
                                const agg_value = calculate_aggregation(col, rows);
                                const label = col.aggregation?.label || col.aggregation?.type;
                                return (_jsx("td", { className: cn('px-3 py-2 text-sm', col.field_type === 'numeric' && 'text-right font-mono'), children: agg_value !== null
                                        ? format_number(agg_value, col.constraints?.num_decimals)
                                        : (label && col.aggregation?.type !== 'none' ? '' : '') }, col.id));
                            })] }) }))] }) }));
}
//# sourceMappingURL=summary_data_table.js.map