import { getCurrentInstance, computed, ref } from 'vue'
import { curry } from 'lodash-es'
import type { EpFormItem, FormItemColProp, FormItemRowProp, InstanceForm } from 'global-type'

type RowKey = FormItemRowProp['rowKey']
type ColProp = FormItemColProp['colProp']

const _ = curry.placeholder

const defineColumn = curry<EpFormItem, RowKey, ColProp, EpFormItem>((item, rowKey, colProp) => {
  return { rowKey, colProp, ...item } as EpFormItem
})

const useForm = () => {
  const instance = getCurrentInstance()

  if (!instance) {
    return { _, defineColumn }
  }

  const formRef = ref<InstanceForm>()

  const elFormRef = computed(() => {
    return formRef?.value?.formRef
  })

  const model = computed(() => {
    return elFormRef.value?.model
  })

  return {
    elFormRef,
    formRef,
    model,
    _,
    defineColumn,
  }
}

export default useForm
