"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getSharedProps = getSharedProps;
exports.scrollItemIntoView = scrollItemIntoView;

var _smoothscrollPolyfill = _interopRequireDefault(require("smoothscroll-polyfill"));

var _constants = require("./constants");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/*
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 import/prefer-default-export */
if (typeof document !== 'undefined') {
  _smoothscrollPolyfill.default.polyfill();
}
/**
 * Given a props object and a mapper dictionary of prop keys, we will prepend
 * all of the existing prop keys inside mapper with $ to present styletron
 * from passing through those props to the underlying React component.
 */


function getSharedProps(props) {
  var mapper = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _constants.SHARED_PROPS_MAPPER;
  return Object.keys(props).reduce(function (newProps, propName) {
    var newName = mapper[propName] ? "$".concat(propName) : propName;
    newProps[newName] = props[propName];
    return newProps;
  }, {});
}
/**
 * Helps scroll a list item into view when cycling through list via
 * keybindings and highlighted item is not in view.
 */


function scrollItemIntoView(_ref) {
  var node = _ref.node,
      parentNode = _ref.parentNode,
      isFirst = _ref.isFirst,
      isLast = _ref.isLast;
  var nodeDOM = node.current;
  var parentNodeDOM = parentNode.current;
  var nodeRect = nodeDOM.getBoundingClientRect();
  var parentNodeRect = parentNodeDOM.getBoundingClientRect(); // while scrolling down, if element is below view

  if (nodeRect.bottom > parentNodeRect.bottom) {
    if (isLast) {
      parentNodeDOM.scrollTop = parentNodeDOM.scrollHeight - parentNodeRect.height;
    } else {
      nodeDOM.scrollIntoView(false);
    } // while scrolling up, if element is above view

  } else if (nodeRect.top < parentNodeRect.top) {
    if (isFirst) {
      parentNodeDOM.scrollTop = 0;
    } else {
      nodeDOM.scrollIntoView();
    }
  }
}