<script setup lang="ts">
import { getUrlForLocale } from '~/src/domain.ts';
import type { LocaleCode } from '~~/locale/locales.ts';

const props = defineProps<{
    link: string;
    locale?: LocaleCode;
}>();

const config = useConfig();

const isExternal = computed(() => !props.link.startsWith('/'));

const to = computed(() => {
    if (isExternal.value) {
        return props.link;
    }
    if (props.locale !== undefined && props.locale !== config.locale) {
        return getUrlForLocale(props.locale) + props.link;
    }
    return props.link;
});
</script>

<template>
    <NuxtLink :to :no-rel="!isExternal" :target="isExternal ? '_blank' : undefined">
        <slot></slot>
    </NuxtLink>
</template>
