"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.getSilentRouterHistory = getSilentRouterHistory;
exports.setDocumentTitle = setDocumentTitle;
exports.isReactComponent = isReactComponent;
exports.isReactElement = isReactElement;
exports.isElementHasType = isElementHasType;
exports.cleanEmptyChildren = cleanEmptyChildren;
exports.mountComponent = mountComponent;
exports.isAutomationTestActive = isAutomationTestActive;
exports.isSafeStringValue = isSafeStringValue;
exports.fillBrandName = fillBrandName;
exports.extendFormState = extendFormState;
exports.cleanFormState = cleanFormState;
exports.wait = wait;
exports.deprecation = deprecation;
exports.stripTags = exports.scrollEelementIntoView = exports.filterKeyCodes = exports.OS = void 0;

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _lodash = _interopRequireDefault(require("lodash"));

var _react = _interopRequireDefault(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _reactRouter = require("react-router");

var _utils = require("../../utils");

var _cookie = require("./cookie");

/**
 * @module utils/functions
 */

/**
 * Returns a hash history
 */
function getSilentRouterHistory() {
  if (!_reactRouter.hashHistory._pushStatePatched) {
    _reactRouter.hashHistory._pushStatePatched = true;

    _reactRouter.hashHistory.pushState = function pushState(state, url) {
      if (typeof console !== 'undefined') {
        // eslint-disable-next-line
        console.error("Warning: pushState deprecated method for hashHistory");
      }

      return this.push(url);
    };
  }

  return _reactRouter.hashHistory;
}
/**
 * Changes document title
 * @TODO Replace with React Helmet
 * @param title
 */


function setDocumentTitle(title) {
  var suffix = (0, _utils.getConfig)().headerAccountBlock || (0, _utils.getConfig)().brandDisplayName;
  document.title = title + ' - ' + suffix;
}
/**
 * @param val
 * @return {boolean}
 */


function isReactComponent(val) {
  return val instanceof _react.default.Component.constructor;
}
/**
 * @param val
 * @return {*|boolean}
 */


function isReactElement(val) {
  return _react.default.isValidElement(val);
}
/**
 * @param {React.Element} element
 * @param {constructor} type
 * @return {boolean}
 * @example isElementHasType(tabPanelChild, NavDropdown)
 */


function isElementHasType(element, type) {
  return element.type === type || element.type.prototype instanceof type;
}
/**
 * Filters empty [null, undefined] children of React component
 * @param {ReactNode} children
 * @returns {React.ReactChild[]}
 */


function cleanEmptyChildren(children) {
  return _react.default.Children.toArray(children).filter(Boolean);
}
/**
 * @param Component
 * @param container
 * @param params
 */


function mountComponent(Component, container, params) {
  var element; //FIXME React Router needs to be created from scratch otherwise a previous state is picked up
  //FIXME Replace all existing routers with router factory functions

  if (typeof Component === 'function' && Component.name === 'createRouter') {
    Component = Component();
  }

  if (isReactElement(Component)) {
    element = Component;
  } else if (isReactComponent(Component)) {
    element = _react.default.createElement(Component, params);
  }

  if (!_react.default.isValidElement(element)) {
    // eslint-disable-next-line
    console.error('Cannot render invalid React element', Component);
    throw new Error('Cannot render invalid React element');
  } // eslint-disable-next-line react/no-render-return-value


  return _reactDom.default.render(element, container);
}
/**
 * @return {boolean}
 */


function isAutomationTestActive() {
  return !!(0, _cookie.readCookie)('automationFlow');
}
/**
 * @param value
 * @return {boolean}
 */


function isSafeStringValue(value) {
  if (value.length > 0) {
    return value.indexOf('"') === -1 && value.indexOf('/') === -1 && value.indexOf('<') === -1 && value.indexOf('>') === -1 && value.indexOf('&') === -1;
  } else {
    return true;
  }
} //PC userAgent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36"
//MAC userAgent: ""


var OS = {
  is: {
    mac: /mac\sos\sx/i.test(navigator.userAgent),
    win: /windows/i.test(navigator.userAgent)
  },
  name: 'others'
};
exports.OS = OS;
OS.is.others = !Object.keys(OS.is).some(function (o) {
  if (OS.is[o]) {
    OS.name = o;
    return true;
  }
});
/**
 * @param str
 * @return {*}
 */

function fillBrandName(str) {
  return (0, _utils.translate)(str, {
    brandName: (0, _utils.getConfig)().shortBrandName
  });
}
/**
 * @param prevState
 * @param newState
 * @return {*}
 */


function extendFormState(prevState, newState) {
  return _lodash.default.merge(prevState, newState);
}
/**
 * @param prevState
 * @param removedIds
 * @return {*|{}}
 */


function cleanFormState(prevState, removedIds) {
  return _lodash.default.omit(prevState, removedIds);
}

function wait(mileSeconds) {
  return new Promise(function (resolve) {
    setTimeout(resolve, mileSeconds);
  });
}

function deprecation(Component, message) {
  var name = Component.displayName || Component.name || 'Component';

  var Deprecated =
  /*#__PURE__*/
  function (_React$Component) {
    (0, _inheritsLoose2.default)(Deprecated, _React$Component);

    function Deprecated() {
      return _React$Component.apply(this, arguments) || this;
    }

    var _proto = Deprecated.prototype;

    _proto.componentDidMount = function componentDidMount() {
      // eslint-disable-next-line no-console
      console.warn("Component " + name + " is deprecated.", message || '');
    };

    _proto.render = function render() {
      return _react.default.createElement(Component, this.props);
    };

    return Deprecated;
  }(_react.default.Component);

  (0, _defineProperty2.default)(Deprecated, "displayName", "Deprecated(" + name + ")");
  return Deprecated;
}

var KEY_CODE_ENTER = 13;
var KEY_CODE_SPACE = 32;
var defaultKeyCodesFilter = [KEY_CODE_ENTER, KEY_CODE_SPACE];

var filterKeyCodes = function filterKeyCodes(callback, keyCodes) {
  if (keyCodes === void 0) {
    keyCodes = defaultKeyCodesFilter;
  }

  return function (_ref) {
    var keyCode = _ref.keyCode;

    if (_lodash.default.includes(keyCodes, keyCode)) {
      callback();
    }
  };
};

exports.filterKeyCodes = filterKeyCodes;

var scrollEelementIntoView = function scrollEelementIntoView(elementRef) {
  var $element = _reactDom.default.findDOMNode(elementRef);

  if ($element && typeof $element.scrollIntoView === 'function') {
    $element.scrollIntoView({
      behavior: 'smooth'
    });
  }
};

exports.scrollEelementIntoView = scrollEelementIntoView;

var stripTags = function stripTags(string) {
  return string.replace(/(<([^>]+)>)/gi, '');
};

exports.stripTags = stripTags;