"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Color = require("color");
var cloneDeep = require("lodash/cloneDeep");
var find = require("lodash/find");
var forEach = require("lodash/forEach");
var get = require("lodash/get");
var isObject = require("lodash/isObject");
var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
exports.lighten = function (color) {
    return Color(color)
        .fade(0.3)
        .string();
};
exports.darken = function (color) {
    return Color(color)
        .darken(0.2)
        .string();
};
exports.fade = function (color) {
    return Color(color)
        .fade(0.95)
        .string();
};
exports.blacken = function (color) {
    return Color(color)
        .darken(0.6)
        .string();
};
exports.normal = function (props) {
    return get(props.theme, 'weights.0');
};
exports.bold = function (props) {
    return get(props.theme, 'weights.1');
};
exports.px = function (n) { return (typeof n === 'number' ? n + 'px' : n); };
exports.randomString = function (length) {
    if (length === void 0) { length = 16; }
    var text = '';
    var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    for (var i = 0; i < length; i++) {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    return text;
};
exports.stopPropagation = function (e) {
    e.stopPropagation();
};
exports.stopEvent = function (e) {
    e.preventDefault();
    e.stopPropagation();
};
exports.getColorFromTheme = function (theme) { return function (colorString) {
    return get(theme, "colors." + colorString);
}; };
exports.getColoringType = function (props) {
    var type = find(Object.keys(props), function (b) {
        return !!props[b] && !!find(Object.keys(props.theme.colors), function (k) { return k === b; });
    });
    return type;
};
exports.getColor = function (props, key, shade) {
    if (get(props, key)) {
        return get(props, key);
    }
    var type = exports.getColoringType(props);
    if (type) {
        var color = exports.getColorFromTheme(props.theme)(type);
        if (isObject(color)) {
            return color[shade];
        }
        else {
            return color;
        }
    }
};
exports.monospace = function (props) {
    return props.monospace ? { fontFamily: props.theme.monospace } : null;
};
function withProps() {
    return function (fn) { return fn; };
}
exports.withProps = withProps;
exports.regexEscape = function (str) {
    return str.replace(matchOperatorsRe, '\\$&');
};
exports.stripSchemaFormats = function (schema, whitelist) {
    if (whitelist === void 0) { whitelist = []; }
    var newSchema = cloneDeep(schema);
    var _strip = function (schema) {
        if (schema.format && whitelist.indexOf(schema.format) === -1) {
            delete schema.format;
        }
        if (schema.properties) {
            forEach(schema.properties, function (subSchema) {
                _strip(subSchema);
            });
        }
        if (schema.items) {
            _strip(schema.items);
        }
    };
    _strip(newSchema);
    return newSchema;
};
exports.disallowAdditionalProperties = function (schema) {
    var newSchema = cloneDeep(schema);
    var disallow = function (schema) {
        if (schema.additionalProperties) {
            schema.additionalProperties = false;
        }
        if (schema.properties) {
            forEach(schema.properties, function (subSchema) {
                disallow(subSchema);
            });
        }
    };
    disallow(newSchema);
    return newSchema;
};
exports.getAngleBetweenPoints = function (p1, p2) {
    return (Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180) / Math.PI;
};
exports.constrainNumber = function (value, lower, upper) {
    if (lower === void 0) { lower = 0; }
    if (upper === void 0) { upper = 100; }
    return Math.min(Math.max(value, lower), upper);
};
