import { bindPopover } from '@/components/popover/composables/usePopover';
import { GridSettings } from '@sc/handsontable';
import { BRType, IRation, RationPrefix } from '@sc/types';
import createRenderer from '../createRenderer';
import switcherRenderer from './switcherRenderer';

type callback = (...args: any) => Promise<string>;

export default function getPopoverSwitcherRender(callback: callback) {
  return async (
    instance: Handsontable,
    TD: HTMLElement,
    row: number,
    col: number,
    prop: string | number,
    value: any,
    cellProperties: GridSettings
  ) => {
    const rowSourceData = instance.getSourceDataAtRow(row);
    const nativeRender = createRenderer(switcherRenderer);
    const container = nativeRender(instance, TD, row, col, prop, value, cellProperties);
    const content = await callback(rowSourceData);
    // 有定额调整状态的要悬浮提示
    if (content) {
      bindPopover(TD, container, content);
    }
    return TD;
  };
}
