text: import { isObject } from './utils';
  import Validator from './validator';
  
  /**
   * Checks if a parent validator instance was requested.
   * @param {Object|Array} injections
   */
  const requestsValidator = (injections) => {
    if (! injections) {
      return false;
    }
  
    /* istanbul ignore next */
    if (Array.isArray(injections) && ~injections.indexOf('$validator')) {
      return true;
    }
  
    if (isObject(injections) && injections.$validator) {
      return true;
    }
  
    return false;
  };
  
  /**
   * Creates a validator instance.
   * @param {Vue} vm
   * @param {Object} options
   */
  const createValidator = (vm, options) => new Validator(null, {
    init: false,
    vm,
    fastExit: options.fastExit
  });
  
  export default (Vue, options = {}) => {
    const mixin = {};
    mixin.provide = function providesValidator () {
      if (this.$validator) {
        return {
          $validator: this.$validator
        };
      }
  
      return {};
    };
  
    mixin.beforeCreate = function beforeCreate () {
      // if its a root instance, inject anyways, or if it requested a new instance.
      if (this.$options.$validates || !this.$parent) {
        this.$validator = createValidator(this, options);
      }
  
      const requested = requestsValidator(this.$options.inject);
  
      // if automatic injection is enabled and no instance was requested.
      if (! this.$validator && options.inject && !requested) {
        this.$validator = createValidator(this, options);
      }
  
      // don't inject errors or fieldBag as no validator was resolved.
      if (! requested && ! this.$validator) {
        return;
      }
  
      // There is a validator but it isn't injected, mark as reactive.
      if (! requested && this.$validator) {
        Vue.util.defineReactive(this.$validator, 'errors', this.$validator.errors);
        Vue.util.defineReactive(this.$validator, 'fieldBag', this.$validator.fieldBag);
      }
  
      if (! this.$options.computed) {
        this.$options.computed = {};
      }
  
      this.$options.computed[options.errorBagName || 'errors'] = function errorBagGetter () {
        return this.$validator.errors;
      };
      this.$options.computed[options.fieldsBagName || 'fields'] = function fieldBagGetter () {
        return this.$validator.fieldBag;
      };
    };
  
    return mixin;
  };
  