use arrow_array::RecordBatch;
use crate::column_types::models::ColumnType;
use crate::sheets_source::_helpers::cells::value_text;
use crate::sheets_source::_helpers::layout::data_rows;
use crate::sheets_source::main::sheet_batch::sheet_batch;
use crate::sheets_source::main::sheet_table::sheet_table;
use crate::sheets_source::models::{Cell, Grid, HeaderRule};
pub fn sheet_input(grid: &Grid, rule: &HeaderRule) -> Result<RecordBatch, String> {
let mut cleaned = grid.clone();
for cell in cleaned.rows.iter_mut().flatten() {
if *cell == Cell::Text(String::new()) {
*cell = Cell::Empty;
}
}
let table = sheet_table(&cleaned, rule).map_err(|problems| problems.join("; "))?;
let mut columns: Vec<(String, ColumnType)> = Vec::with_capacity(table.columns.len());
for column in &table.columns {
columns.push((column.name.clone(), column.column_type.clone()?));
}
let rows = data_rows(&cleaned, rule, columns.len());
for (position, (_, column_type)) in columns.iter().enumerate() {
if *column_type != ColumnType::Text {
continue;
}
for row in &rows {
if let Some(Cell::Number { value, kind, shown }) = cleaned.rows[*row].get_mut(position)
{
*shown = value_text(*value, *kind);
}
}
}
sheet_batch(&cleaned, rule, &columns)
}