"use strict";
var __assign = (this && this.__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;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var RegexParser = require("regex-parser");
var isArray = require("lodash/isArray");
var assign = require("lodash/assign");
var some = require("lodash/some");
var includes = require("lodash/includes");
var values = require("lodash/values");
var keys = require("lodash/keys");
var Input_1 = require("../Input");
var Grid_1 = require("../Grid");
var normalizeToCollection = function (value) { return (isArray(value) ? value : [value]); };
exports.rules = {
    is: function (target, value) {
        return some(normalizeToCollection(target), value);
    },
    'is not': function (target, value) {
        return !some(normalizeToCollection(target), value);
    },
    'key is': {
        getLabel: function (schema) { return schema.keyLabel && schema.keyLabel + " is"; },
        test: function (target, value) {
            return some(normalizeToCollection(target), value);
        }
    },
    'key contains': {
        getLabel: function (schema) { return schema.keyLabel && schema.keyLabel + " contains"; },
        test: function (target, value) {
            var lookupKey = keys(value).pop();
            var lookupValue = values(value).pop();
            return some(normalizeToCollection(target), function (item) { return item && item[lookupKey] && item[lookupKey].includes(lookupValue); });
        }
    },
    'key does not contain': {
        getLabel: function (schema) {
            return schema.keyLabel && schema.keyLabel + " does not contain";
        },
        test: function (target, value) {
            var lookupKey = keys(value).pop();
            var lookupValue = values(value).pop();
            return !some(normalizeToCollection(target), function (item) { return item && item[lookupKey] && item[lookupKey].includes(lookupValue); });
        }
    },
    'key matches RegEx': {
        getLabel: function (schema) { return schema.keyLabel && schema.keyLabel + " matches RegEx"; },
        test: function (target, value) {
            var lookupKey = keys(value).pop();
            var lookupValue = values(value).pop();
            return some(normalizeToCollection(target), function (item) {
                return item &&
                    item[lookupKey] &&
                    item[lookupKey].match(RegexParser(lookupValue));
            });
        }
    },
    'key does not match RegEx': {
        getLabel: function (schema) {
            return schema.keyLabel && schema.keyLabel + " does not match RegEx";
        },
        test: function (target, value) {
            var lookupKey = keys(value).pop();
            var lookupValue = values(value).pop();
            return !some(normalizeToCollection(target), function (item) {
                return item &&
                    item[lookupKey] &&
                    item[lookupKey].match(RegexParser(lookupValue));
            });
        }
    },
    'value is': {
        getLabel: function (schema) { return schema.valueLabel && schema.valueLabel + " is"; },
        test: function (target, value) {
            return some(normalizeToCollection(target), value);
        }
    },
    'value contains': {
        getLabel: function (schema) { return schema.valueLabel && schema.valueLabel + " contains"; },
        test: function (target, value) {
            var lookupKey = keys(value).pop();
            var lookupValue = values(value).pop();
            return some(normalizeToCollection(target), function (item) { return item && item[lookupKey] && item[lookupKey].includes(lookupValue); });
        }
    },
    'value does not contain': {
        getLabel: function (schema) {
            return schema.valueLabel && schema.valueLabel + " does not contain";
        },
        test: function (target, value) {
            var lookupKey = keys(value).pop();
            var lookupValue = values(value).pop();
            return !some(normalizeToCollection(target), function (item) { return item && item[lookupKey] && item[lookupKey].includes(lookupValue); });
        }
    },
    'value matches RegEx': {
        getLabel: function (schema) {
            return schema.valueLabel && schema.valueLabel + " matches RegEx";
        },
        test: function (target, value) {
            var lookupKey = keys(value).pop();
            var lookupValue = values(value).pop();
            return some(normalizeToCollection(target), function (item) {
                return item &&
                    item[lookupKey] &&
                    item[lookupKey].match(RegexParser(lookupValue));
            });
        }
    },
    'value does not match RegEx': {
        getLabel: function (schema) {
            return schema.valueLabel && schema.valueLabel + " does not match RegEx";
        },
        test: function (target, value) {
            var lookupKey = keys(value).pop();
            var lookupValue = values(value).pop();
            return !some(normalizeToCollection(target), function (item) {
                return (item &&
                    item[lookupKey] &&
                    item[lookupKey].match(RegexParser(lookupValue)));
            });
        }
    }
};
exports.validate = function (value) { return true; };
exports.normalize = function (value) { return value; };
var keyOperators = [
    'is',
    'is not',
    'key is',
    'key contains',
    'key does not contain',
    'key matches RegEx',
    'key does not match RegEx'
];
var valueOperators = [
    'is',
    'is not',
    'value is',
    'value contains',
    'value does not contain',
    'value matches RegEx',
    'value does not match RegEx'
];
exports.Edit = function (props) {
    var value = props.value, schema = props.schema, onChange = props.onChange, operator = props.operator;
    return (React.createElement(Grid_1.Flex, { wrap: true },
        includes(keyOperators, operator) && (React.createElement(Input_1.default, { type: 'text', value: value ? value[schema.key] : '', mr: 2, mb: 1, placeholder: schema.keyLabel || 'Key', onChange: function (e) {
                return onChange(assign(value, (_a = {}, _a[schema.key] = e.target.value, _a)));
                var _a;
            } })),
        includes(valueOperators, operator) && (React.createElement(Input_1.default, { type: 'text', value: value ? value[schema.value] : '', placeholder: schema.valueLabel || 'Value', onChange: function (e) {
                return onChange(assign(value, (_a = {}, _a[schema.value] = e.target.value, _a)));
                var _a;
            } }))));
};
exports.Display = function (_a) {
    var data = _a.data, props = __rest(_a, ["data"]);
    return (React.createElement("div", __assign({}, props), JSON.stringify(data)));
};
