<script>
import { Ellipsis } from '@vue2-client/components'
import { executeStrFunctionByContext } from '@vue2-client/utils/runEvalFunction'
import { getConfigByNameAsync, runLogic } from '@vue2-client/services/api/common'

export default {
  name: 'CustomFuncCel',
  methods: {
    runLogic,
    async fetchSlotValue () {
      try {
        let func = this.item.slotCustomFunction
        if (this.item.slotCustomFunction.startsWith('function')) {
          func = `async ${func}`
        }
        this.slotValueResult = await executeStrFunctionByContext(this, func, [this.text, this.record, this.localDataSource, this.index, runLogic, getConfigByNameAsync])
        this.loading = false
      } catch (error) {
        console.error('Error fetching slot value:', error)
      }
    }
  },
  mounted () {
    this.fetchSlotValue()
  },
  data: () => {
    return {
      slotValueResult: '',
      loading: true
    }
  },
  components: { Ellipsis },
  // eslint-disable-next-line
  props: ['item', 'text', 'record', 'index', 'localDataSource'],
}
</script>

<template>
  <span>
    {{ loading ? text : slotValueResult }}
  </span>
</template>

<style scoped lang="less">

</style>
