export const POST_EVENT_SEPARATOR = '=>';

export const WEBVIEW_EVENTS = {
  IMG_CLICK: 'IMG_CLICK',
  TABLE_RENDER: 'TABLE_RENDER',
};

export const POST_MESSAGE_SCRIPT = `
(function () {
  // Get table height
  setTimeout(function(){
    const tables = document.getElementsByTagName("table")
  
    if(tables && tables[0]) {
      window.ReactNativeWebView.postMessage('${WEBVIEW_EVENTS.TABLE_RENDER}${POST_EVENT_SEPARATOR}' + tables[0].scrollHeight);
    }
  }, 500);

  // Set onClick event in img
  const imgElements = document.getElementsByTagName("img");
  
  for (let index = 0; index < imgElements.length; index++) {
    const element = imgElements[index];
    if(element.parentElement.tagName !== "A") {
      const src = element.getAttribute("src");
      element.setAttribute("onclick","window.ReactNativeWebView.postMessage('${WEBVIEW_EVENTS.IMG_CLICK}${POST_EVENT_SEPARATOR}"+src+"')");
    }
  }
})();
`;
