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';

const costSwitchRender = (
  instance: Handsontable,
  row: number,
  col: number,
  prop: string | number,
  value: any,
  cellProperties: GridSettings
) => {
  let code = value || '';
  // 当前行的源数据
  const rowSourceData = instance.getSourceDataAtRow(row) as IRation;
  const { adjustState } = rowSourceData;
  // 定额显示借 换 补 等
  if (rowSourceData.kind === BRType.RATION) {
    const prefix = rowSourceData.prefix || '';
    const tailStr = adjustState ? RationPrefix.REP : '';
    code = prefix + code + tailStr;
  }
  const container = switcherRenderer(instance, row, col, prop, code, cellProperties);
  return container;
};

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