'use strict';
var formats = require('ajv/lib/compile/formats')();
var equal = require('ajv/lib/compile/equal');
var validate = (function() {
  var refVal = [];
  var refVal1 = {
    "description": "The represenation of a path within an OADA server",
    "type": "string",
    "format": "uri-reference"
  };
  refVal[1] = refVal1;
  var refVal2 = (function() {
    var refVal = [];
    var refVal1 = {
      "description": "_rev is the revision for a resource in the OADA API.",
      "type": "integer"
    };
    refVal[1] = refVal1;
    return function validate(data, dataPath, parentData, parentDataProperty, rootData) {
      'use strict';
      var vErrors = null;
      var errors = 0;
      if (rootData === undefined) rootData = data;
      if ((data && typeof data === "object" && !Array.isArray(data))) {
        if (true) {
          var errs__0 = errors;
          var valid1 = true;
          if (data._id === undefined) {
            valid1 = false;
            validate.errors = [{
              keyword: 'required',
              dataPath: (dataPath || '') + "",
              schemaPath: '#/required',
              params: {
                missingProperty: '_id'
              },
              message: 'should have required property \'_id\''
            }];
            return false;
          } else {
            var errs_1 = errors;
            if (typeof data._id !== "string") {
              validate.errors = [{
                keyword: 'type',
                dataPath: (dataPath || '') + '._id',
                schemaPath: '#/properties/_id/type',
                params: {
                  type: 'string'
                },
                message: 'should be string'
              }];
              return false;
            }
            var valid1 = errors === errs_1;
          }
          if (valid1) {
            var data1 = data._rev;
            if (data1 === undefined) {
              valid1 = false;
              validate.errors = [{
                keyword: 'required',
                dataPath: (dataPath || '') + "",
                schemaPath: '#/required',
                params: {
                  missingProperty: '_rev'
                },
                message: 'should have required property \'_rev\''
              }];
              return false;
            } else {
              var errs_1 = errors;
              var errs_2 = errors;
              if ((typeof data1 !== "number" || (data1 % 1) || data1 !== data1)) {
                validate.errors = [{
                  keyword: 'type',
                  dataPath: (dataPath || '') + '._rev',
                  schemaPath: '../../oada.schema.json#/definitions/_rev/type',
                  params: {
                    type: 'integer'
                  },
                  message: 'should be integer'
                }];
                return false;
              }
              var valid2 = errors === errs_2;
              if (valid2) {}
              if (errors === errs_1) {}
              var valid1 = errors === errs_1;
            }
            if (valid1) {}
          }
          if (errs__0 == errors) {}
        }
      } else {
        validate.errors = [{
          keyword: 'type',
          dataPath: (dataPath || '') + "",
          schemaPath: '#/type',
          params: {
            type: 'object'
          },
          message: 'should be object'
        }];
        return false;
      }
      if (errors === 0) {}
      validate.errors = vErrors;
      return errors === 0;
    };
  })();
  refVal2.schema = {
    "type": "object",
    "required": ["_rev", "_id"],
    "additionalProperties": true,
    "properties": {
      "_id": {
        "type": "string"
      },
      "_rev": {
        "$ref": "../../oada.schema.json#/definitions/_rev"
      }
    }
  };
  refVal2.errors = null;
  refVal[2] = refVal2;
  return function validate(data, dataPath, parentData, parentDataProperty, rootData) {
    'use strict'; /*# sourceURL=https://formats.openag.io/trellis/rules/configured.schema.json */
    var vErrors = null;
    var errors = 0;
    if (rootData === undefined) rootData = data;
    if ((data && typeof data === "object" && !Array.isArray(data))) {
      if (true) {
        var errs__0 = errors;
        var valid1 = true;
        if (data.enabled === undefined) {
          valid1 = true;
        } else {
          var errs_1 = errors;
          if (typeof data.enabled !== "boolean") {
            validate.errors = [{
              keyword: 'type',
              dataPath: (dataPath || '') + '.enabled',
              schemaPath: '#/properties/enabled/type',
              params: {
                type: 'boolean'
              },
              message: 'should be boolean'
            }];
            return false;
          }
          var valid1 = errors === errs_1;
        }
        if (valid1) {
          var data1 = data.services;
          if (data1 === undefined) {
            valid1 = true;
          } else {
            var errs_1 = errors;
            if (Array.isArray(data1)) {
              var errs__1 = errors;
              var valid1;
              for (var i1 = 0; i1 < data1.length; i1++) {
                var errs_2 = errors;
                if (typeof data1[i1] !== "string") {
                  validate.errors = [{
                    keyword: 'type',
                    dataPath: (dataPath || '') + '.services[' + i1 + ']',
                    schemaPath: '#/properties/services/items/type',
                    params: {
                      type: 'string'
                    },
                    message: 'should be string'
                  }];
                  return false;
                }
                var valid2 = errors === errs_2;
                if (!valid2) break;
              }
              if (errs__1 == errors) {
                var i = data1.length,
                  valid1 = true,
                  j;
                if (i > 1) {
                  var itemIndices = {},
                    item;
                  for (; i--;) {
                    var item = data1[i];
                    if (typeof item !== "string") continue;
                    if (typeof itemIndices[item] == 'number') {
                      valid1 = false;
                      j = itemIndices[item];
                      break;
                    }
                    itemIndices[item] = i;
                  }
                }
                if (!valid1) {
                  validate.errors = [{
                    keyword: 'uniqueItems',
                    dataPath: (dataPath || '') + '.services',
                    schemaPath: '#/properties/services/uniqueItems',
                    params: {
                      i: i,
                      j: j
                    },
                    message: 'should NOT have duplicate items (items ## ' + j + ' and ' + i + ' are identical)'
                  }];
                  return false;
                } else {}
              }
            } else {
              validate.errors = [{
                keyword: 'type',
                dataPath: (dataPath || '') + '.services',
                schemaPath: '#/properties/services/type',
                params: {
                  type: 'array'
                },
                message: 'should be array'
              }];
              return false;
            }
            if (errors === errs_1) {}
            var valid1 = errors === errs_1;
          }
          if (valid1) {
            if (data.type === undefined) {
              valid1 = false;
              validate.errors = [{
                keyword: 'required',
                dataPath: (dataPath || '') + "",
                schemaPath: '#/required',
                params: {
                  missingProperty: 'type'
                },
                message: 'should have required property \'type\''
              }];
              return false;
            } else {
              var errs_1 = errors;
              if (typeof data.type !== "string") {
                validate.errors = [{
                  keyword: 'type',
                  dataPath: (dataPath || '') + '.type',
                  schemaPath: '#/properties/type/type',
                  params: {
                    type: 'string'
                  },
                  message: 'should be string'
                }];
                return false;
              }
              var valid1 = errors === errs_1;
            }
            if (valid1) {
              var data1 = data.on;
              if (data1 === undefined) {
                valid1 = true;
              } else {
                var errs_1 = errors;
                var errs__1 = errors,
                  prevValid1 = false,
                  valid1 = false,
                  passingSchemas1 = null;
                var errs_2 = errors;
                var schema2 = validate.schema.properties.on.oneOf[0].enum;
                var valid2;
                valid2 = false;
                for (var i2 = 0; i2 < schema2.length; i2++)
                  if (equal(data1, schema2[i2])) {
                    valid2 = true;
                    break;
                  } if (!valid2) {
                  var err = {
                    keyword: 'enum',
                    dataPath: (dataPath || '') + '.on',
                    schemaPath: '#/properties/on/oneOf/0/enum',
                    params: {
                      allowedValues: schema2
                    },
                    message: 'should be equal to one of the allowed values'
                  };
                  if (vErrors === null) vErrors = [err];
                  else vErrors.push(err);
                  errors++;
                } else {}
                if (errors === errs_2) {}
                var valid2 = errors === errs_2;
                if (valid2) {
                  valid1 = prevValid1 = true;
                  passingSchemas1 = 0;
                }
                var errs_2 = errors;
                var schema2 = validate.schema.properties.on.oneOf[1].enum;
                var valid2;
                valid2 = false;
                for (var i2 = 0; i2 < schema2.length; i2++)
                  if (equal(data1, schema2[i2])) {
                    valid2 = true;
                    break;
                  } if (!valid2) {
                  var err = {
                    keyword: 'enum',
                    dataPath: (dataPath || '') + '.on',
                    schemaPath: '#/properties/on/oneOf/1/enum',
                    params: {
                      allowedValues: schema2
                    },
                    message: 'should be equal to one of the allowed values'
                  };
                  if (vErrors === null) vErrors = [err];
                  else vErrors.push(err);
                  errors++;
                } else {}
                if (errors === errs_2) {}
                var valid2 = errors === errs_2;
                if (valid2 && prevValid1) {
                  valid1 = false;
                  passingSchemas1 = [passingSchemas1, 1];
                } else {
                  if (valid2) {
                    valid1 = prevValid1 = true;
                    passingSchemas1 = 1;
                  }
                }
                if (!valid1) {
                  var err = {
                    keyword: 'oneOf',
                    dataPath: (dataPath || '') + '.on',
                    schemaPath: '#/properties/on/oneOf',
                    params: {
                      passingSchemas: passingSchemas1
                    },
                    message: 'should match exactly one schema in oneOf'
                  };
                  if (vErrors === null) vErrors = [err];
                  else vErrors.push(err);
                  errors++;
                  validate.errors = vErrors;
                  return false;
                } else {
                  errors = errs__1;
                  if (vErrors !== null) {
                    if (errs__1) vErrors.length = errs__1;
                    else vErrors = null;
                  }
                }
                if (errors === errs_1) {}
                var valid1 = errors === errs_1;
              }
              if (valid1) {
                var data1 = data.path;
                if (data1 === undefined) {
                  valid1 = false;
                  validate.errors = [{
                    keyword: 'required',
                    dataPath: (dataPath || '') + "",
                    schemaPath: '#/required',
                    params: {
                      missingProperty: 'path'
                    },
                    message: 'should have required property \'path\''
                  }];
                  return false;
                } else {
                  var errs_1 = errors;
                  var errs_2 = errors;
                  if ((typeof data1 === "number")) {
                    if (true) {}
                  }
                  if (errors === errs_2) {
                    if (typeof data1 === "string") {
                      if (!formats['uri-reference'].test(data1)) {
                        validate.errors = [{
                          keyword: 'format',
                          dataPath: (dataPath || '') + '.path',
                          schemaPath: '../../oada.schema.json#/definitions/path/format',
                          params: {
                            format: 'uri-reference'
                          },
                          message: 'should match format "uri-reference"'
                        }];
                        return false;
                      } else {}
                    } else {
                      validate.errors = [{
                        keyword: 'type',
                        dataPath: (dataPath || '') + '.path',
                        schemaPath: '../../oada.schema.json#/definitions/path/type',
                        params: {
                          type: 'string'
                        },
                        message: 'should be string'
                      }];
                      return false;
                    }
                    if (errors === errs_2) {}
                  }
                  var valid2 = errors === errs_2;
                  if (valid2) {}
                  if (errors === errs_1) {}
                  var valid1 = errors === errs_1;
                }
                if (valid1) {
                  var data1 = data.conditions;
                  if (data1 === undefined) {
                    valid1 = false;
                    validate.errors = [{
                      keyword: 'required',
                      dataPath: (dataPath || '') + "",
                      schemaPath: '#/required',
                      params: {
                        missingProperty: 'conditions'
                      },
                      message: 'should have required property \'conditions\''
                    }];
                    return false;
                  } else {
                    var errs_1 = errors;
                    if ((data1 && typeof data1 === "object" && !Array.isArray(data1))) {
                      var errs__1 = errors;
                      var valid2 = true;
                      for (var key1 in data1) {
                        var data2 = data1[key1];
                        var errs_2 = errors;
                        if ((data2 && typeof data2 === "object" && !Array.isArray(data2))) {
                          if (true) {
                            var errs__2 = errors;
                            var valid3 = true;
                            if (data2.condition === undefined) {
                              valid3 = false;
                              validate.errors = [{
                                keyword: 'required',
                                dataPath: (dataPath || '') + '.conditions[\'' + key1 + '\']',
                                schemaPath: '#/properties/conditions/additionalProperties/required',
                                params: {
                                  missingProperty: 'condition'
                                },
                                message: 'should have required property \'condition\''
                              }];
                              return false;
                            } else {
                              var errs_3 = errors;
                              if (!refVal2(data2.condition, (dataPath || '') + '.conditions[\'' + key1 + '\'].condition', data2, 'condition', rootData)) {
                                if (vErrors === null) vErrors = refVal2.errors;
                                else vErrors = vErrors.concat(refVal2.errors);
                                errors = vErrors.length;
                              } else {}
                              if (errors === errs_3) {}
                              var valid3 = errors === errs_3;
                            }
                            if (valid3) {
                              var data3 = data2.options;
                              if (data3 === undefined) {
                                valid3 = true;
                              } else {
                                var errs_3 = errors;
                                if ((!data3 || typeof data3 !== "object" || Array.isArray(data3))) {
                                  validate.errors = [{
                                    keyword: 'type',
                                    dataPath: (dataPath || '') + '.conditions[\'' + key1 + '\'].options',
                                    schemaPath: '#/properties/conditions/additionalProperties/properties/options/type',
                                    params: {
                                      type: 'object'
                                    },
                                    message: 'should be object'
                                  }];
                                  return false;
                                }
                                var valid3 = errors === errs_3;
                              }
                              if (valid3) {}
                            }
                            if (errs__2 == errors) {}
                          }
                        } else {
                          validate.errors = [{
                            keyword: 'type',
                            dataPath: (dataPath || '') + '.conditions[\'' + key1 + '\']',
                            schemaPath: '#/properties/conditions/additionalProperties/type',
                            params: {
                              type: 'object'
                            },
                            message: 'should be object'
                          }];
                          return false;
                        }
                        if (errors === errs_2) {}
                        var valid2 = errors === errs_2;
                        if (!valid2) break;
                      }
                      if (valid2) {}
                      if (errs__1 == errors) {}
                    }
                    if (errors === errs_1) {}
                    var valid1 = errors === errs_1;
                  }
                  if (valid1) {
                    var data1 = data.actions;
                    if (data1 === undefined) {
                      valid1 = false;
                      validate.errors = [{
                        keyword: 'required',
                        dataPath: (dataPath || '') + "",
                        schemaPath: '#/required',
                        params: {
                          missingProperty: 'actions'
                        },
                        message: 'should have required property \'actions\''
                      }];
                      return false;
                    } else {
                      var errs_1 = errors;
                      if ((data1 && typeof data1 === "object" && !Array.isArray(data1))) {
                        var errs__1 = errors;
                        var valid2 = true;
                        for (var key1 in data1) {
                          var data2 = data1[key1];
                          var errs_2 = errors;
                          if ((data2 && typeof data2 === "object" && !Array.isArray(data2))) {
                            if (true) {
                              var errs__2 = errors;
                              var valid3 = true;
                              if (data2.action === undefined) {
                                valid3 = false;
                                validate.errors = [{
                                  keyword: 'required',
                                  dataPath: (dataPath || '') + '.actions[\'' + key1 + '\']',
                                  schemaPath: '#/properties/actions/additionalProperties/required',
                                  params: {
                                    missingProperty: 'action'
                                  },
                                  message: 'should have required property \'action\''
                                }];
                                return false;
                              } else {
                                var errs_3 = errors;
                                if (!refVal[2](data2.action, (dataPath || '') + '.actions[\'' + key1 + '\'].action', data2, 'action', rootData)) {
                                  if (vErrors === null) vErrors = refVal[2].errors;
                                  else vErrors = vErrors.concat(refVal[2].errors);
                                  errors = vErrors.length;
                                } else {}
                                if (errors === errs_3) {}
                                var valid3 = errors === errs_3;
                              }
                              if (valid3) {
                                var data3 = data2.options;
                                if (data3 === undefined) {
                                  valid3 = true;
                                } else {
                                  var errs_3 = errors;
                                  if ((!data3 || typeof data3 !== "object" || Array.isArray(data3))) {
                                    validate.errors = [{
                                      keyword: 'type',
                                      dataPath: (dataPath || '') + '.actions[\'' + key1 + '\'].options',
                                      schemaPath: '#/properties/actions/additionalProperties/properties/options/type',
                                      params: {
                                        type: 'object'
                                      },
                                      message: 'should be object'
                                    }];
                                    return false;
                                  }
                                  var valid3 = errors === errs_3;
                                }
                                if (valid3) {}
                              }
                              if (errs__2 == errors) {}
                            }
                          } else {
                            validate.errors = [{
                              keyword: 'type',
                              dataPath: (dataPath || '') + '.actions[\'' + key1 + '\']',
                              schemaPath: '#/properties/actions/additionalProperties/type',
                              params: {
                                type: 'object'
                              },
                              message: 'should be object'
                            }];
                            return false;
                          }
                          if (errors === errs_2) {}
                          var valid2 = errors === errs_2;
                          if (!valid2) break;
                        }
                        if (valid2) {}
                        if (errs__1 == errors) {}
                      }
                      if (errors === errs_1) {}
                      var valid1 = errors === errs_1;
                    }
                    if (valid1) {}
                  }
                }
              }
            }
          }
        }
        if (errs__0 == errors) {}
      }
    }
    if (errors === 0) {}
    validate.errors = vErrors;
    return errors === 0;
  };
})();
validate.schema = {
  "$id": "https://formats.openag.io/trellis/rules/configured.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "Representation of a constructed OADA rule for rules engine",
  "properties": {
    "enabled": {
      "description": "Wether this rule should be active",
      "type": "boolean",
      "default": true
    },
    "services": {
      "description": "Array of the names of the services involved in this rule",
      "$comment": "Should this be here?",
      "type": "array",
      "items": {
        "type": "string"
      },
      "uniqueItems": true
    },
    "type": {
      "description": "Content-type this rule applies to",
      "type": "string"
    },
    "on": {
      "$comment": "Where should this definiton live?",
      "description": "Whether to trigger on new item, changed item, etc.",
      "oneOf": [{
        "description": "Trigger on a new item being added",
        "enum": ["new"]
      }, {
        "description": "Trigger on any change to an item",
        "enum": ["change"]
      }],
      "default": "new"
    },
    "path": {
      "description": "The path to an OADA list to which to apply this rule",
      "$ref": "../../oada.schema.json#/definitions/path"
    },
    "conditions": {
      "description": "List of conditions involved in this rule",
      "additionalProperties": {
        "description": "Particular condition involved in this rule",
        "type": "object",
        "properties": {
          "condition": {
            "description": "A link to the condition document",
            "$ref": "../../oada/link/v1.schema.json#/definitions/versioned"
          },
          "options": {
            "description": "Parameter values for this condition",
            "type": "object"
          }
        },
        "required": ["condition"]
      }
    },
    "actions": {
      "description": "List of actions involved in this rule",
      "additionalProperties": {
        "description": "Particular action involved in this rule",
        "type": "object",
        "properties": {
          "action": {
            "description": "A link to the action document",
            "$ref": "../../oada/link/v1.schema.json#/definitions/versioned"
          },
          "options": {
            "description": "Parameter values for this action",
            "type": "object"
          }
        },
        "required": ["action"]
      }
    }
  },
  "required": ["type", "path", "conditions", "actions"],
  "examples": [{
    "type": "application/json",
    "enabled": true,
    "path": "/bookmarks/foo",
    "on": "new",
    "services": ["test-service"],
    "actions": {
      "test-action": {
        "action": {
          "_id": "resources/foo",
          "_rev": 1
        },
        "options": {
          "foo": 1,
          "bar": "a"
        }
      }
    },
    "conditions": {}
  }]
};
validate.errors = null;
module.exports = validate;