import Handsontable, { GridSettings } from '@sc/handsontable';
import { addClass, append, empty, style } from '@/utils/frontend/dom';
import { Hot } from '@/types/components';

export default function createRenderer(renderer: Hot.IRenderer) {
  // eslint-disable-next-line func-names
  return function (
    instance: Handsontable,
    TD: HTMLElement,
    row: number,
    col: number,
    prop: string | number,
    value: any,
    cellProperties: GridSettings
  ): HTMLElement {
    empty(TD);
    style(TD, { padding: '0' });

    const container = renderer(instance, row, col, prop, value, cellProperties);

    // 设置自定义className
    const { className } = cellProperties;
    const classList = [];
    if (typeof className === 'string') {
      classList.push(...className.split(' '));
    } else if (className instanceof Array) {
      classList.push(...className);
    }
    addClass(TD, ...classList);
    append(TD, container);
    return container;
  };
}
