// Util functions for injected code

function getDetails() {
  if (window.location.href.includes("www.linkedin.com/sales/lead/")) {
    return getProfileDetails()
  }
  else if (window.location.href.includes("www.linkedin.com/sales/company/")) {
    return getCompanyDetails()
  }
  else {
    return new Promise((resolve, reject) => resolve({ type: "", url: window.location.href }));
  }
}
function getCompanyDetails(tryNum = 0) {
  const checkLoadedElement = document.querySelector("[data-anonymize='company-name']")
  if (!checkLoadedElement || checkLoadedElement.innerText === "" || !document.querySelector(".employee-count dt")) {
    if (tryNum < 4) {
      console.log("waiting...")
      return new Promise((resolve, reject) => {
        setTimeout(async () => {
          resolve(await getCompanyDetails(tryNum + 1))
        }, 500);
      })

    }
  }
  let companyLISN = window.location.href
  if (companyLISN) {
    const spliceIndex = companyLISN.indexOf('?');
    if (spliceIndex !== -1)
      companyLISN = companyLISN.slice(0, spliceIndex);
  }
  let details = {
    imgSrc: document.querySelector("[data-anonymize='company-logo']").src,
    companyName: document.querySelector("[data-anonymize='company-name']").innerText,
    industry: document.querySelector("[data-anonymize='industry']").innerText,
    location: document.querySelector("[data-anonymize='location']").innerText,
    lisn: companyLISN,
    revenue: 0,
    website: "",
    numEmployees: 0
  }
  const websiteElement = document.querySelector("[data-control-name='visit_company_website']");
  if (websiteElement)
    details.website = websiteElement.href;

  const employeeElement = document.querySelector(".employee-count dt")
  if (employeeElement)
    details.numEmployees = employeeElement.innerText;
  if (details.imgSrc.includes("data:image/gif;base64"))
    details.imgSrc = "";
  return new Promise((resolve, reject) => resolve({ type: "company", details, url: window.location.href }));
}
function getProfileDetails(tryNum = 0) {
  const checkLoadedElement = document.querySelector("[data-anonymize='person-name']")
  if (!checkLoadedElement || checkLoadedElement.innerText === "" ||
    (!document.querySelector("[data-anonymize='company-name']").getAttribute("href") && document.querySelector("[data-anonymize='person-name']").innerText !== "LinkedIn Member")) {
    console.log("waiting...")
    if (tryNum < 4)
      return new Promise((resolve, reject) => {
        setTimeout(async () => {
          resolve(await getProfileDetails(tryNum + 1))
        }, 500);
      })
  }
  let companyLISN = document.querySelector("[data-anonymize='company-name']").getAttribute("href")
  if (companyLISN) {
    const spliceIndex = companyLISN.indexOf('?');
    if (spliceIndex !== -1)
      companyLISN = companyLISN.slice(0, spliceIndex);
    companyLISN = location.protocol + "//" + location.hostname + companyLISN;
  }
  const pathname = window.location.pathname;
  const id = pathname.substring(pathname.lastIndexOf('/') + 1, pathname.indexOf(','));
  const name = document.querySelector("[data-anonymize='person-name']").innerText;
  const [firstName, ...lastArr] = name.split(' ');
  const lastName = lastArr.join(' ');
  let details = {
    firstName,
    lastName,
    //headline: document.querySelector("[data-anonymize='headline']").innerText,
    jobTitle: document.querySelector("[data-anonymize='job-title']").innerText,
    companyName: document.querySelector("[data-anonymize='company-name']").innerText,
    imgSrc: document.querySelectorAll("[data-anonymize='headshot-photo']")[1].getAttribute('src'),
    linkedInSalesNav: window.location.href,
    id,
    companyLISN,
    email: "",
    phone: ""
  }
  if (details.imgSrc.includes("data:image/gif;base64"))
    details.imgSrc = "";
  document.querySelectorAll("div.profile-topcard__contact-info.mt5 dd a").forEach((link) => {
    const contactType = link.getAttribute("data-anonymize")
    let dataType = null
    if (contactType !== "remove") {
      dataType = contactType
    }
    // Insert code to get addition contact info here
    if (dataType) {
      details[dataType] = link.innerText.split("\n")[1]
    }
  })

  return new Promise((resolve, reject) => resolve({ type: "profile", details, url: window.location.href }));
}

function toggleFrame(evt = null) {
  isOpen = !isOpen
  /*
  if (isOpen && window.location.href.includes("www.linkedin.com/sales/people/"))
    sendDetails();
    */
  if (evt)
    evt.preventDefault()
  i.classList.toggle('fa-closed');
  d.classList.toggle('fa-closed');
}

function setIFrameSrc(src) {

  const i = document.getElementsByClassName('fa-content')[0]
  i.src = src;

}
function setWindowURL(url) {
  window.location.href = url;
}