'use strict';

// MODULES //

var isNonNegativeInteger = require( '@stdlib/utils/is-nonnegative-integer' );
var isUnityProbabilityArray = require( '@stdlib/utils/is-unity-probability-array' );
var isBoolean = require( '@stdlib/utils/is-boolean').isPrimitive;
var isObject = require( '@stdlib/utils/is-plain-object' );


// MAIN //

/**
* Validates function options.
*
* @param {Object} opts - destination for validated options
* @param {Options} options - function options
* @param {NonNegativeInteger} [options.size] - sample size
* @param {ProbabilityArray} [options.probs] - element probabilities
* @param {boolean} [options.replace] - boolean indicating whether to sample with replacement
* @param {boolean} [options.mutate] - boolean indicating whether to mutate the `pool` when sampling without replacement
* @returns {(null|Error)} null or an error
*
* @example
* var opts = {};
* var options = {
*     'size': 10,
*     'replace': false,
*     'mutate': true,
*     'probs': [ 0.7, 0.3 ]
* };
* var err = validate( opts, options );
* if ( err ) {
*     throw err;
* }
*/
function validate( opts, options ) {
	if ( !isObject( options ) ) {
		return new TypeError( 'invalid input argument. Options argument must be an object. Value: `' + options + '`.' );
	}
	if ( options.hasOwnProperty( 'size' ) ) {
		opts.size = options.size;
		if ( !isNonNegativeInteger( opts.size ) ) {
			return new TypeError( 'invalid option. `size` option must be a non-negative integer. Option: `' + opts.size + '`.' );
		}
	}
	if ( options.hasOwnProperty( 'probs' ) ) {
		opts.probs = options.probs;
		if ( !isUnityProbabilityArray( opts.probs ) ) {
			return new TypeError( 'invalid option. `probs` option must be an array of probabilities that sum to one. Option: `' + opts.probs + '`.' );
		}
	}
	if ( options.hasOwnProperty( 'mutate' ) ) {
		opts.mutate = options.mutate;
		if ( !isBoolean( opts.mutate ) ) {
			return new TypeError( 'invalid option. `mutate` option must be a boolean primitive. Option: `' + opts.mutate + '`.' );
		}
	}
	if ( options.hasOwnProperty( 'replace' ) ) {
		opts.replace = options.replace;
		if ( !isBoolean( opts.replace ) ) {
			return new TypeError( 'invalid option. `replace` option must be a boolean primitive. Option: `' + opts.replace + '`.' );
		}
	}
	return null;
} // end FUNCTION validate()


// EXPORTS //

module.exports = validate;

@@ -182,24 +182,86 @@
 y-array' );%0A
+var hasOwnProp = require( '@stdlib/utils/has-own-property' );%0A
 var isBoolea
@@ -1322,32 +1322,24 @@
 );%0A%09%7D%0A%09if ( 
-options.
 hasOwnProper
@@ -1336,21 +1336,26 @@
 sOwnProp
-erty(
+( options,
  'size' 
@@ -1564,32 +1564,24 @@
 %09%7D%0A%09%7D%0A%09if ( 
-options.
 hasOwnProper
@@ -1578,21 +1578,26 @@
 sOwnProp
-erty(
+( options,
  'probs'
@@ -1834,32 +1834,24 @@
 %09%7D%0A%09%7D%0A%09if ( 
-options.
 hasOwnProper
@@ -1848,21 +1848,26 @@
 sOwnProp
-erty(
+( options,
  'mutate
@@ -2078,24 +2078,16 @@
 %7D%0A%09if ( 
-options.
 hasOwnPr
@@ -2092,13 +2092,18 @@
 Prop
-erty(
+( options,
  're

Use hasOwnProp utility