export const createCopyFeedback = (resetAfter = 1500) => {
	let lastCopied = $state<string | null>(null);
	let timer: ReturnType<typeof setTimeout> | undefined;

	$effect(() => () => clearTimeout(timer));

	return {
		get copied() {
			return lastCopied;
		},
		copy: async (label: string, value: string = label) => {
			try {
				await navigator.clipboard.writeText(value);
			} catch {
				return;
			}
			lastCopied = label;
			clearTimeout(timer);
			timer = setTimeout(() => (lastCopied = null), resetAfter);
		}
	};
};
