<script setup>
import {inject, onMounted, ref} from "vue";

const Tea = inject("$Tea")

const props = defineProps(["v-element", "v-file", "v-value"])

const composeURL = function () {
	let text = ""
	if (props.vValue != null) {
		text = props.vValue
	} else {
		let e = document.getElementById(props.vElement)
		if (e == null) {
			// 不提示错误，因为此时可能页面未加载完整
			return
		}
		text = e.innerText
		if (text == null) {
			text = e.textContent
		}
	}
	return Tea.url("/ui/download", {
		file: file.value,
		text: text
	})
}

const {file, url} = (function () {
	let filename = props.vFile
	if (filename == null || filename.length == 0) {
		filename = "unknown-file"
	}
	return {
		file: ref(filename),
		url: ref(composeURL())
	}
})()

onMounted(() => {
	setTimeout(() => {
		url.value = composeURL()
	}, 1000)
})

</script>

<template>
	<a :href="url" target="_blank" style="font-weight: normal">
		<slot></slot>
	</a>
</template>

<style scoped>

</style>