"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.polyfillMediaQueries = exports.classNameFor = exports.unprefixKeys = exports.prefixKeys = exports.omitKeys = exports.propsFor = void 0;
var classnames_1 = require("classnames");
/**
 * Extracts properties from props that klass's propTypes field specifies, as an object.
 * Useful for inheriting properties from other components; see ModalButton for examples.
 */
function propsFor(klass, props) {
    return Object.keys(props).reduce(function (prev, propKey) {
        if (Object.keys(klass.propTypes).indexOf(propKey) >= 0) {
            prev[propKey] = props[propKey];
        }
        return prev;
    }, {});
}
exports.propsFor = propsFor;
/**
 * Returns a copy of obj (Object) with keys toOmit ([string]) and their associated values removed.
 */
function omitKeys(obj) {
    var toOmit = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        toOmit[_i - 1] = arguments[_i];
    }
    return Object.keys(obj).reduce(function (prev, key) {
        if (toOmit.indexOf(key) < 0) {
            prev[key] = obj[key];
        }
        return prev;
    }, {});
}
exports.omitKeys = omitKeys;
/**
 * Returns a copy of obj (Object) with prefix (String) prepended to each key. Capitalizes the first
 * letter of the old key if capitalizeFirst (bool) is true.
 */
function prefixKeys(obj, prefix, capitalizeFirst) {
    if (capitalizeFirst === void 0) { capitalizeFirst = true; }
    return Object.keys(obj).reduce(function (prev, key) {
        var suffix = capitalizeFirst ? key.charAt(0).toUpperCase() + key.slice(1) : key;
        prev[prefix + suffix] = obj[key];
        return prev;
    }, {});
}
exports.prefixKeys = prefixKeys;
/**
 * Returns a copy of obj (Object) with prefix (String) removed from the start of each key, if it is
 * present. Lowercases the first letter of the old key if lowercaseFirst (bool) is true AND the
 * old key begins with the prefix.
 */
function unprefixKeys(obj, prefixToRemove, lowercaseFirst) {
    if (lowercaseFirst === void 0) { lowercaseFirst = true; }
    return Object.keys(obj).reduce(function (prev, key) {
        if (key.indexOf(prefixToRemove) === 0) {
            var sansPrefix = key.slice(prefixToRemove.length);
            var newKey = lowercaseFirst
                ? sansPrefix.charAt(0).toLowerCase() + sansPrefix.slice(1)
                : sansPrefix;
            prev[newKey] = obj[key];
        }
        return prev;
    }, {});
}
exports.unprefixKeys = unprefixKeys;
/**
 * Combine classname strings with long kabobs (--), and prepend each
 * prefix to the result.
 * For example:
 * ```
 * // returns "Component--a--b MyCustomClassName--a--b"
 * classNamesFor(["Component", "MyCustomClassName"], "a", "b")
 * ```
 * If no prefixes are provided (i.e. prefixes === null or prefixes only contains null/empty values)
 * it just doesn't append a prefix—i.e. it kabobs the rest of the arguments together.
 * @param {string | Array<string>} prefixes Prefixes to prepend to resulting classname
 * @param {string | Array<string>} classSegments Class names to kabob together
 */
function classNameFor(prefixes, classSegments) {
    var prefixList = (prefixes instanceof Array ? prefixes : [prefixes]).filter(function (p) { return !!p; });
    var classSegmentList;
    if (!classSegments) {
        classSegmentList = [];
    }
    else {
        classSegmentList = classSegments instanceof Array ? classSegments : [classSegments];
    }
    if (prefixList.length === 0) {
        return classSegmentList.join("--");
    }
    return classnames_1.default(prefixList.map(function (p) { return [p].concat(classSegmentList).join("--"); }));
}
exports.classNameFor = classNameFor;
// This is defined as a separate function to avoid binding multiple event
// listeners. We do not want this code to run more than once.
function polyfillTouchAndHover() {
    var classList = document.querySelector("body").classList;
    if ("ontouchstart" in window) {
        classList.add("mq-touch-enabled");
    }
    else {
        classList.add("mq-hover");
    }
}
/**
 * Detect browser capabilities and apply faux media queries
 *
 * This will allow us to write CSS rules like:
 *
 *     .mq-hover .conditional-styles {
 *       // only applied when the browser supports hover
 *     }
 */
function polyfillMediaQueries() {
    window.addEventListener("load", polyfillTouchAndHover);
}
exports.polyfillMediaQueries = polyfillMediaQueries;
