import React from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'

import { isString } from './util'
import Icon from './icon'
import classes from '../styles/tag.module.sass'

const Tag = ({ spaced, variant, tooltip, children }) => {
    if (variant === 'new') {
        const isValid = isString(children) && !isNaN(children)
        const version = isValid ? Number(children).toFixed(1) : children
        const tooltipText = `This feature is new and was introduced in spaCy v${version}`
        // TODO: we probably want to handle this more elegantly, but the idea is
        // that we can hide tags referring to old versions
        const hideTag = version.startsWith('2')
        return hideTag ? null : (
            <TagTemplate spaced={spaced} tooltip={tooltipText}>
                v{version}
            </TagTemplate>
        )
    }
    if (variant === 'model') {
        const tooltipText = `To use this functionality, spaCy needs a model to be installed that supports the following capabilities: ${children}`
        return (
            <TagTemplate spaced={spaced} tooltip={tooltipText}>
                Needs model
            </TagTemplate>
        )
    }
    return (
        <TagTemplate spaced={spaced} tooltip={tooltip}>
            {children}
        </TagTemplate>
    )
}

const TagTemplate = ({ spaced, tooltip, children }) => {
    const tagClassNames = classNames(classes.root, {
        [classes.spaced]: spaced,
    })
    return (
        <span className={tagClassNames} data-tooltip={tooltip}>
            {children}
            {tooltip && <Icon name="help" width={12} className={classes.icon} />}
        </span>
    )
}

Tag.defaultProps = {
    spaced: false,
}

Tag.propTypes = {
    spaced: PropTypes.bool,
    tooltip: PropTypes.string,
    children: PropTypes.node.isRequired,
}

export default Tag

@@ -656,24 +656,27 @@
 ions%0A       
+ //
  const hideT
@@ -722,25 +722,8 @@
 turn
- hideTag ? null :
  (%0A 

Revert change for now