window.addEventListener('DOMContentLoaded', init, false);
 
function init() {
	//Add event listeners for the showing and hiding of the message input
	  window.addEventListener("hide-message-input", hideMessageInput, false);
	  window.addEventListener("show-message-input", showMessageInput, false);
}  

var initESW = function(gslbBaseURL) {
	embedded_svc.settings.displayHelpButton = true; //Or false
	embedded_svc.settings.language = ''; //For example, enter 'en' or 'en-US'

	embedded_svc.settings.defaultMinimizedText = 'CLINICIAN SUPPORT'; //(Defaults to Chat with an Expert)
    embedded_svc.settings.chatbotAvatarImgURL = 'https://nccc-prod.secure.force.com/botendpointprod/resource/ChatbotLogoNCCC';

	//embedded_svc.settings.defaultMinimizedText = '...'; //(Defaults to Chat with an Expert)
	//embedded_svc.settings.disabledMinimizedText = '...'; //(Defaults to Agent Offline)

	//embedded_svc.settings.loadingText = ''; //(Defaults to Loading)
	//embedded_svc.settings.storageDomain = 'yourdomain.com'; //(Sets the domain for your deployment so that visitors can navigate subdomains during a chat session)

	// Settings for Chat
	//embedded_svc.settings.directToButtonRouting = function(prechatFormData) {
		// Dynamically changes the button ID based on what the visitor enters in the pre-chat form.
		// Returns a valid button ID.
	//};
	//embedded_svc.settings.prepopulatedPrechatFields = {}; //Sets the auto-population of pre-chat form fields
	//embedded_svc.settings.fallbackRouting = []; //An array of button IDs, user IDs, or userId_buttonId
	//embedded_svc.settings.offlineSupportMinimizedText = '...'; //(Defaults to Contact Us)

	embedded_svc.settings.enabledFeatures = ['LiveAgent'];
	embedded_svc.settings.entryFeature = 'LiveAgent';
	embedded_svc.settings.autoOpenPostChat = true;

	embedded_svc.init(
		'https://nccc.my.salesforce.com',
		'https://nccc-prod.secure.force.com/botendpointprod',
		gslbBaseURL,
		'00Di0000000YeC4',
		'NCCC_Chatbot',
		{
			baseLiveAgentContentURL: 'https://c.la1-c2-ia5.salesforceliveagent.com/content',
			deploymentId: '5723r000000JPws',
			buttonId: '5733r000000JSo3',
			baseLiveAgentURL: 'https://d.la1-c2-ia5.salesforceliveagent.com/chat',
			eswLiveAgentDevName: 'EmbeddedServiceLiveAgent_Parent04I3r000000fxUlEAI_1833237c719',
			isOfflineSupportEnabled: false
		}
	);
	};

	if (!window.embedded_svc) {
		var s = document.createElement('script');
		s.setAttribute('src', 'https://nccc.my.salesforce.com/embeddedservice/5.0/esw.min.js');
		s.onload = function() {
			initESW(null);
		};
		document.body.appendChild(s);
	} else {
		initESW('https://service.force.com');
	}

	

/**
 ** The following functions were added to respond to ShowMessageInput and HideMessageInput events sent from the chatmessageoverride 
 */
function showMessageInput() {
	console.log('showMessageInput');
	const chasitorControl = window.document.querySelectorAll('.chasitorControls');
	if(chasitorControl.length > 0){
		console.log('found chasitorControl');
		chasitorControlsChildren = window.document.querySelectorAll('.chasitorControls')[0].style = '';
		console.log(chasitorControlsChildren);
		Array.from(chasitorControlsChildren).forEach((element, index) => {
			if(index == 3){
				element.style.display = 'none';
			} else {
				element.style.display = 'flex';
			}
		});
	}
}

function hideMessageInput() {
	console.log('hideMessageInput');
	const chasitorControl = window.document.querySelectorAll('.chasitorControls');
	if(chasitorControl.length > 0){
		chasitorControlsChildren = window.document.querySelectorAll('.chasitorControls')[0].children;
		console.log(chasitorControlsChildren);
		Array.from(chasitorControlsChildren).forEach((element) => {
			element.style.display = 'none';
		});
		const newDiv = document.createElement("span");

		// and give it some content
		const newContent = document.createTextNode("Please select from a menu item above");

		// add the text node to the newly created div
		newDiv.appendChild(newContent);

		window.document.querySelectorAll('.chasitorControls')[0].appendChild(newDiv);
		window.document.querySelectorAll('.chasitorControls')[0].setAttribute('aria-label', 'Please select from a menu item above.');
		window.document.querySelectorAll('.chasitorControls')[0].style = 'display: flex;justify-content: center;align-content: center;flex-direction: column-reverse;text-align: left;color: gray;';
	}
}