<script setup>
const props = defineProps({
  row: {
    type: Object,
    required: true,
    default: {}
  }
})

const relations = ref([])

const normalizeId = (value) => {
  if (value === null || value === undefined || value === "") return null
  if (typeof value === "object") return normalizeId(value.id)
  const parsed = Number(value)
  return Number.isNaN(parsed) ? String(value) : parsed
}

const relationLabel = computed(() => {
  const relation = props.row?.memberrelation
  if (relation && typeof relation === "object" && relation.type) return relation.type

  const id = normalizeId(relation)
  if (!id) return ""
  return relations.value.find((i) => normalizeId(i.id) === id)?.type || ""
})

const relationId = computed(() => {
  return normalizeId(props.row?.memberrelation)
})

const loadRelations = async () => {
  try {
    relations.value = await useEntities("memberrelations").select()
  } catch (e) {
    relations.value = []
  }
}

loadRelations()
</script>

<template>
  <NuxtLink
      v-if="relationId && relationLabel"
      :to="`/standardEntity/memberrelations/show/${relationId}`"
      class="text-primary"
  >
    {{ relationLabel }}
  </NuxtLink>
  <span v-else>{{ relationLabel }}</span>
</template>
