"use strict";
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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @module utils/functions
 */
var lodash_1 = __importDefault(require("lodash"));
var react_1 = __importDefault(require("react"));
var react_dom_1 = __importDefault(require("react-dom"));
var react_router_1 = require("react-router");
var utils_1 = require("../../utils");
/**
 * Returns a hash history
 */
function getSilentRouterHistory() {
    if (!react_router_1.hashHistory['_pushStatePatched']) {
        react_router_1.hashHistory['_pushStatePatched'] = true;
        react_router_1.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 react_router_1.hashHistory;
}
exports.getSilentRouterHistory = getSilentRouterHistory;
/**
 * Changes document title
 * @TODO Replace with React Helmet
 * @param title
 */
function setDocumentTitle(title) {
    var suffix = utils_1.isConfigInitialized()
        ? utils_1.getConfig().headerAccountBlock || utils_1.getConfig().brandDisplayName
        : '';
    document.title = title + ' - ' + suffix;
}
exports.setDocumentTitle = setDocumentTitle;
/**
 * @param val
 * @return {boolean}
 */
function isReactComponent(val) {
    return val instanceof react_1.default.Component.constructor;
}
exports.isReactComponent = isReactComponent;
/**
 * @param val
 * @return {*|boolean}
 */
function isReactElement(val) {
    return react_1.default.isValidElement(val);
}
exports.isReactElement = isReactElement;
/**
 * @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;
}
exports.isElementHasType = isElementHasType;
/**
 * Filters empty [null, undefined] children of React component
 * @param {ReactNode} children
 * @returns {React.ReactChild[]}
 */
function cleanEmptyChildren(children) {
    return react_1.default.Children.toArray(children).filter(Boolean);
}
exports.cleanEmptyChildren = cleanEmptyChildren;
/**
 * @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_1.default.createElement(Component, params);
    }
    if (!react_1.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 react_dom_1.default.render(element, container);
}
exports.mountComponent = mountComponent;
/**
 * @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);
    }
    return true;
}
exports.isSafeStringValue = isSafeStringValue;
//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: ""
exports.OS = {
    is: {
        mac: /mac\sos\sx/i.test(navigator.userAgent),
        win: /windows/i.test(navigator.userAgent),
        others: null
    },
    name: 'others'
};
exports.OS.is.others = !Object.keys(exports.OS.is).some(function (o) {
    if (exports.OS.is[o]) {
        exports.OS.name = o;
        return true;
    }
});
/**
 * @param str
 * @return {*}
 */
function fillBrandName(str) {
    return utils_1.translate(str, { brandName: utils_1.getConfig().shortBrandName });
}
exports.fillBrandName = fillBrandName;
/**
 * @param prevState
 * @param newState
 * @return {*}
 */
function extendFormState(prevState, newState) {
    return lodash_1.default.merge(prevState, newState);
}
exports.extendFormState = extendFormState;
/**
 * @param prevState
 * @param removedIds
 * @return {*|{}}
 */
function cleanFormState(prevState, removedIds) {
    return lodash_1.default.omit(prevState, removedIds);
}
exports.cleanFormState = cleanFormState;
function wait(mileSeconds) {
    return new Promise(function (resolve) {
        setTimeout(resolve, mileSeconds);
    });
}
exports.wait = wait;
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_1.default.createElement(Component, __assign({}, this.props));
        };
        Deprecated.displayName = "Deprecated(" + name + ")";
        return Deprecated;
    }(react_1.default.Component));
    return Deprecated;
}
exports.deprecation = deprecation;
var KEY_CODE_ENTER = 13;
var KEY_CODE_SPACE = 32;
var defaultKeyCodesFilter = [KEY_CODE_ENTER, KEY_CODE_SPACE];
exports.filterKeyCodes = function (callback, keyCodes) {
    if (keyCodes === void 0) { keyCodes = defaultKeyCodesFilter; }
    return function (_a) {
        var keyCode = _a.keyCode;
        if (lodash_1.default.includes(keyCodes, keyCode)) {
            callback();
        }
    };
};
exports.scrollEelementIntoView = function (elementRef) {
    var $element = react_dom_1.default.findDOMNode(elementRef);
    if ($element && typeof $element.scrollIntoView === 'function') {
        $element.scrollIntoView({ behavior: 'smooth' });
    }
};
exports.stripTags = function (string) { return string.replace(/(<([^>]+)>)/gi, ''); };
//# sourceMappingURL=functions.js.map