import { length } from 'stringz';
import IntlRelativeFormat from 'intl-relativeformat';
import { delegate } from 'rails-ujs';
import emojify from '../mastodon/emoji';
import { getLocale } from '../mastodon/locales';
import loadPolyfills from '../mastodon/load_polyfills';

require.context('../images/', true);

const { localeData } = getLocale();
localeData.forEach(IntlRelativeFormat.__addLocaleData);

function main() {
  const locale = document.documentElement.lang;
  const dateTimeFormat = new Intl.DateTimeFormat(locale, {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
  });
  const relativeFormat = new IntlRelativeFormat(locale);

  document.addEventListener('DOMContentLoaded', () => {
    [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
      content.innerHTML = emojify(content.innerHTML);
    });

    [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
      const datetime = new Date(content.getAttribute('datetime'));
      const formattedDate = dateTimeFormat.format(datetime);
      content.title = formattedDate;
      content.textContent = formattedDate;
    });

    [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
      const datetime = new Date(content.getAttribute('datetime'));
      content.textContent = relativeFormat.format(datetime);;
    });
  });

  delegate(document, '.video-player video', 'click', ({ target }) => {
    if (target.paused) {
      target.play();
    } else {
      target.pause();
    }
  });

  delegate(document, '.media-spoiler', 'click', ({ target }) => {
    target.style.display = 'none';
  });

  delegate(document, '.webapp-btn', 'click', ({ target, button }) => {
    if (button !== 0) {
      return true;
    }
    window.location.href = target.href;
    return false;
  });

  delegate(document, '.status__content__spoiler-link', 'click', ({ target }) => {
    const contentEl = target.parentNode.parentNode.querySelector('.e-content');
    if (contentEl.style.display === 'block') {
      contentEl.style.display = 'none';
      target.parentNode.style.marginBottom = 0;
    } else {
      contentEl.style.display = 'block';
      target.parentNode.style.marginBottom = null;
    }
    return false;
  });

  delegate(document, '.account_display_name', 'input', ({ target }) => {
    const nameCounter = document.querySelector('.name-counter');
    if (nameCounter) {
      nameCounter.textContent = 30 - length(target.value);
    }
  });

  delegate(document, '.account_note', 'input', ({ target }) => {
    const noteCounter = document.querySelector('.note-counter');
    if (noteCounter) {
      noteCounter.textContent = 160 - length(target.value);
    }
  });
}

loadPolyfills().then(main).catch(error => {
  console.log(error); // eslint-disable-line no-console
});

@@ -406,20 +406,22 @@
 unction 
-main
+loaded
 () %7B%0A  c
@@ -696,66 +696,8 @@
 );%0A%0A
-  document.addEventListener('DOMContentLoaded', () =%3E %7B%0A  
   %5B%5D
@@ -764,26 +764,24 @@
 t) =%3E %7B%0A    
-  
 content.inne
@@ -816,18 +816,16 @@
 rHTML);%0A
-  
   %7D);%0A%0A 
@@ -817,34 +817,32 @@
 HTML);%0A  %7D);%0A%0A  
- 
- 
 %5B%5D.forEach.call(
@@ -897,34 +897,32 @@
 ntent) =%3E %7B%0A    
-  
 const datetime =
@@ -966,26 +966,24 @@
 ime'));%0A    
- 
- 
 const format
@@ -1025,26 +1025,24 @@
 etime);%0A    
-  
 content.titl
@@ -1060,26 +1060,24 @@
 edDate;%0A    
- 
- 
 content.text
@@ -1101,18 +1101,16 @@
 edDate;%0A
-  
   %7D);%0A%0A 
@@ -1110,18 +1110,16 @@
  %7D);%0A%0A  
- 
- 
 %5B%5D.forEa
@@ -1189,18 +1189,16 @@
 %3E %7B%0A    
-  
 const da
@@ -1250,26 +1250,24 @@
 ime'));%0A    
- 
- 
 content.text
@@ -1316,19 +1316,187 @@
 ;%0A  
-  %7D);%0A  %7D);
+%7D);%0A%7D%0A%0Afunction main() %7B%0A  if (%5B'interactive', 'complete'%5D.includes(document.readyState)) %7B%0A    loaded();%0A  %7D else %7B%0A    document.addEventListener('DOMContentLoaded', loaded);%0A  %7D
 %0A%0A  

Check ready state of document in public pages (#3652)