// @flow
import React, { type ComponentType } from 'react';
import styled, { css } from 'styled-components';
import type { TextProps } from './text.proptypes';
import {
  getTextAlign,
  getFontSize,
  getFontStyle,
  getFontFamily,
  getTextDecoration,
  getTextTransform,
  getFontWeight,
  getTabularStyles,
} from '../../../mixins/typography';
import { getForegroundColor } from '../../../mixins/branding';
import { SemanticHeader } from '../../../mixins/semantic-header-level';
import { getSpacingRules } from '../../../mixins/spacing';
import { getStackeeRules } from '../../../mixins/stackee';

const commonStyles = css`
  line-height: 1.4;
  margin: 0 0 1em 0;
  ${getTabularStyles}
`;

const StyledP = styled.p`
  ${commonStyles}
  color: ${getForegroundColor};
  text-align: ${getTextAlign};
  font-size: ${getFontSize};
  font-style: ${getFontStyle};
  font-family: ${getFontFamily};
  font-weight: ${getFontWeight};
  text-decoration: ${getTextDecoration};
  text-transform: ${getTextTransform};
  ${getSpacingRules};
  ${getStackeeRules};
`;

const StyledHeader = styled(SemanticHeader)`
  ${commonStyles}
`;

export const Text: ComponentType<TextProps> = ({
  headerLevel = 'none',
  ...props
}: TextProps) => {
  return headerLevel === 'none' ? (
    <StyledP {...props} />
  ) : (
    <StyledHeader {...props} headerLevel={headerLevel} />
  );
};

Text.displayName = 'Text';
