'use strict';

var _assign         = require( 'lodash-node/compat/objects/assign' );
var _isString       = require( 'lodash-node/compat/objects/isString' );
var _isPlainObject  = require( 'lodash-node/compat/objects/isPlainObject' );
var atomic          = require( 'atomic' );
var BaseRelay       = require( './base' );

/**
 * Ajax relay.
 *
 * Fire a log message to a REST endpoint using AJAX.
 *
 * @param {object} opts                 - Options object
 * @param {string} opts.endpoint        - URL string of the endpoint
 * @param {string} [opts.minLevel='error']
 *                                      - Minimum level to log at
 * @param {object} [opts.logDatamap={level:'level', message: 'message'}]
 *                                      - Map from data sent to endpoint to
 *                                        `logEvent` data
 */
module.exports = function AjaxRelay ( opts ) {

    // Process options
    if ( !_isPlainObject( opts ) || !_isString( opts.endpoint ) ) {
        throw new TypeError( 'New Ajax relays must supply at least an `opts.endpoint` URL string.' );
    }
    opts = _assign( {}, {
        minLevel:   'error', // Log only error-level messages by default
        verbose:    false,
        logDataMap: {
            level:      'level',
            message:    'message'
        }
    }, opts );

    opts.onLog = function ( logEvent ) {

        // Map logData properties to logEvent properties
        var logData = {};
        for ( var logProp in opts.logDataMap ) {
            logData[ logProp ] = logEvent[ opts.logDataMap[ logProp ] ];
        }

        atomic.post( opts.endpoint, logData )
            .success( function () {
                if ( opts.verbose ) {
                    try {
                        console.info( 'Ajax log to "' + opts.endpoint + '" was successful.' );
                    } catch ( e ) {}
                }
            } )
            .error( function () {
                if ( opts.verbose ) {
                    try {
                        console.warn( 'Warning: Ajax log to "' + opts.endpoint + '" was NOT successful.' );
                    } catch ( e ) {}
                }
            } );
    };

    return new BaseRelay( opts );
};

@@ -12,227 +12,8 @@
 ';%0A%0A
-var _assign         = require( 'lodash-node/compat/objects/assign' );%0Avar _isString       = require( 'lodash-node/compat/objects/isString' );%0Avar _isPlainObject  = require( 'lodash-node/compat/objects/isPlainObject' );%0A
 var 
@@ -704,46 +704,17 @@
  ( !
-_isPlainObject( 
 opts
- )
  %7C%7C !
-_isString( 
 opts
@@ -724,18 +724,16 @@
 dpoint )
- )
  %7B%0A     
@@ -847,42 +847,76 @@
 opts
- = _assign( %7B%7D, %7B%0A        
+.minLevel   = typeof opts.minLevel   !== 'undefined' ? opts.
 minLevel
 :   
@@ -915,19 +915,20 @@
 evel
-:
   
+ :
  'error'
 , //
@@ -927,107 +927,181 @@
 ror'
-, // Log only error-level messages by default%0A        
+;%0A    opts.verbose    = typeof opts.verbose    !== 'undefined' ? opts.
 verbose
-:
+ 
    
+:
  false
-,
+;
 %0A    
-    logDataMap: %7B%0A    
+opts.logDataMap = typeof opts.logDataMap !== 'undefined' ? opts.logDataMap : %7B%0A
     
@@ -1133,20 +1133,16 @@
         
-    
 message:
@@ -1159,32 +1159,13 @@
 ge'%0A
-    
     %7D
-%0A    %7D, opts );
 %0A%0A  

Remove dependency on lodash assign, isString, and isPlain object in ajax relay, drop minified size by ~20k