import { IS_BROWSER } from '../../constants';

/**
 * Copies the given data to the clipboard.
 */
export async function copy(value: any) {
  // See:
  //    https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f
  if (IS_BROWSER) {
    if (typeof value === 'object') {
      value = JSON.stringify(value, null, '  ');
    }
    const el = document.createElement('textarea');
    el.value = value;
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
  }
}
