const componentName = 'customtzcld'; //TODO: Get it from the packages.json

export const TZCLD_URLS = {
  my_territories: 'tzcld-mes-territoires',
  my_territory_space: 'tzcld-mon-territoire',
  my_territory_infos: 'tzcld-presentation-territoire',
  my_territory_auto_eval: 'tzcld-auto-evaluation',
  my_territory_follow: 'tzcld-suivi',
  my_territory_follow_requests_all: 'tzcld-suivi-requests',
  my_territory_follow_request_add: 'tzcld-suivi-request-add',
  my_territory_follow_request_edit: 'tzcld-suivi-request-edit',
  my_territory_follow_request_space: 'tzcld-suivi-request-space',
  my_territory_follow: 'tzcld-suivi',
  my_territory_exchanges: 'tzcld-echnages',
  my_territory_all_exchanges: 'tzcld-tous-les-echnages',
  my_territory_add_exchange: 'tzcld-add-echnage',
  my_territory_exchange_space: 'tzcld-echnage-space',
  my_territory_exchange: 'tzcld-echnage',
  my_territory_edit_exchange: 'tzcld-edit-echnage',
  my_territory_ref_all_exchanges: 'tzcld-tous-les-echnages-ref',
  my_territory_ref_add_exchange: 'tzcld-add-echnage-ref',
  my_territory_ref_exchange_space: 'tzcld-echnage-space-ref',
  my_territory_ref_exchange: 'tzcld-echnage-ref',
  my_territory_ref_edit_exchange: 'tzcld-edit-echnage-ref',
  my_territory_shared_files_all: 'tzcld-tous-les-fichiers',
  my_territory_shared_file_add: 'tzcld-add-fichier',
  my_territory_shared_files_space: 'tzcld-fichier-space',
  my_territory_shared_files_edit: 'tzcld-edit-fichier',
  my_territory_grappe_shared_files_all: 'tzcld-tous-les-fichiers-grappe',
  my_territory_grappe_shared_file_add: 'tzcld-add-fichier-grappe',
  my_territory_grappe_shared_files_space: 'tzcld-fichier-space-grappe',
  my_territory_grappe_shared_files_edit: 'tzcld-edit-fichier-grappe',
}

export function path() {
  let path = ".";
  if(window.orbit) {
    let component = window.orbit.npm.find(e=>e.package==`@startinblox/custom-tzcld`);
    path = `https://cdn.jsdelivr.net/npm/${component.package}@${component.version}/dist`;
    if(component.path) {
      path = component.path.replace('/index.js', '')
    }
  }
  return path;
}
export function community_uniq() {
  let uniq = "";
  if(window.orbit) {
    let component = window.orbit.components.find(e=>e.module==`djangoldp_community`);
    //console.log(component) ;
    if(component.uniq) {
      uniq = component.uniq
    }
  }
  return uniq;
}

export function coreVersion() {
  let coreUrl = "https://cdn.jsdelivr.net/npm/@startinblox/core";
  if(window.orbit) {
    let core = window.orbit.npm.find(e=>e.package=='@startinblox/core');
    coreUrl += `@${core.version}/+esm`;
    if(core.path) {
      coreUrl = core.path;
    }
  }
  return coreUrl;
}

export function setupOrbit(component) {
  if(window.orbit) {
    component.localize = (key) => {
      return window.orbit.intl.t(`${componentName}.${key}`) || component.strings[key] || key;
    }
    component.getRoute = window.orbit.getRoute;
  } else {
    component.getRoute = () => componentName;
  }
}


export function uniq() {
  return Math.random().toString(16).slice(2);
}

export function findAncestor (el, cls) {
    while (el = el.parentElement)  {

      if (el.classList.contains(cls)) {
         return el;
      }
    }  
}

export function getIntId(url) {
  let parts = url.split('/');
  return parts.at(-2);
}

export function getFileExt(url) {
  let part = url.substring(url.lastIndexOf('.') + 1); 
  return part ;
}


export function getSVGFolder(url) {
  let part = `<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  <g clip-path="url(#clip0_336_36945)">
  <path fill-rule="evenodd" clip-rule="evenodd" d="M22.5037 4.875H11.2537L9.00374 2.625H1.50374C0.675363 2.625 0.00373755 3.29662 0.00373755 4.125V7.875H-0.00338745V9.375H0.00373755V19.875C0.00373755 20.7034 0.675363 21.375 1.50374 21.375H11V19.875H1.50336V9.375H22.5034V14H24.0037V6.375C24.0037 5.54662 23.3321 4.875 22.5037 4.875ZM8.31784 4.125H1.50339L1.50374 7.875H22.5037V6.375H10.632L10.1606 5.9355L8.31784 4.125Z" fill="#1A2C5F"/>
  <path d="M16.8731 21.25H13V20.6823L15.729 16.9511H13.0741V16.25H16.7991V16.8246L14.059 20.5489H16.8731V21.25Z" fill="#1A2C5F"/>
  <path d="M17.9649 21.25V16.25H18.8499V21.25H17.9649Z" fill="#1A2C5F"/>
  <path d="M24 17.7616C24 18.2837 23.8149 18.6839 23.4446 18.962C23.0743 19.2402 22.5473 19.3793 21.8635 19.3793H21.3007V21.25H20.4157V16.25H21.9746C22.651 16.25 23.157 16.3777 23.4927 16.633C23.8309 16.8884 24 17.2646 24 17.7616ZM21.3007 18.6919H21.7709C22.2251 18.6919 22.5584 18.6189 22.7707 18.473C22.983 18.3271 23.0891 18.0991 23.0891 17.789C23.0891 17.5017 22.9941 17.2874 22.804 17.146C22.6139 17.0047 22.3177 16.934 21.9153 16.934H21.3007V18.6919Z" fill="#1A2C5F"/>
  </g>
  <defs>
  <clipPath id="clip0_336_36945">
  <rect width="24" height="24" fill="white"/>
  </clipPath>
  </defs>
  </svg>`; 
  return part ;
}

export function getSVGFile(url) {
  let part = `<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  <path d="M13.125 0H4.875C4.04662 0 3.375 0.671625 3.375 1.5V22.5C3.375 23.3284 4.04662 24 4.875 24H19.125C19.9534 24 20.625 23.3284 20.625 22.5V7.50037L13.125 0ZM19.125 8.12175V8.25H12.375V1.5H12.504L19.125 8.12175ZM4.875 22.5V1.5H10.875V9.75H19.125V22.5H4.875Z" fill="#1A2C5F"/>
  </svg>`; 
  return part ;
}





export function live (eventType, elementQuerySelector, cb) {
  document.addEventListener(eventType, function (event) {

      var qs = document.querySelectorAll(elementQuerySelector);

      if (qs) {
          var el = event.target, index = -1;
          while (el && ((index = Array.prototype.indexOf.call(qs, el)) === -1)) {
              el = el.parentElement;
          }

          if (index > -1) {
              cb.call(el, event);
          }
      }
  });
}