import Button from '../button'
import { vui } from '../core'
import { IconProp } from '../icon'
import { cs } from '../utils'
import { CopyToClipboardProps } from './copyToClipboard.types'
import { useCopyToClipboard } from './useCopyToClipboard'

const defaultIcon: IconProp = 'falCopy'

/**
 * A helper button component.
 *
 * Copies the provided text to clipboard.
 *
 * Copying is available only for a secure environment (https://), otherwise the button is disabled.
 *
 */
export const CopyToClipboard = vui<'button', CopyToClipboardProps>((props, ref) => {
  const { disabled, copyText, className, icon = defaultIcon, size = 'sm', variant = 'tertiaryDark', ...rest } = props

  const { copy, isCopyDisabled } = useCopyToClipboard(copyText)

  return (
    <Button
      aria-label={`Copy to clipboard ${copyText}`}
      className={cs('vui-copy-to-clipboard', className)}
      disabled={disabled || isCopyDisabled}
      icon={icon}
      onClick={() => copy()}
      ref={ref}
      size={size}
      variant={variant}
      {...rest}
    />
  )
})

CopyToClipboard.displayName = 'CopyToClipboard'
export default CopyToClipboard
