import { cloneElement, type ReactElement } from 'react'
import { Linking } from 'react-native'
import { isWeb } from '@junwon/aesthetics'

interface LinkProps {
  href: string
  children: ReactElement
}

export function Link({ href, children }: LinkProps) {
  if (isWeb) {
    return (
      <a
        href={href}
        target="_blank"
        rel="noopener noreferrer"
        style={{ textDecoration: 'none' }}
      >
        {children}
      </a>
    )
  }

  return cloneElement(children, {
    onPress: () => Linking.openURL(href),
  })
}
