define(
    [	'jquery',
    	'mage/utils/wrapper',
        'mage/utils/objects'],
    function ($, wrapper, mageUtils) {
        'use strict';

        return function (target) {
            target.quoteAddressToFormAddressData =
	            wrapper.wrapSuper(target.quoteAddressToFormAddressData, function (modelAddress) {
	                var shippingAddress = this._super(modelAddress);

	                //split housenumber from street
	                if(mageUtils.isObject(shippingAddress.street) && typeof shippingAddress.street[0] != 'undefined')
                	{
	                	let i = shippingAddress.street[0].search(/\d/g);
	                	
	                	if(i > 0)
                		{
	                		shippingAddress.street[2] = shippingAddress.street[0].substr(i);
	                		shippingAddress.street[0] = shippingAddress.street[0].substr(0, i).trim();
                		}
                	}
	                
	                return shippingAddress;
	            });
            
            target.formAddressDataToQuoteAddress = 
            	wrapper.wrapSuper(target.formAddressDataToQuoteAddress, function(formAddress) {
            		if(formAddress) 
            		{
	                    if(typeof formAddress.street != 'undefined' && typeof formAddress.street[2] != 'undefined')
	                	{
	                    	//remove any number part first
	                    	let i = formAddress.street[0].search(/\d/g);
	                    	if(i > -1)
	                		{
	                    		formAddress.street[0] = formAddress.street[0].substr(0, i).trim();
	                		}
	                    	
	                    	//remove number part if is included in street part
	                    	while(formAddress.street[2].trim() != '' && formAddress.street[0].trim().endsWith(formAddress.street[2].trim()))
                    		{
	                    		let n = formAddress.street[0].lastIndexOf(formAddress.street[2].trim()); 
	                    		formAddress.street[0] = formAddress.street[0].substr(0, n).trim();
                    		}
	                    	
	                    	formAddress.street[0] = formAddress.street[0].trim() + ' ' + formAddress.street[2].trim();
	                    	delete formAddress.street[2];
	                	}
            		}
            		
            		return this._super(formAddress);
            	});

            return target;
        };
    }
);
