# src/Util.js

    /* eslint-disable no-console, consistent-return */
    const util = (function Util() {
      function validateConfig(context, targetConfig) {
        const mandatorySpec = Object.seal({
          credential: {
            key: undefined,
            serviceId: undefined,
          },
        });

        Object.keys(mandatorySpec).forEach((category) => {
          Object.keys(mandatorySpec[category]).forEach((item) => {
            if (targetConfig[category][item]) {
              (mandatorySpec[category][item] = true);
            } else {
              (mandatorySpec[category][item] = false);
            }
          });
        });

        Object.keys(mandatorySpec).forEach((category) => {
          Object.keys(mandatorySpec[category]).forEach((item) => {
            if (mandatorySpec[category][item] === false) {
              if (context.eventManager.hasEventListener('onError')) { context.eventManager.dispatchEvent('onError', 'InvalidParameterError'); }
            }
          });
        });
      }

      function bind(fn, context){
      	if(!fn || !context){
      		throw new Error("Failed to execute 'bind' on 'utils': 2 arguments required, but only " + arguments.length + " present.");
      	}
      	return function(){
      		fn.apply(context, Array.prototype.slice.call(arguments));
      	};
      }

      function replaceCodec(sdp, mLineReg, preferCodec){
    		var mLine,
    			newMLine = [],
    			sdpCodec,
    			mLineSplit,
    			reg = new RegExp("a=rtpmap:(\\d+) " + preferCodec + "/\\d+");

    		mLine = sdp.match(mLineReg);
    		if(!mLine){
    			return sdp;
    		}

    		sdpCodec = sdp.match(reg);
    		if(!sdpCodec){
    			return sdp;
    		}

    		mLine = mLine[0];
    		sdpCodec = sdpCodec[1];

    		mLineSplit = mLine.split(" ");
    		newMLine.push(mLineSplit[0]);
    		newMLine.push(mLineSplit[1]);
    		newMLine.push(mLineSplit[2]);
    		newMLine.push(sdpCodec);

    		for(var i=3; i<mLineSplit.length; i++){
    			if(mLineSplit[i] !== sdpCodec){
    				newMLine.push(mLineSplit[i]);
    			}
    		}

    		return sdp.replace(mLine, newMLine.join(" "));
    	}

    /**
     * @param Array elements
     * @param String boundary
     * @return String
     */
    function buildMessage(filename, binary, boundary) {
        var CRLF = "\r\n";
        var parts = [];
        var part = "";
        var fileName = filename;

        /*
         * Content-Disposition header contains name of the field
         * used to upload the file and also the name of the file as
         * it was on the user's computer.
         */
        part += 'Content-Disposition: form-data; ';
        part += 'name=files"; ';
        part += 'filename="'+ fileName + '"' + CRLF;

        /*
         * Content-Type header contains the mime-type of the file
         * to send. Although we could build a map of mime-types
         * that match certain file extensions, we'll take the easy
         * approach and send a general binary header:
         * application/octet-stream
         */
        part += "Content-Type: application/octet-stream";
        part += CRLF + CRLF; // marks end of the headers part

        /*
         * File contents read as binary data, obviously
         */
        //part += element.files[0].getAsBinary() + CRLF;
        part += binary + CRLF;
        parts.push(part);

        var request = "--" + boundary + CRLF;
            request+= parts.join("--" + boundary + CRLF);
            request+= "--" + boundary + "--" + CRLF;

        return request;
      }
    return Object.freeze({
      validateConfig,
      bind,
      buildMessage,
    });
    }());
    export default util;