import sleep from 'sleep-promise';
import { onMarketingPage } from './service';
import { onChangeTab, onLocationChange } from '@content/service/util';

export function main(ctx) {
  ctx.addEventListener(window, 'scroll', () => {
    onMarketingPage().catch(() => {});
  });

  document.addEventListener('readystatechange', () => {
    onMarketingPage().catch(() => {});
  });

  onMarketingPage().catch(() => {});

  sleep(2000).then(() => {
    if (!ctx.isInvalid) {
      onMarketingPage().catch(() => {});
    }
  });

  onChangeTab(() => {
    sleep(1000).then(() => {
      if (!ctx.isInvalid) {
        onMarketingPage();
      }
    });
  });

  onLocationChange(() => {
    sleep(500).then(() => {
      if (!ctx.isInvalid) {
        onMarketingPage().catch(() => {});
      }
    });
  });
}
