// @flow
import React from 'react';
import styled from 'styled-components';
import { FormattedNumber } from 'react-intl';
import type { CurrencyProps } from './currency-formatter.proptypes';

const StyledWrapper = styled.span`
  display: inline-block;
  &::first-letter {
    margin-right: 0.1em;
  }
`;

export const CurrencyFormatter = ({
  showPlusSign = false,
  ...props
}: CurrencyProps) => {
  const { value, currency } = props;

  return (
    <StyledWrapper>
      {showPlusSign && value > 0 ? '+' : null}
      <FormattedNumber
        value={value}
        minimumFractionDigits={2}
        currencyDisplay="symbol"
        currency={currency}
        style="currency" // eslint-disable-line
        {...props}
      />
    </StyledWrapper>
  );
};

CurrencyFormatter.displayName = 'CurrencyFormatter';
