import { computed, h } from 'vue'
import { formInputViewProps, fromInputEmits } from '../../form-input-props'
import { isNotEmpty, isArray, isObject, getUsingConfig } from '@cip/utils/util'
import { useOptions, useFormView } from '@cip/components/hooks/form-input'
export default {
  props: { ...formInputViewProps, multiple: Boolean },
  emits: [...fromInputEmits],
  setup (props) {
    const { securityConfig, proxyOtherValue, inputStyle } = useFormView(props)
    const multiple = computed(() => {
      return getUsingConfig(securityConfig.value.multiple, props.multiple)
    })
    const { getValue, getOtherValue, optionProps, splitKey } = useOptions(props, multiple)

    const viewValue = computed(() => {
      if (isNotEmpty(proxyOtherValue[0]?.value)) return proxyOtherValue[0]?.value
      const value = getValue(props.modelValue)
      const otherValue = getOtherValue(props.modelValue, value) // || props.modelValue
      if (isArray(otherValue)) {
        if (isObject(otherValue[0])) {
          return otherValue.map(i => i[optionProps.value.label]).join(`${splitKey.value} `)
        }
        return otherValue.join(`${splitKey.value} `)
      }
      if (isObject(otherValue)) {
        return otherValue[optionProps.value.value]
      }
      return otherValue
    })
    return () => h('span', { style: inputStyle.value }, [viewValue.value])
  }
}
