text: import nod from '../common/nod';
  import { CollapsibleEvents } from '../common/collapsible';
  import forms from '../common/models/forms';
  import scrollToElement from 'scroll-to-element'; // Chiara
  import mediaQueryListFactory from '../common/media-query-list'; // Chiara
  
  const mediumMedia = mediaQueryListFactory('medium'); // Chiara
  
  export default class {
      constructor($reviewForm) {
          this.validator = nod({
              submit: $reviewForm.find('input[type="submit"]'),
          });
  
          this.$reviewsContent = $('#product-reviews');
          this.$collapsible = $('[data-collapsible]', this.$reviewsContent);
  
          this.initLinkBind();
          this.initLinkBindChiara(); // Chiara theme
          this.injectPaginationLink();
          this.collapseReviews();
      }
  
      /**
       * On initial page load, the user clicks on "(12 Reviews)" link
       * The browser jumps to the review page and should expand the reviews section
       */
      initLinkBind() {
          const $content = $('#productReviews-content', this.$reviewsContent);
  
          $('.productView-reviewLink').on('click', () => {
              if (!$content.hasClass('is-open')) {
                  this.$collapsible.trigger(CollapsibleEvents.click);
              }
          });
      }
  
      // Chiara theme
      initLinkBindChiara() {
          $('.productView-reviewLink').on('click', (event) => {
              if (mediumMedia && mediumMedia.matches) {
                  const $tab = $('.productView-tab--reviews');
                  if ($tab.length > 0) {
                      event.preventDefault();
                      if (!$tab.hasClass('is-active')) {
                          $tab.find('a').trigger('click');
                      }
                      scrollToElement($tab.get(0), {
                          offset: -100,
                          duration: 100,
                      });
                  }
              }
          });
      }
  
      collapseReviews() {
          // We're in paginating state, do not collapse
          if (window.location.hash && window.location.hash.indexOf('#product-reviews') === 0) {
              return;
          }
  
          // force collapse on page load
          this.$collapsible.trigger(CollapsibleEvents.click);
      }
  
      /**
       * Inject ID into the pagination link
       */
      injectPaginationLink() {
          const $nextLink = $('.pagination-item--next .pagination-link', this.$reviewsContent);
          const $prevLink = $('.pagination-item--previous .pagination-link', this.$reviewsContent);
  
          if ($nextLink.length) {
              $nextLink.attr('href', `${$nextLink.attr('href')}#product-reviews`);
          }
  
          if ($prevLink.length) {
              $prevLink.attr('href', `${$prevLink.attr('href')}#product-reviews`);
          }
      }
  
      registerValidation(context) {
          this.context = context;
          this.validator.add([{
              selector: '[name="revrating"]',
              validate: 'presence',
              errorMessage: this.context.reviewRating,
          }, {
              selector: '[name="revtitle"]',
              validate: 'presence',
              errorMessage: this.context.reviewSubject,
          }, {
              selector: '[name="revtext"]',
              validate: 'presence',
              errorMessage: this.context.reviewComment,
          }, {
              selector: '[name="email"]',
              validate: (cb, val) => {
                  const result = forms.email(val);
                  cb(result);
              },
              errorMessage: this.context.reviewEmail,
          }]);
  
          return this.validator;
      }
  
      validate() {
          return this.validator.performCheck();
      }
  }
  