// @flow
import { css } from 'styled-components';
import type { ThemeProps } from '../../../types';
import { getPaletteColor } from '../../../mixins/branding';
import { getSpacingRules } from '../../../mixins/spacing';
import { getStackeeRules } from '../../../mixins/stackee';
import {
  getFontFamily,
  getFontWeight,
  getFontSize,
} from '../../../mixins/typography';
import {
  FOCUS_VISIBLE_SELECTOR,
  getBaseFocusState,
  focusedBoxShadow,
  focusedBorderShadow,
  focusedBorderColor,
} from '../../../mixins/focus';

export const getBorderRadius = ({ theme }: ThemeProps) =>
  theme.buttonBorderRadius;

export const getBorderColor = ({
  hideBorder,
  ...rest
}: {|
  ...ThemeProps,
  hideBorder: boolean,
|}) => (hideBorder ? 'transparent' : getPaletteColor('greyscale-light')(rest));

export const getBorderColorHover = ({
  hideBorder,
  ...rest
}: {|
  ...ThemeProps,
  hideBorder: boolean,
|}) => (hideBorder ? 'transparent' : getPaletteColor('greyscale-medium')(rest));

export const getBorderColorActive = ({
  hideBorder,
  ...rest
}: {|
  ...ThemeProps,
  hideBorder: boolean,
|}) => (hideBorder ? 'transparent' : getPaletteColor('greyscale-dark')(rest));

export const commonStyles = css`
  && {
    display: inline-block;
    min-width: 80px;
    height: 32px;
    border-width: 1px;
    border-style: solid;
    border-radius: ${getBorderRadius};
    background-color: ${getPaletteColor('white')};
    border-color: ${getBorderColor};
    color: ${getPaletteColor('greyscale-dark')};
    text-decoration: none;
    font-size: ${getFontSize};
    font-family: ${getFontFamily};
    font-weight: ${getFontWeight};
    padding: 0 15px;
    ${getBaseFocusState({})}
  }
  ${FOCUS_VISIBLE_SELECTOR} {
    border-color: transparent;
    box-shadow: ${focusedBoxShadow},
      ${focusedBorderShadow} ${focusedBorderColor};
  }

  &[disabled] {
    color: ${getPaletteColor('greyscale-medium')};
    border-color: ${getPaletteColor('greyscale-light')};
  }

  &&& {
    ${getSpacingRules}
  }
  ${getStackeeRules};
`;

export const buttonStyles = css`
  line-height: 100%;

  &:enabled:hover {
    border-color: ${getBorderColorHover};
  }

  &:enabled:active {
    color: ${getPaletteColor('greyscale-black')};
    border-color: ${getBorderColorActive};
  }
`;

export const linkStyles = css`
  line-height: 32px;
  text-align: center;
  text-decoration: none;

  &:hover {
    border-color: ${getBorderColorHover};
  }
  &:active {
    color: ${getPaletteColor('greyscale-black')};
    border-color: ${getBorderColorActive};
  }
`;
