class EdsCStatusMessage {
	constructor(selector = 'data-eds-c-status-message') {
		this.selector = `[${selector}]`;
	}

	// Function to close the status message
	closeStatusMessage(event) {
		const statusMessageElement = event.target.closest('.eds-c-status-message');
		statusMessageElement.parentNode.removeChild(statusMessageElement);
	}

	init() {
		const componentElements = document.querySelectorAll(this.selector);
		if (!componentElements || componentElements.length === 0) {
			return;
		}

		componentElements.forEach(componentElement => {
			const closeButton = componentElement.querySelector('.eds-c-status-message__dismiss-button');
			if (closeButton) {
				// Event listener for clicking the close button
				closeButton.addEventListener('click', this.closeStatusMessage);
				
				// Event listener for pressing Enter key on close button
				closeButton.addEventListener('keypress', event => {
					if (event.key === 'Enter') {
						this.closeStatusMessage(event);
					}
				});
			}
		});
	}
}

export default EdsCStatusMessage;
