var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
/**
 * @module utils/functions
 */
import _ from 'lodash';
import React from 'react';
import ReactDOM from 'react-dom';
import { hashHistory } from 'react-router';
import { isConfigInitialized, getConfig, translate } from '../../utils';
/**
 * Returns a hash history
 */
export function getSilentRouterHistory() {
    if (!hashHistory['_pushStatePatched']) {
        hashHistory['_pushStatePatched'] = true;
        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 hashHistory;
}
/**
 * Changes document title
 * @TODO Replace with React Helmet
 * @param title
 */
export function setDocumentTitle(title) {
    var suffix = isConfigInitialized()
        ? getConfig().headerAccountBlock || getConfig().brandDisplayName
        : '';
    document.title = title + ' - ' + suffix;
}
/**
 * @param val
 * @return {boolean}
 */
export function isReactComponent(val) {
    return val instanceof React.Component.constructor;
}
/**
 * @param val
 * @return {*|boolean}
 */
export function isReactElement(val) {
    return React.isValidElement(val);
}
/**
 * @param {React.Element} element
 * @param {constructor} type
 * @return {boolean}
 * @example isElementHasType(tabPanelChild, NavDropdown)
 */
export 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[]}
 */
export function cleanEmptyChildren(children) {
    return React.Children.toArray(children).filter(Boolean);
}
/**
 * @param Component
 * @param container
 * @param params
 */
export 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.createElement(Component, params);
    }
    if (!React.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.render(element, container);
}
/**
 * @param value
 * @return {boolean}
 */
export function isSafeStringValue(value) {
    if (value.length > 0) {
        return (value.indexOf('"') === -1 &&
            value.indexOf('/') === -1 &&
            value.indexOf('<') === -1 &&
            value.indexOf('>') === -1 &&
            value.indexOf('&') === -1);
    }
    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: ""
export var OS = {
    is: {
        mac: /mac\sos\sx/i.test(navigator.userAgent),
        win: /windows/i.test(navigator.userAgent),
        others: null
    },
    name: 'others'
};
OS.is.others = !Object.keys(OS.is).some(function (o) {
    if (OS.is[o]) {
        OS.name = o;
        return true;
    }
});
/**
 * @param str
 * @return {*}
 */
export function fillBrandName(str) {
    return translate(str, { brandName: getConfig().shortBrandName });
}
/**
 * @param prevState
 * @param newState
 * @return {*}
 */
export function extendFormState(prevState, newState) {
    return _.merge(prevState, newState);
}
/**
 * @param prevState
 * @param removedIds
 * @return {*|{}}
 */
export function cleanFormState(prevState, removedIds) {
    return _.omit(prevState, removedIds);
}
export function wait(mileSeconds) {
    return new Promise(function (resolve) {
        setTimeout(resolve, mileSeconds);
    });
}
export function deprecation(Component, message) {
    var name = Component.displayName || Component.name || 'Component';
    var Deprecated = /** @class */ (function (_super) {
        __extends(Deprecated, _super);
        function Deprecated() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        Deprecated.prototype.componentDidMount = function () {
            // eslint-disable-next-line no-console
            console.warn("Component " + name + " is deprecated.", message || '');
        };
        Deprecated.prototype.render = function () {
            return React.createElement(Component, __assign({}, this.props));
        };
        Deprecated.displayName = "Deprecated(" + name + ")";
        return Deprecated;
    }(React.Component));
    return Deprecated;
}
var KEY_CODE_ENTER = 13;
var KEY_CODE_SPACE = 32;
var defaultKeyCodesFilter = [KEY_CODE_ENTER, KEY_CODE_SPACE];
export var filterKeyCodes = function (callback, keyCodes) {
    if (keyCodes === void 0) { keyCodes = defaultKeyCodesFilter; }
    return function (_a) {
        var keyCode = _a.keyCode;
        if (_.includes(keyCodes, keyCode)) {
            callback();
        }
    };
};
export var scrollElementIntoView = function (element) {
    if (element && typeof element.scrollIntoView === 'function') {
        element.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
    }
};
export var stripTags = function (string) { return string.replace(/(<([^>]+)>)/gi, ''); };
//# sourceMappingURL=functions.js.map