/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var getKeys = require( '@stdlib/utils/keys' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var isRegExp = require( '@stdlib/assert/is-regexp' );
var reFromString = require( '@stdlib/utils/regexp-from-string' );
var replace = require( '@stdlib/string/replace' );


// VARIABLES //

var RE_REGEXP_CHARS = /[-\/\\^$*+?.()|[\]{}]/g; // eslint-disable-line no-useless-escape


// MAIN //

/**
* Normalizes a license `header` input value.
*
* @private
* @param {(string|RegExp|Object)} header - license header to lint against
* @returns {Object} normalized header value
*/
function normalize( header ) {
	var keys;
	var out;
	var k;
	var v;
	var i;
	if ( isString( header ) ) {
		header = replace( header, RE_REGEXP_CHARS, '\\$&' );
		out = {
			'default': reFromString( '/'+header+'/g' )
		};
	} else if ( isRegExp( header ) ) {
		out = {
			'default': header
		};
	} else {
		keys = getKeys( header );
		out = {};
		for ( i = 0; i < keys.length; i++ ) {
			k = keys[ i ];
			v = header[ k ];
			if ( isString( v ) ) {
				v = replace( v, RE_REGEXP_CHARS, '\\$&' );
				out[ k ] = reFromString( '/'+v+'/g' );
			} else {
				out[ k ] = v;
			}
		}
	}
	return out;
}


// EXPORTS //

module.exports = normalize;
