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(() => {});
  });

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

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

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

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

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