import React, { FC, useCallback } from 'react';

import classNames from 'classnames';

import { Icon } from '~/components/input/Icon';

import styles from './styles.module.scss';

interface IProps {
  size?: string;
  color?: 'primary' | 'danger' | 'info' | 'black' | 'default';
  deletable?: boolean;
  hoverable?: boolean;
  editing?: boolean;
  hasInput?: boolean;
  onClick?: () => void;
  onDelete?: () => void;
  title?: string;
}

const TagWrapper: FC<IProps> = ({
  color = 'default',
  children,
  size,
  deletable,
  hoverable,
  editing,
  hasInput,
  onClick,
  onDelete,
  title = '',
}) => {
  const canBeDeleted = deletable && !editing && !hasInput;
  const onDeletePress = useCallback(
    (event) => {
      if (!onDelete) {
        return;
      }

      event.stopPropagation();
      onDelete();
    },
    [onDelete],
  );

  return (
    <div
      className={classNames(
        styles.tag,
        size && styles[`size-${size}`],
        styles[`color-${color}`],
        {
          [styles.hoverable]: hoverable,
          [styles.editing]: editing,
          [styles.deletable]: canBeDeleted,
          [styles.input]: hasInput,
          [styles.clickable]: onClick,
        },
      )}
      onClick={onClick}
    >
      <div className={styles.hole} />

      <div className={styles.text}>
        <div className={styles.title}>{title}</div>
        {children}
      </div>

      {deletable && (
        <button type="button" className={styles.delete} onClick={onDeletePress}>
          <Icon icon="close" size={12} />
        </button>
      )}
    </div>
  );
};

export { TagWrapper };
