(function () {
  'use strict';

  var EMPTY_TEXT = '\u0644\u0627 \u064a\u0648\u062c\u062f \u0628\u064a\u0627\u0646\u0627\u062a';
  var ROW_ATTR = 'data-ua-empty-row';

  function isSuppressed(el) {
    if (!el) return true;
    if (el.hasAttribute('hidden')) return true;
    if (el.style && el.style.display === 'none') return true;
    return false;
  }

  function isLoading(table, tbody) {
    var loadingParent = table && table.closest('[aria-busy="true"], .is-loading, .loading, [data-loading="1"], [data-ua-loading="1"]');
    return !!loadingParent || (tbody && (
      tbody.getAttribute('aria-busy') === 'true' ||
      tbody.getAttribute('data-loading') === '1' ||
      tbody.getAttribute('data-ua-loading') === '1'
    ));
  }

  function isVisible(row) {
    return !isSuppressed(row);
  }

  function colSpanFor(table) {
    var headerCells = table ? table.querySelectorAll('thead th, thead td') : [];
    if (headerCells && headerCells.length) return headerCells.length;
    var firstRowCells = table ? table.querySelectorAll('tr:first-child th, tr:first-child td') : [];
    return firstRowCells && firstRowCells.length ? firstRowCells.length : 1;
  }

  function makeEmptyRow(table) {
    var tr = document.createElement('tr');
    tr.setAttribute(ROW_ATTR, '1');
    tr.className = 'ua-empty-row';

    var td = document.createElement('td');
    td.colSpan = colSpanFor(table);
    td.className = 'text-center text-muted ua-empty-cell';
    td.textContent = EMPTY_TEXT;

    tr.appendChild(td);
    return tr;
  }

  function emptyRowsFor(tbody) {
    return Array.prototype.slice.call(tbody.querySelectorAll('tr[' + ROW_ATTR + '="1"]'));
  }

  function removeEmptyRows(tbody) {
    emptyRowsFor(tbody).forEach(function (row) { row.remove(); });
  }

  function normalizeTable(table) {
    if (!table || table.hasAttribute('data-ua-no-empty-state') || isSuppressed(table)) return;
    var tbody = table.tBodies && table.tBodies.length ? table.tBodies[0] : null;
    if (!tbody) return;

    if (isLoading(table, tbody)) {
      removeEmptyRows(tbody);
      return;
    }

    var rows = Array.prototype.slice.call(tbody.rows || []);
    var realRows = rows.filter(function (row) {
      return row.getAttribute(ROW_ATTR) !== '1' && isVisible(row);
    });

    if (realRows.length === 0) {
      var emptyRows = emptyRowsFor(tbody);
      if (emptyRows.length === 0) {
        tbody.appendChild(makeEmptyRow(table));
        emptyRows = emptyRowsFor(tbody);
      }
      emptyRows.slice(1).forEach(function (row) { row.remove(); });
      emptyRowsFor(tbody).forEach(function (row) {
        var cell = row.cells && row.cells[0];
        if (cell) cell.colSpan = colSpanFor(table);
      });
    } else {
      removeEmptyRows(tbody);
    }
  }

  function normalizeAll() {
    document.querySelectorAll('table').forEach(normalizeTable);
  }

  function installStyles() {
    if (document.getElementById('ua-empty-states-style')) return;
    var style = document.createElement('style');
    style.id = 'ua-empty-states-style';
    style.textContent = [
      '.ua-empty-row .ua-empty-cell{',
      '  padding:24px 12px!important;',
      '  font-weight:700;',
      '  color:rgba(226,232,240,.72)!important;',
      '  background:rgba(15,23,42,.22);',
      '}',
      'body:not(.ua-design-v2) .ua-empty-row .ua-empty-cell{',
      '  color:#6b7280!important;',
      '  background:#f8fafc;',
      '}'
    ].join('');
    document.head.appendChild(style);
  }

  var scheduled = false;
  function schedule() {
    if (scheduled) return;
    scheduled = true;
    window.requestAnimationFrame(function () {
      scheduled = false;
      normalizeAll();
    });
  }

  installStyles();
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', schedule);
  } else {
    schedule();
  }

  new MutationObserver(schedule).observe(document.documentElement, {
    childList: true,
    subtree: true,
    attributes: true,
    attributeFilter: ['style', 'hidden', 'class', 'aria-busy', 'data-loading', 'data-ua-loading']
  });
})();
