import $ from 'jquery';

class IHKEventOverviewTiles {
  constructor(section) {
    this.section = section.addClass('initiated');
    this.cols = section.find('.col');

    const fullOffset = Math.ceil(this.cols.length / 6) * 3 - this.cols.last().index() - 1;
    const fullOffset2 = Math.ceil(this.cols.length / 3) * 3 - this.cols.last().index() - 1;
    if (fullOffset === 0) {
      return;
    }
    if (fullOffset2 === 0) {
      return;
    }

    let firstHalfWidth = this.cols.length - fullOffset * 2;
    let firstHalfWidth2 = this.cols.length - fullOffset2 * 2;
    if (firstHalfWidth < 0) {
      firstHalfWidth = 0;
    }
    if (firstHalfWidth2 < 0) {
      firstHalfWidth2 = 0;
    }
    this.cols.eq(firstHalfWidth).addClass('width-half').nextAll().addClass('width-half');
    this.cols.eq(firstHalfWidth2).addClass('width-third').nextAll().addClass('width-third');
  }
}

export default IHKEventOverviewTiles;

$('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', function () {
  $('.topics:not(.initiated), .eventoverview-section:not(.initiated)').each(() => {
    new IHKEventOverviewTiles($(this));
  });
})
