import { Utility } from '../../core/utility';
import './pageactions.sass';
import * as throttle from 'lodash.throttle';

export const PAGEACTION_SECONDARY_UTIL_SELECTOR = '.pagenav__list-item';
const PAGEACTION_SECONDARY_INITIALIZED_CLASS = '.pagenav-list-item--initialized';
const PAGEACTION_SECONDARY_CLASS = 'pagenav-secondary';

@Utility({
  selector: PAGEACTION_SECONDARY_UTIL_SELECTOR,
})
export class PageActionSecondaryUtil {
  _element;
  navIdent;
  idPrefix;
  radioButton;
  closeButton;
  container;
  wasOpen;

  _throttleUpdateWasOpen;

  constructor(element) {
    if (!element) {
      throw new Error('Pageaction Secondary utility needs to be passed an element!');
    }

    if (element.classList.contains(PAGEACTION_SECONDARY_INITIALIZED_CLASS)) {
      return false;
    }

    this._element = element;

    const childContainer = this._element.querySelector('.pagenav-item__children');

    if (!childContainer) {
      return false;
    }

    if (this._element.classList.contains(PAGEACTION_SECONDARY_CLASS)) {
      this.navIdent = 'secondary';
    } else {
      const links = Array.from(this._element.querySelectorAll('.pagenav-item__link')).filter(l => !childContainer.contains(l));

      if (!links || Array.from(links).length !== 1) {
	throw new Error('Pageaction Secondary utility could not find associated link!');
      }
      this.navIdent = links[0].id;
    }
    
    const idMatch = this.navIdent.match(/^(r[0-9]+__)(.+)$/);
    if (idMatch) {
      this.idPrefix = idMatch[1];
      this.navIdent = idMatch[2];
    } else {
      this.idPrefix = '';
    }
    
    this.radioButton = document.getElementById(`${this.idPrefix}pageaction-item__expand-${this.navIdent}`);
    if (!this.radioButton) {
      throw new Error('Pageaction Secondary utility could not find associated radio button!');
    }

    this.closeButton = document.getElementById(`${this.idPrefix}pageaction-item__expand-none`);
    if (!this.closeButton) {
      throw new Error('Pageaction Secondary utility could not find radio button for closing!');
    }

    this.container = document.querySelector('.pagenav-item__children-wrapper');
    if (!this.container) {
      throw new Error('Pageaction Secondary utility could not find associated container!');
    }
   
    const closer = this._element.querySelector('.pagenav-item__close-label');
    if (closer) {
      closer.classList.add('pagenav-item__close-label--hidden');
    }

    this.updateWasOpen();
    this.throttleUpdateWasOpen = throttle(this.updateWasOpen.bind(this), 100, { leading: false, trailing: true });
    
    this._element.classList.add(PAGEACTION_SECONDARY_INITIALIZED_CLASS);
  }

  start() {
    if (!this.container)
      return;
    
    window.addEventListener('click', this.clickHandler.bind(this));
    this.radioButton.addEventListener('change', this.throttleUpdateWasOpen.bind(this));
  }

  clickHandler() {
    if (!this.container.contains(event.target) && window.document.contains(event.target) && this.wasOpen) {
      this.close();
    }
  }

  close() {
    this.radioButton.checked = false;
    this.throttleUpdateWasOpen();
  }

  isOpen() {
    return this.radioButton.checked;
  }

  updateWasOpen() {
    this.wasOpen = this.isOpen();
  }

  destroy() { /* TODO */ }
}



export const PageActionsUtils = [
  PageActionSecondaryUtil,
];
