<script setup>
import AccountDescriptorProxy from 'src/pages/Account/Card/AccountDescriptorProxy.vue';
import WorkerDescriptorProxy from 'src/pages/Worker/Card/WorkerDescriptorProxy.vue';
import { ref, onMounted } from 'vue';
import axios from 'axios';
const $props = defineProps({
    name: { type: String, default: null },
    tag: { type: String, default: null },
    workerId: { type: Number, default: null },
    defaultName: { type: Boolean, default: false },
});
const isWorker = ref(false);

onMounted(async () => {
    try {
        const {
            data: { exists },
        } = await axios(`/Workers/${$props.workerId}/exists`);
        isWorker.value = exists;
    } catch (error) {
        if (error.status === 403) return;
        throw error;
    }
});
</script>
<template>
    <slot name="link">
        <span :class="{ link: workerId }">
            {{ defaultName ? (name ?? $t('globals.system')) : name }}
        </span>
    </slot>
    <WorkerDescriptorProxy
        v-if="isWorker"
        :id="workerId"
        @on-fetch="(data) => (isWorker = data?.workerId !== undefined)"
    />
    <AccountDescriptorProxy v-else :id="workerId" />
</template>
