<script setup>
import { computed } from 'vue'
import { useI18n } from 'vue-i18n';
import dayjs from 'dayjs/esm'
import relativeTime from 'dayjs/esm/plugin/relativeTime'
import utc from 'dayjs/esm/plugin/utc'
import 'dayjs/esm/locale/de.js'
dayjs.extend(relativeTime)
dayjs.extend(utc)

const props = defineProps({
    iso: {
        type: String,
        default: ""
    },
    unix: {
        type: Number,
        default: 0
    }
});

const { locale } = useI18n();

const tzOffset = (new Date()).getTimezoneOffset();

// compute difference from given iso string and now whenever data changes
const lastUpdate = computed(() => {
    let lastUpdateTime;

    if (props.iso) {
        lastUpdateTime = dayjs(props.iso).utc(true);
    } else {
        // lastUpdateTime = dayjs(props.unix * 1000);
        lastUpdateTime = dayjs(props.unix * 1000).utcOffset(tzOffset).utc(true);
    }

    return lastUpdateTime.locale(locale.value).fromNow();
});

</script>
<template>
    {{ lastUpdate }}
</template>
<style scoped>
</style>

