import { computed, unref, ref } from 'vue'
import { isBoolean } from '@/utils/is'

export function usePagination(refProps) {
  const configRef = ref({})
  const getPaginationInfo = computed(() => {
    const { pagination, paginationSetting } = unref(refProps)
    /* 判断是否需要展示分页 */
    if ((isBoolean(pagination) && !pagination)) {
      return false
    }
    /* 返回配置的分页信息 */
    return {
      pageSize: paginationSetting.pageSize,
      pageSizes: paginationSetting.pageSizes,
      showSizePicker: paginationSetting.showSizePicker,
      showQuickJumper: paginationSetting.showQuickJumper,
      ...(isBoolean(pagination) ? {} : pagination),
      ...unref(configRef),
      pageCount: unref(configRef)[paginationSetting.totalField],
      prefix({ itemCount }) {
        return `共 ${itemCount} 条`
      }
    }
  })

  function setPagination(info) {
    const paginationInfo = unref(getPaginationInfo)
    configRef.value = {
      ...(!isBoolean(paginationInfo) ? paginationInfo : {}),
      ...info
    }
  }

  function getPagination() {
    return unref(getPaginationInfo)
  }

  return { getPaginationInfo, setPagination, getPagination }
}
