/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/

/* eslint-disable flowtype/no-weak-types */
import { capitalize, fromPopperPlacement, getOppositePosition, getPopoverMarginStyles, getArrowPositionStyles, getEndPosition, getStartPosition, isVerticalPosition, parsePopperOffset, splitPlacement, toPopperPlacement } from '../utils.js';
describe('Popover utils', function () {
  describe('getOppositePosition', function () {
    test('getOppositePosition should return opposite positions', function () {
      expect(getOppositePosition('top')).toBe('bottom');
      expect(getOppositePosition('bottom')).toBe('top');
      expect(getOppositePosition('left')).toBe('right');
      expect(getOppositePosition('right')).toBe('left');
    });
  });
  describe('capitalize', function () {
    test('capitalize should capitalize input strings', function () {
      expect(capitalize('top')).toBe('Top');
      expect(capitalize('left')).toBe('Left');
      expect(capitalize('')).toBe('');
      expect(capitalize('topLeft')).toBe('TopLeft');
    });
  });
  describe('toPopperPlacement', function () {
    test('toPopperPlacement should convert from popover placements to popper placements', function () {
      expect(toPopperPlacement('rightTop')).toBe('right-start');
      expect(toPopperPlacement('right')).toBe('right');
      expect(toPopperPlacement('rightBottom')).toBe('right-end');
      expect(toPopperPlacement('topLeft')).toBe('top-start');
      expect(toPopperPlacement('top')).toBe('top');
      expect(toPopperPlacement('topRight')).toBe('top-end');
    });
  });
  describe('fromPopperPlacement', function () {
    test('fromPopperPlacement to convert from popper placements to popover placements', function () {
      expect(fromPopperPlacement('right-start')).toBe('rightTop');
      expect(fromPopperPlacement('right')).toBe('right');
      expect(fromPopperPlacement('right-end')).toBe('rightBottom');
      expect(fromPopperPlacement('top-start')).toBe('topLeft');
      expect(fromPopperPlacement('top')).toBe('top');
      expect(fromPopperPlacement('top-end')).toBe('topRight');
      expect(fromPopperPlacement('')).toBe(null);
    });
  });
  describe('parsePopperOffset', function () {
    test('parsePopperOffset should handle valid integer inputs', function () {
      expect(parsePopperOffset({
        top: 10,
        left: 15
      })).toEqual({
        top: 10,
        left: 15
      });
    });
    test('parsePopperOffset should round float inputs', function () {
      expect(parsePopperOffset({
        top: 10.1,
        left: 15.24
      })).toEqual({
        top: 10,
        left: 15
      });
    });
    test('parsePopperOffset should be resilient to null input', function () {
      expect(parsePopperOffset({
        top: null,
        left: null
      })).toEqual({
        top: 0,
        left: 0
      });
    });
  });
  describe('splitPlacement', function () {
    test('splitPlacement should split placements apart', function () {
      expect(splitPlacement('rightTop')).toEqual(['right', 'top']);
      expect(splitPlacement('right')).toEqual(['right']);
      expect(splitPlacement('rightBottom')).toEqual(['right', 'bottom']);
      expect(splitPlacement('topRight')).toEqual(['top', 'right']);
      expect(splitPlacement('top')).toEqual(['top']);
      expect(splitPlacement('topLeft')).toEqual(['top', 'left']);
      expect(splitPlacement('auto')).toEqual(['auto']);
      expect(splitPlacement('')).toEqual([]);
    });
    test('splitPlacement should handle empty input', function () {
      expect(splitPlacement('')).toEqual([]);
    });
  });
  describe('isVerticalPosition', function () {
    test('isVerticalPosition should return true if input string is vertical position', function () {
      expect(isVerticalPosition('top')).toEqual(true);
      expect(isVerticalPosition('bottom')).toEqual(true);
      expect(isVerticalPosition('left')).toEqual(false);
      expect(isVerticalPosition('right')).toEqual(false);
    });
    test('isVerticalPosition should handle empty or null input', function () {
      expect(isVerticalPosition('')).toEqual(false);
      var nullArg = null;
      expect(isVerticalPosition(nullArg)).toEqual(false);
    });
  });
  describe('getPopoverMarginStyles', function () {
    test('getPopoverMarginStyles should return correct margins without arrow', function () {
      var showArrow = false;
      expect(getPopoverMarginStyles(showArrow, 'topLeft')).toEqual({
        marginBottom: '8px'
      });
      expect(getPopoverMarginStyles(showArrow, 'top')).toEqual({
        marginBottom: '8px'
      });
      expect(getPopoverMarginStyles(showArrow, 'right')).toEqual({
        marginLeft: '8px'
      });
      expect(getPopoverMarginStyles(showArrow, 'bottomLeft')).toEqual({
        marginTop: '8px'
      });
    });
    test('getPopoverMarginStyles should return correct margins with arrow', function () {
      var showArrow = true;
      expect(getPopoverMarginStyles(showArrow, 'topLeft')).toEqual({
        marginBottom: '6px'
      });
      expect(getPopoverMarginStyles(showArrow, 'top')).toEqual({
        marginBottom: '6px'
      });
      expect(getPopoverMarginStyles(showArrow, 'right')).toEqual({
        marginLeft: '6px'
      });
      expect(getPopoverMarginStyles(showArrow, 'bottomLeft')).toEqual({
        marginTop: '6px'
      });
    });
  });
  describe('getArrowPositionStyles', function () {
    test('getArrowPositionStyles should return no styles when placement is unknown yet', function () {
      expect(getArrowPositionStyles({
        top: 0,
        left: 15
      }, 'auto')).toBe(null);
    });
    test('getArrowPositionStyles should return correct styles for topLeft', function () {
      expect(getArrowPositionStyles({
        top: 0,
        left: 15
      }, 'topLeft')).toEqual({
        bottom: '-4px',
        left: '15px'
      });
    });
    test('getArrowPositionStyles should return correct styles for leftTop', function () {
      expect(getArrowPositionStyles({
        top: 15,
        left: 0
      }, 'leftTop')).toEqual({
        right: '-4px',
        top: '15px'
      });
    });
  });
  describe('getStartPosition', function () {
    test('getStartPosition should return correct position for topLeft', function () {
      expect(getStartPosition({
        left: 10,
        top: 15
      }, 'topLeft', true)).toEqual('translate3d(10px, 27px, 0)');
    });
  });
  describe('getEndPosition', function () {
    test('getEndPosition should return correct end position', function () {
      expect(getEndPosition({
        left: 10,
        top: 15
      })).toEqual('translate3d(10px, 15px, 0)');
    });
  });
});