<template>
	<component
		:is="'script'"
		type="application/ld+json"
		v-text="jsonLd"
	/>
</template>

<script setup>
/**
 * XSchema — JSON-LD from a schema prop or from xMarketing name/url.
 * Does not invent a business. Emits only fields that are actually set.
 */
const props = defineProps({
	/** Full schema.org object. When set, this is the payload. */
	schema: {
		type: Object,
		default: null,
	},
	/** Schema.org @type when building from name/url. */
	type: {
		type: String,
		default: "WebSite",
	},
	name: {
		type: String,
		default: "",
	},
	url: {
		type: String,
		default: "",
	},
});

const appConfig = useAppConfig();

const jsonLd = computed(() => {
	if (props.schema && typeof props.schema === "object") {
		const payload = { "@context": "https://schema.org", ...props.schema };
		return JSON.stringify(payload);
	}

	const name = props.name || appConfig?.xMarketing?.name;
	const url = props.url || appConfig?.xMarketing?.url;
	const data = {
		"@context": "https://schema.org",
		"@type": props.type || "WebSite",
	};
	if (name) data.name = name;
	if (url) data.url = url;
	return JSON.stringify(data);
});
</script>
