import { datetimeToTime } from '@/utils/validate'
import { Ref } from 'vue'
import { CommonTableFormatter } from '~/common'

export function useFormatter(flag: Ref<boolean>) {
  const tableColumnFormatter = (columnLabel: string) => {
    return unref(flag) ? columnLabel : columnLabel.replace('/件', '')
  }

  const tableDataFormatter: CommonTableFormatter = (
    row,
    { label, property },
    cellValue,
    index
  ) => {
    if (!unref(flag)) {
      const matched = label.match(/(?<=\()\S+(?=\))/)
      if (matched) {
        const machedStr = matched[0]
        const countIndex = machedStr.split('/').findIndex(str => str === '件')
        if (
          countIndex > -1 &&
          typeof cellValue === 'string' &&
          cellValue.split('/')[countIndex]
        ) {
          return cellValue
            .split('/')
            .filter((_, index) => index !== countIndex)
            .join('/')
        }
      }
    }
    // 临时处理-没有发起货站交接这一节点数据时，不显示发起货站交接时间
    if (
      property === 'requestDepotJoinTime' &&
      !row["concat(requestDepotJoinBoard,'/',requestDepotJoin)"]
    ) {
      return ''
    }
    if (property.includes('Time') && typeof cellValue === 'string') {
      // if (
      //   ['planDepartureTime', 'acLandingTime', 'planLandingTime'].includes(
      //     property
      //   )
      // ) {
      //   return cellValue
      //     .trim()
      //     .slice(5, -3)
      //     .replace(/[T|\s]+/, '\n')
      // } else {
      //   return datetimeToTime(cellValue, row.flightDate)
      // }
      return datetimeToTime(cellValue, row.flightDate)
    }
    if (property === 'jiahuo') {
      return cellValue ? '是' : '否'
    }

    return String(cellValue ?? '')
  }

  return {
    tableColumnFormatter,
    tableDataFormatter,
  }
}
