import {makeArray, Expander} from '../../../themes/springernature/js/helpers';

class EdsCHeaderNew {

	constructor(selector = 'data-eds-c-header-new'){
		this.selector = `[${selector}]`;
		this.dataComponent = '[data-eds-c-header-new-expander]';
		this.expanderAnchor = '[data-eds-c-header-new-expander-anchor]';
		this.notificationNavActionItem = '[data-eds-c-header-new-your-work-js]';
		this.ariaLiveAnnouncementsList = '[data-eds-c-header-new-aria-live-announcements]';
		this.cartAriaLabel = '[data-eds-c-header-new-cart-aria-label]';
		this.notificationAriaLabel = '[data-eds-c-header-new-notification-aria-label]';
		this.tethered = 'has-tethered';
	}

	_findTarget(headerElement, selector){
		if (selector) {
			return document.querySelector(selector);
		}
		return null;
	}

	_cartUiHandler() {
		const spanElement = document.querySelector('.eds-c-header-new__action-items__cart-count');
		if (spanElement) {
			if (spanElement.classList.contains('u-display-none')) {
				spanElement.classList.remove('u-display-none');
			}
		}
	}

	_createAndAppendNewAriaLiveItem (message) {
		const el = document.querySelector(this.ariaLiveAnnouncementsList);
		const newItem = document.createElement('p');
		newItem.innerHTML = message;
		el.appendChild(newItem);
	}

	cartHandler(count, message) {
		if (count >= 0) {
			this._cartUiHandler();
		}
		document.querySelector('.eds-c-header-new__action-items__cart-count').innerHTML = count;
		if (document.querySelector(this.cartAriaLabel)) {
			document.querySelector(this.cartAriaLabel).innerHTML = message;
		}
		this._createAndAppendNewAriaLiveItem(message);
	}

	notificationHandler(message) {
		const actionItemElement = document.querySelector(this.notificationNavActionItem);
		actionItemElement.classList.add('eds-c-header-new__action-items--has-notification'); // add visual red dot
		if (document.querySelector(this.notificationAriaLabel)) {
			document.querySelector(this.notificationAriaLabel).innerHTML = message;
		}
		this._createAndAppendNewAriaLiveItem(message);
	}

	cleanAriaLiveAnnouncements() {
		document.querySelector(this.ariaLiveAnnouncementsList).innerHTML = '';
	}

	init() {
		const headerElement = document.querySelector(this.selector);

		if (!headerElement) {
			return;
		}

		const triggerElements = document.querySelectorAll(this.dataComponent);

		if (triggerElements.length === 0 || !headerElement) {
			return;
		}

		makeArray(triggerElements).forEach(trigger => {
			const targetElement = this._findTarget(headerElement, trigger.getAttribute('data-eds-c-header-new-js'));

			if (!targetElement) {
				return;
			}
			const triggerAttributes = [{name: 'role', value: 'button'}];
			triggerAttributes.forEach(function (attribute) {
				trigger.setAttribute(attribute.name, attribute.value);
			});
			if (targetElement.hasAttribute('data-eds-c-header-new-expander-append')) {
				headerElement.querySelector(this.expanderAnchor).append(targetElement);
			} else {
				headerElement.after(targetElement);
			}
			targetElement.classList.add(this.tethered);
			const expander = new Expander(trigger, targetElement, {AUTOFOCUS: 'firstTabbable'});
			expander.init();

			return expander;
		});
	}
}

export default EdsCHeaderNew;

