"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const validate = function (sc, pe, pr, old, user, assign, find, includes) {
    var base = ['_id', '_rev', '_revisions', '_deleted'];
    var keys = Object.keys;
    var doc = assign({}, old || {}, pr || {});
    var err = find(keys(sc).map(function (k) {
        return eval('(' + sc[k] + ')')(k, doc[k]);
    }), (function (el) {
        return el !== '';
    }));
    if (err) {
        throw ({ forbidden: err });
    }
    var ex = keys(pr).filter(function (k) {
        return !includes(base, k);
    }).filter(function (k) {
        return !~keys(sc).indexOf(k);
    });
    if (ex.length) {
        throw ({ forbidden: 'Keys ' + ex.join(', ') + ' aren\'t allowed' });
    }
    if (!pe)
        return;
    if (!old) {
        var perm = pe.create;
        for (var i = 0; i < user.roles.length; i++) {
            var role = user.roles[i];
            if (perm[role])
                return;
        }
        throw ({ forbidden: 'User is not allowed to create documents' });
    }
    var perm = pe.read;
    var ok = false;
    for (var i = 0; i < user.roles.length; i++) {
        var role = user.roles[i];
        if (perm[role])
            ok = true;
    }
    if (!ok)
        throw ({ forbidden: 'User is not allowed to update documents' });
    for (var i = 0; i < keys(pr).length; i++) {
        var key = keys(pr)[i];
        var val = pr[key];
        if (val !== old[key] && !~pe.upd_fd.indexOf(key) && !includes(base, key))
            throw ({ forbidden: 'Not allowed to update ' + key });
    }
};
function createValidator(schema, perms) {
    return `function(d,o,u) {
  function _f(t, a) {
    if (null == t) throw new TypeError('"t" is null or not defined');
    var b = Object(t),
        c = b.length >>> 0;
    if ("function" != typeof a) throw new TypeError("predicate must be a function");
    for (var d = arguments[1], e = 0; e < c;) {
      var f = b[e];
      if (a.call(d, f, e, b)) return f;
      e++
    }
  }
  function _i (r,n,e){if(null==r)throw new TypeError('"arr" is null or not defined');var t=Object(r),a=t.length>>>0;if(0===a)return!1;for(var i=0|e,u=Math.max(i>=0?i:a-Math.abs(i),0);u<a;){if(t[u]===n)return!0;u++}return!1}
  function _a (a, b) {
    "use strict";
    if (null == a) throw new TypeError("Cannot convert undefined or null to object");
    for (var c = Object(a), d = 1; d < arguments.length; d++) {
      var e = arguments[d];
      if (null != e)
        for (var f in e) Object.prototype.hasOwnProperty.call(e, f) && (c[f] = e[f])
    }
    return c;
  };
  (${validate.toString()})(${JSON.stringify(schema, (key, val) => {
        return typeof val === 'function' ? val.toString() : val;
    })}, ${JSON.stringify(perms)}, d, o, u, _a, _f, _i);
}`.replace(/(\s\s+|\n)/g, ' ');
}
exports.createValidator = createValidator;
//# sourceMappingURL=index.js.map