import noop from 'lodash/noop';

import { getGridCustomColumnTypeByField, getGridCustomColumnTypes } from '../index';
import type { GridCustomColumnTypes, GridRenderCellParams } from '../index';

export const getRenderCellParams = (props: {
  colDef: GridRenderCellParams['colDef'];
  colType: GridCustomColumnTypes;
  field?: GridRenderCellParams['field'];
  isAutoGenerated?: GridRenderCellParams['rowNode']['isAutoGenerated'];
  row: GridRenderCellParams['row'];
  value?: GridRenderCellParams['value'];
}) => {
  const { colDef, colType, field = colType, isAutoGenerated = false, row, value } = props;
  return {
    api: {},
    align: 'left' as const,
    cellMode: 'view' as GridRenderCellParams['cellMode'],
    children: value,
    colDef,
    colIndex: 0,
    field,
    getValue: noop,
    hasFocus: false,
    height: 32,
    id: row._id,
    row,
    rowId: 0,
    rowNode: { isAutoGenerated } as GridRenderCellParams['rowNode'],
    tabIndex: 0 as GridRenderCellParams['tabIndex'],
    value,
    width: 32
  };
};

export const renderCellByField = (props: {
  field: string;
  rowData?: GridRenderCellParams['row'];
}) => {
  const { field, rowData } = props;
  const colType = getGridCustomColumnTypeByField(field);
  const colTypeDef = { ...getGridCustomColumnTypes()[colType], field };
  const row = rowData || { _id: 'test_uid' };
  const value = row[field];
  const renderCellParams = getRenderCellParams({
    colDef: { ...colTypeDef, field } as GridRenderCellParams['colDef'],
    colType,
    field,
    row,
    value
  });
  return colTypeDef.renderCell?.(renderCellParams);
};

export const renderCellByType = (props: {
  colType: GridCustomColumnTypes;
  rowData?: GridRenderCellParams['row'];
  value?: GridRenderCellParams['value'];
}) => {
  const { colType, rowData, value } = props;
  const colTypeDef = getGridCustomColumnTypes()[colType];
  const row = rowData || { _id: 'test_uid' };
  const renderCellParams = getRenderCellParams({
    colDef: colTypeDef as GridRenderCellParams['colDef'],
    colType,
    row,
    value
  });
  return colTypeDef.renderCell?.(renderCellParams);
};
