/* Minification failed. Returning unminified contents.
(8,96-97): run-time error JS1195: Expected expression: >
(10,26-27): run-time error JS1014: Invalid character: `
(10,27-28): run-time error JS1195: Expected expression: .
(10,38-39): run-time error JS1004: Expected ';': {
(10,52-53): run-time error JS1004: Expected ';': {
(10,62-63): run-time error JS1195: Expected expression: :
(10,67-68): run-time error JS1014: Invalid character: `
(10,86-87): run-time error JS1195: Expected expression: >
(21,9-10): run-time error JS1002: Syntax error: }
(24,29-30): run-time error JS1195: Expected expression: )
(24,31-32): run-time error JS1004: Expected ';': {
(36,2-3): run-time error JS1195: Expected expression: )
(41,28-29): run-time error JS1004: Expected ';': {
(42,3-34): run-time error JS1018: 'return' statement outside of function: return $('.table-widget table')
 */
(function ($) {
    var containerSelector = '.columns .column.is-half .container';
    var $nonNestedContainers = $(containerSelector).not(containerSelector + ' .container');
    $nonNestedContainers.removeClass('container');
})(jQuery);
// See SH-63 for more info.
$(function () {
    ['.has-inline-media--2', '.has-inline-media--3', '.has-inline-media--4'].forEach(selector => {
        // Every widget has it's own container, but we want to get them next to each other in a separate wrapper.
        const $lasts = $(`.container${selector} ~ ${selector}:last`).each((i, node) => {
            // Get every last widget of same size
            const $last = $(node)
            // Then find it's preceding siblings
            const $siblings = $last.siblings(selector)
            // Add them together
            const $containers = $last.add($siblings)
            // Extract all their children and add them to the last widget, wrapped in a new div
            $containers.children().appendTo($last).wrapAll('<div class="inline-media-container" />')
            // Remove the now empty widget containers of which their children were moved.
            $siblings.remove()
        })
    })
});;
$(document).ready(function () {
  getWidgetTables().each(function () {
    var $table = $(this);
    var tableHeadings = getTableHeadingTexts($table.find('thead tr:last'));
    var $thead = $table.find('thead');
    var $fakeHead = getFakeTableHead($thead);

    var $fakeBody = getFakeBody($table.find('tbody'), tableHeadings);

    var $fakeTable = getFakeTable($fakeHead, $fakeBody);
    $table.replaceWith($fakeTable);
  });
});

/**
 * Returns all tables generated from the 'table' widget.
 */
function getWidgetTables() {
  return $('.table-widget table');
}

/**
 * Returns a fake table head element from the given real table head element.
 * @param {*} $thead A table head jQuery object.
 */
function getFakeTableHead($thead) {
  var $fakeHead = $('<div class="thead"></div>');

  // Add fake rows.
  $thead.find('tr').each(function (i) {
    var $tr = $(this);
    var $fakeRow = $('<div class="tr"></div>');
    $fakeHead.append($fakeRow);

    // Add fake cells to row.
    $tr.find('th').each(function (i, th) {
      var width = th.style.width;
      var $fakeHeadCell = $('<div class="th"></div>').css('min-width', width || 0);

      $fakeHeadCell.append(
        // Table headings must be anchors.
        $('<a>' + $(this).html() + '</a>')
      );

      $fakeRow.append($fakeHeadCell);
    });
  })

  return $fakeHead;
}

/**
 * Returns all of the heading texts from the given table.
 * @param {*} $table A table jQuery object.
 */
function getTableHeadingTexts($tr) {
  var $headingElements = $tr.find('th');
  return $.map($headingElements, function (th) {
    return { text: th.innerText, width: th.style.width };
  });
}

/**
 * Returns a fake table body from the given real table body, using the given table heading texts.
 * @param {*} $tableBody A table body jQuery element.
 * @param {*} tableHeadingTexts An array that contains all of the heading texts of the containing table.
 */
function getFakeBody($tableBody, tableHeadingTexts) {
  var $fakeBody = $('<div class="tbody"></div>');

  $tableBody.find('tr').each(function () {
    var $fakeRow = getFakeRow($(this), tableHeadingTexts);
    $fakeBody.append($fakeRow);
  });

  return $fakeBody;
}

/**
 * Returns a fake row from the given real row, using the given table heading texts.
 * @param {*} $row A row jQuery object.
 * @param {*} tableHeadingTexts An array that contains all of the headings texts of the containing table.
 */
function getFakeRow($row, tableHeadingTexts) {
  var $fakeRow = $('<div class="tr"></div>');

  // Add fake cells to row.
  $row.find('td').each(function (i) {
    var $fakeHeadCell = $('<div class="td"></div>');
    var header = tableHeadingTexts[i] ? tableHeadingTexts[i].text : '';
    var width = tableHeadingTexts[i] ? tableHeadingTexts[i].width : 0;
    var innerCellHtml = $(this).html();

    $fakeHeadCell.attr('data-header', header);

    $fakeHeadCell
      .html(innerCellHtml)
      .css('min-width', width || 0)
      .toggleClass('is-empty', /^\s*(&nbsp;)*\s*$/.test(innerCellHtml))
      .toggleClass('no-header', /^\s*$/.test(header));

    $fakeRow.append($fakeHeadCell);
  });

  // Add empty cell because the last cell does not get a label.
  $fakeRow.append($('<div></div>'));

  return $fakeRow;
}

/**
 * Returns a fake table object that contains the given head and body objects.
 * @param {*} $head A table head jQuery object.
 * @param {*} $body A table body jQuery object.
 */
function getFakeTable($head, $body) {
  var $fakeTable = $('<div class="table"></div>');
  $fakeTable.append($head);
  $fakeTable.append($body);

  
  if ($head.find('.tr').length > 1) {
    $fakeTable.attr('data-header', $head.find('.tr:first').text().trim());
  }

  return $fakeTable;
}
;
