import { ref } from "vue";

export function useClipboard(): { copied: typeof copied; copy: (value: string) => Promise<boolean> } {
  const copied = ref(false);

  async function copy(value: string): Promise<boolean> {
    if (!value) return false;
    try {
      await navigator.clipboard.writeText(value);
      copied.value = true;
      window.setTimeout(() => {
        copied.value = false;
      }, 1200);
      return true;
    } catch {
      copied.value = false;
      return false;
    }
  }

  return { copied, copy };
}
