"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 assign = require("lodash/assign");
var React = require("react");
var utils = require("../../utils");
var Input_1 = require("../Input");
var Textarea_1 = require("../Textarea");
exports.operators = {
    is: {
        getLabel: function (_s) { return 'is'; },
    },
    contains: {
        getLabel: function (_s) { return 'contains'; },
    },
    not_contains: {
        getLabel: function (_s) { return 'does not contain'; },
    },
    matches_re: {
        getLabel: function (_s) { return 'matches RegEx'; },
    },
    not_matches_re: {
        getLabel: function (_s) { return 'does not match RegEx'; },
    },
};
exports.decodeFilter = function (filter) {
    var operator = filter.title;
    var value;
    var field;
    if (filter.properties) {
        var keys = Object.keys(filter.properties);
        if (!keys.length) {
            return null;
        }
        field = keys[0];
        if (operator === 'is') {
            value = filter.properties[field].const;
        }
        else if (operator === 'contains') {
            value = filter.properties[field].description;
        }
        else if (operator === 'matches_re') {
            value = filter.properties[field].pattern;
        }
        else {
            return null;
        }
    }
    else if (filter.anyOf) {
        if (filter.anyOf.length === 0 || !filter.anyOf[0].properties) {
            return null;
        }
        var keys = Object.keys(filter.anyOf[0].properties);
        if (!keys.length) {
            return null;
        }
        field = keys[0];
        if (operator === 'not_contains') {
            value = filter.anyOf[0].properties[field].description;
        }
        else if (operator === 'not_matches_re') {
            value = filter.anyOf[0].properties[field].not.pattern;
        }
        else {
            return null;
        }
    }
    else {
        return null;
    }
    return {
        field: field,
        operator: operator,
        value: value,
    };
};
exports.createFilter = function (field, operator, value, schema) {
    var title = schema.title;
    var base = {
        $id: utils.randomString(),
        title: operator,
        description: (title || field) + " " + exports.operators[operator].getLabel(schema) + " " + value,
        type: 'object',
    };
    if (operator === 'is') {
        return assign(base, {
            properties: (_a = {},
                _a[field] = {
                    title: title,
                    const: value,
                },
                _a),
            required: [field],
        });
    }
    if (operator === 'contains') {
        return assign(base, {
            properties: (_b = {},
                _b[field] = {
                    type: 'string',
                    description: value,
                    pattern: utils.regexEscape(value),
                },
                _b),
            required: [field],
        });
    }
    if (operator === 'not_contains') {
        return assign(base, {
            anyOf: [
                {
                    properties: (_c = {},
                        _c[field] = {
                            type: 'string',
                            description: value,
                            not: {
                                pattern: utils.regexEscape(value),
                            },
                        },
                        _c),
                },
                {
                    properties: (_d = {},
                        _d[field] = {
                            type: 'null',
                        },
                        _d),
                },
            ],
        });
    }
    if (operator === 'matches_re') {
        return assign(base, {
            properties: (_e = {},
                _e[field] = {
                    type: 'string',
                    pattern: value,
                },
                _e),
            required: [field],
        });
    }
    if (operator === 'not_matches_re') {
        return assign(base, {
            anyOf: [
                {
                    properties: (_f = {},
                        _f[field] = {
                            type: 'string',
                            not: {
                                pattern: value,
                            },
                        },
                        _f),
                },
                {
                    properties: (_g = {},
                        _g[field] = {
                            type: 'null',
                        },
                        _g),
                },
            ],
        });
    }
    return base;
    var _a, _b, _c, _d, _e, _f, _g;
};
exports.Edit = function (_a) {
    var onUpdate = _a.onUpdate, slim = _a.slim, props = __rest(_a, ["onUpdate", "slim"]);
    if (slim) {
        return (React.createElement(Input_1.default, __assign({ onChange: function (e) {
                return onUpdate(e.currentTarget.value);
            } }, props)));
    }
    return (React.createElement(Textarea_1.default, __assign({ onChange: function (e) {
            return onUpdate(e.currentTarget.value);
        } }, props)));
};
