//penser à de la récursion pour display none

import {
  CalendarEvents,
  ReaderObject,
} from "./adapters/reader/lisio-reader-adapter";
import { SerializedBlockTree } from "./adapters/reader/structure/block-types";
import LisioBroadcastChannelNames from "./enums/lisio-broadcast-channel-names";
import LisioBroadcastChannelMessagesNames from "./enums/lisio-broadcast-channel-message-names";
import { LisioBroadcastChannelController } from "./controllers/broadcastChannel/lisio-broadcast-channel-controller";
import { renderBlockTree } from "./reader-view/render-block-tree";
import { renderMenuOverlay } from "./reader-view/renderers/menu-overlay";

export const domParser = new DOMParser();
const topSVG = domParser.parseFromString(
  `
<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
  <path d="M14.0987 30.2495C14.0987 31.216 14.9499 31.9995 15.9999 31.9995C17.0498 31.9995 17.901 31.216 17.901 30.2495H14.0987ZM17.3444 4.51209C16.602 3.82866 15.3983 3.82865 14.6558 4.51206L2.55685 15.6489C1.81439 16.3323 1.81438 17.4404 2.55682 18.1238C3.29927 18.8072 4.503 18.8072 5.24546 18.1238L16.0001 8.22438L26.7545 18.124C27.4969 18.8074 28.7007 18.8074 29.4431 18.124C30.1855 17.4406 30.1857 16.3325 29.4431 15.6491L17.3444 4.51209ZM17.901 30.2495L17.9013 5.74953L14.099 5.7495L14.0987 30.2495H17.901Z" fill="white"/>
  <path d="M2 1.99976H30" stroke="white" stroke-width="4" stroke-linecap="round"/>
</svg>
`,
  "image/svg+xml",
);
const upSVG = domParser.parseFromString(
  `
<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
  <path d="M13.8271 30C13.8271 31.1046 14.7999 32 15.9999 32C17.1998 32 18.1726 31.1046 18.1726 30H13.8271ZM17.5365 0.585798C16.688 -0.195257 15.3123 -0.195267 14.4638 0.585774L0.636396 13.3136C-0.212122 14.0946 -0.212133 15.361 0.63637 16.142C1.48488 16.9231 2.86057 16.9231 3.7091 16.142L16.0001 4.82842L28.2909 16.1422C29.1393 16.9233 30.5151 16.9233 31.3636 16.1423C32.212 15.3612 32.2123 14.0949 31.3636 13.3138L17.5365 0.585798ZM18.1726 30L18.1729 2.00002L13.8274 1.99998L13.8271 30H18.1726Z" fill="white"/>
</svg>
`,
  "image/svg+xml",
);
const homeSVG = domParser.parseFromString(
  `
  <svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
    <path d="M20.6673 29.9998V18.2103C20.6673 17.8195 20.5034 17.4446 20.2117 17.1683C19.92 16.8919 19.5243 16.7366 19.1118 16.7366H12.8895C12.477 16.7366 12.0813 16.8919 11.7896 17.1683C11.4979 17.4446 11.334 17.8195 11.334 18.2103V29.9998" stroke="white" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
    <path d="M2 13.7893C1.99989 13.3605 2.09852 12.9369 2.289 12.548C2.47949 12.1591 2.75724 11.8142 3.10289 11.5375L13.9918 2.69684C14.5533 2.24723 15.2648 2.00055 16 2.00055C16.7352 2.00055 17.4467 2.24723 18.0082 2.69684L28.8971 11.5375C29.2428 11.8142 29.5205 12.1591 29.711 12.548C29.9015 12.9369 30.0001 13.3605 30 13.7893V27.0524C30 27.8341 29.6722 28.5838 29.0888 29.1365C28.5053 29.6893 27.714 29.9998 26.8889 29.9998H5.11111C4.28599 29.9998 3.49467 29.6893 2.91122 29.1365C2.32778 28.5838 2 27.8341 2 27.0524V13.7893Z" stroke="white" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
  </svg>
`,
  "image/svg+xml",
);
const history: string[] = [];
// const currentDate = new Date(Date.now());
const loadingImg =
  document.querySelector<HTMLImageElement>("img#lisio-loading");
let wasDisabled = false;
const calendarsLangDatas2 = {
  eventColors: [
    "#FF5733",
    "#FF6F00",
    "#FF8C00",
    "#FFA500",
    "#FFC300",
    "#FFD700",
    "#E6A700",
    "#D4A017",
    "#C19A6B",
    "#A67B5B",
    "#8B4513",
    "#6B4226",
    "#964B00",
    "#734F30",
    "#5A3E26",
    "#4B2C20",
    "#D2691E",
    "#CD853F",
    "#BC8F8F",
    "#DB7093",
    "#FF1493",
    "#FF69B4",
    "#FFB6C1",
    "#DC143C",
    "#B22222",
    "#8B0000",
    "#A52A2A",
    "#800000",
    "#556B2F",
    "#808000",
    "#9ACD32",
  ],
  fr: {
    months: [
      "Janvier",
      "Février",
      "Mars",
      "Avril",
      "Mai",
      "Juin",
      "Juillet",
      "Août",
      "Septembre",
      "Octobre",
      "Novembre",
      "Décembre",
    ],
    days: [
      "Lundi",
      "Mardi",
      "Mercredi",
      "Jeudi",
      "Vendredi",
      "Samedi",
      "Dimanche",
    ],
    longestMonthLength: 9,
    longestDayLength: 8,
  },
  en: {
    months: [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "Septembre",
      "October",
      "November",
      "December",
    ],
    days: [
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
      "Sundaiy",
    ],
    longestMonthLength: 9,
    longestDayLength: 9,
  },
};

let navIntoPDF = "";
let isNavClosing = false;

export const main = document.createElement("main");
document.body.appendChild(main);

function getRemovableMainChildren() {
  return Array.from(main.children).filter(
    (child) =>
      child.localName !== "script" &&
      child.id !== "lisio-label" &&
      child.id !== "lisio-widget" &&
      child.localName !== "style",
  );
}

function debugReadingModeContentAction(
  action: "receive_texts" | "clear_all_texts",
  details: {
    askedBy?: string;
    reason: string;
    messageName: string;
    removableNodes?: number;
    textCount?: number;
  },
) {
  console.debug("[reading-mode]", {
    action,
    askedBy: details.askedBy || "unknown",
    currentUrl: window.location.href,
    messageName: details.messageName,
    reason: details.reason,
    removableNodes: details.removableNodes ?? 0,
    textCount: details.textCount ?? 0,
  });
}

const head1 = document.querySelector("head");
const meta = document.createElement("meta");
meta.setAttribute("http-equiv", "Content-Security-Policy");
meta.setAttribute("content", "img-src * data: blob:");
head1?.append(meta);
const style1 = document.createElement("style");
style1.textContent += `
body{margin:0;padding-top:65px;background-color: #fdf8f5;}
main{display: flex; flex-direction: column; padding: 0px 5%;}
h1, h2, h3, h4, h5, h6{font-size: 32px; font-family : Tahoma;}
p{font-family: Calibri; font-size:18px}
.d-none{display:none;}
a{color: #005282;}
main *:not(a):not(a *){color: #000 !important;}
button, input[type="submit"], input[type="button"]{font-size: 17px; background: #005282; background: linear-gradient(0deg, rgba(0, 82, 130, 1) 0%, rgba(29, 113, 184, 1) 100%); border-radius: 1rem; border:none; color: #fff !important; padding : 16px 8px;}
[data-clickable], input[type="checkbox"], input[type="radio"] {cursor: pointer; text-decoration: underline;}

form {display: flex; flex-direction: column; background-color:#f9f9f9; row-gap: 2rem; padding: 1rem;}
label {display: flex; flex-direction: column; align-items: center; column-gap: 1rem;}
input{border-color: #d4d4d4;}
input[type="text"], input[type="number"], input[type="tel"], input[type="email"], select{border-radius : 1rem;}
input[type="checkbox"]{appearance: none; maring:0; position:relative;-webkit-appearance: none; background: #fff; width: 3rem; aspect-ratio: 1; display: block; border: 1px solid #d4d4d4; border-radius: 8px}
input[type="checkbox"]:checked{background: #1D71B8}
input[type="checkbox"]:checked::after {content: "";position: absolute;left: 0.55rem;top: 0.25rem;width: 0.5rem;height: 1rem;border-right: 3px solid #fff;border-bottom: 3px solid #fff;transform: rotate(45deg);}
input[type="radio"] {appearance: none;-webkit-appearance: none;width: 2rem;height: 2rem;border: 1px solid #d4d4d4;border-radius: 50%;background: #fff;cursor: pointer;position: relative;transition: all 0.2s ease;}
input[type="radio"]:checked {background: #1D71B8; border-color: #1D71B8;}
input[type="radio"]:checked::after {content: "";position: absolute;top: 50%;left: 50%;width: 0.8rem;height: 0.8rem;background: #fff;border-radius: 50%;transform: translate(-50%, -50%);}


img{margin: 8px 0; border: 2px solid #000; border-radius:8px; padding: 4px; max-width: 100%; background-color: #ddd;}
img:hover{background-color: #949494;}
.dropdown{cursor: pointer; max-width: 100%; }
.dropdown *:not(.d-none, summary, p, lisio-dys, lisio-dys-container, a, span){display:flex; flex-direction:column}
table{border-collapse: collapse; maring: 24px 0;}
table thead tr {background-color: #555; color: #fff !important; text-align: center;}
table tbody tr:nth-child(odd) {background-color: #f2f2f2;}
table tbody tr:nth-child(even) {background-color: #fff;}
.lisio-slider{display: flex; column-gap: 16px; row-gap: 16px; flex-wrap: wrap; justify-content: space-evenly}
.lisio-caption-slider{display: flex; flex-direction: column; align-items: center}
.lisio-gallery{margin: 8px 0;}
.lisio-gallery-viewport{display: flex; justify-content: center;}
.lisio-gallery-slide{display: flex; flex-direction: column; align-items: center; max-width: 100%;}
.lisio-gallery-nav{display: flex; align-items: center; justify-content: center; column-gap: 16px; margin-top: 8px;}
.lisio-gallery-prev, .lisio-gallery-next{padding: 8px 20px; font-size: 24px; line-height: 1;}
.lisio-gallery-counter{font-family: Calibri; font-size: 16px;}

.lisio-cards-section{margin: 24px 0;}
.lisio-cards{list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; row-gap: 20px;}
.lisio-cards > li{width: 100%;}
.lisio-card{display: flex; flex-wrap: wrap; column-gap: 20px; row-gap: 12px; background-color: #fff; border: 1px solid #e0d9d2; border-radius: 12px; padding: 16px; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);}
.lisio-card > img:first-child{flex: 1 1 220px; align-self: flex-start; margin: 0; padding: 0; border: none; background-color: transparent; max-width: 280px; width: 100%; height: auto; max-height: 280px; object-fit: cover;}
.lisio-card-content{flex: 3 1 280px; min-width: 240px; display: flex; flex-direction: column; row-gap: 8px;}
.lisio-card-content h3{font-size: 22px; margin: 0;}
.lisio-card-summary{margin: 0; font-family: Calibri; font-size: 16px; color: #333;}
.lisio-card-meta{margin: 0; display: flex; flex-wrap: wrap; column-gap: 12px; font-family: Calibri; font-size: 14px; color: #666;}
.lisio-card-more{margin-top: auto; align-self: flex-start;}
@media (max-width: 560px) {
  .lisio-card{flex-direction: column;}
  .lisio-card > img:first-child{max-width: 100%; max-height: 220px;}
}

font{
  pointer-events: none;
}

details {
  display: flex;
  flex-direction: column;
  margin: 1rem 0;
  // border: 1px solid #ccc;
  border-radius: 8px;
  background-color: #fff;
  width: 90%;
}

details div{
  display: flex;
  flex-direction: column;
  padding-inline: 1rem;
}

summary {
  display:flex;
  font-weight: bold;
  cursor: pointer;
  padding-inline: 1rem;
  border-radius: 8px;
  align-items: center;
  border-radius: 32px;  background: #005282; background: linear-gradient(0deg, rgba(0, 82, 130, 1) 0%, rgba(29, 113, 184, 1) 100%);
  color: #fff !important;
}

summary p {
  display: block;
  margin: 0;
}
    
summary p.content {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 30ch;
}

summary:hover, summary:focus {
  background-color: #f0f8ff;
}

details[open] summary p.content{
  visibility: hidden;
}

summary::marker {
  content: '>';
}

summary p.summary-icon {
  font-size: 32px;
  color: #015e66;
  margin-right: 0.5rem;
}

details[open] div.dropdown-content{
  background-color: #fff;
  border-radius : 8px;
  border : 1px solid #d4d4d4;
  width: 90%;
  justify-self: center;
  margin-top: 8px;
  filter: drop-shadow(5px 5px 5px #000000);
}

details hr{
  width: 100%;
}

div#arrows-container{
  position: fixed;
  right : 0;
  bottom : 10vh;
}

div#arrows-container2{
  position: fixed;
  left : 0;
  top : 0;
}

td{
  vertical-align: middle;
  text-align: center;
}

/*
 * The whole menu (menu-overlay.ts) is plain light-DOM content — not shadow
 * DOM — specifically so its text (button labels, breadcrumb, menu links)
 * stays reachable by Lisio's own font-size/line-height/word-spacing
 * accessibility scaling, which only ever walks document.body's light DOM.
 * An earlier version isolated the header/toggle/close buttons in a shadow
 * root to dodge a growing-button bug; the REAL cause turned out to be
 * narrower (see updateBackButton() in menu-overlay.ts) and fixable
 * without giving up accessibility scaling for any part of the menu.
 *
 * Every tier of the menu (.lisio-menu-list, one per "Voir les sous-menus"
 * level) is built once, up front, when Reading Mode initializes — not one
 * tier at a time as the user drills in. Non-current tiers sit inside
 * .lisio-menu-levels with [hidden] set; see buildLevel()/showLevel() in
 * menu-overlay.ts. .lisio-menu-list's own display:flex is an author rule,
 * so it would otherwise WIN over the [hidden] attribute's default
 * display:none (author styles beat the UA stylesheet regardless of
 * selector specificity) — the dedicated [hidden] override below is what
 * actually makes the non-current tiers disappear.
 */
.lisio-menu-toggle{
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 2147483000;
}

.lisio-menu-overlay{
  position: fixed;
  inset: 0;
  z-index: 2147483001;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  background-color: #fdf8f5;
}

.lisio-menu-overlay[hidden]{
  display: none;
}

.lisio-menu-header{
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  column-gap: 16px;
  padding: 8px 16px;
  background-color: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.lisio-menu-breadcrumb{
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  column-gap: 4px;
  font-family: Calibri;
  font-size: 16px;
  text-align: center;
}

.lisio-menu-breadcrumb-link{
  background: transparent;
  border-radius: 0;
  padding: 0;
  color: #005282;
  text-decoration: underline;
  font-size: 16px;
}

.lisio-menu-breadcrumb-current{
  font-weight: bold;
}

.lisio-menu-levels{
  flex: 1 1 auto;
}

.lisio-menu-list{
  list-style: none;
  margin: 0;
  padding: 16px;
  display: flex;
  flex-direction: column;
  row-gap: 12px;
}

.lisio-menu-list[hidden]{
  display: none;
}

.lisio-menu-item{
  display: flex;
  flex-direction: column;
  row-gap: 8px;
  align-items: flex-start;
  background-color: #fff;
  border-radius: 12px;
  padding: 12px 16px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.lisio-menu-item > a{
  cursor: pointer;
  color: #005282;
  text-decoration: underline;
}

/* Items with no href of their own (a dropdown TOGGLE rendered from a
   source-page <button>, not an <a> — see lisio.fr's "Solutions"/"À propos")
   fall back to a plain <span> in renderItemContent() — style it as a
   section label instead of bare inherited text. */
.lisio-menu-item > span{
  font-weight: bold;
  font-size: 18px;
}
`;
head1?.append(style1);

const broadcastChannel = new LisioBroadcastChannelController(
  LisioBroadcastChannelNames.READER,
);
initChannel();

// if(__BUILD_TARGET__ === "extension"){
//   chrome.runtime.onMessage.addListener((message)=>{
//     if(message.name === "open-channels"){
//       broadcastChannel.broadcastChannel.close();
//       broadcastChannel = new LisioBroadcastChannelController(
//         LisioBroadcastChannelNames.READER,
//       );
//       initChannel();
//     }
//   })
// }

let domain: string;
let sources: Map<string, string>;
let ready = false;
let isMobile = false;
// BCMR_INIT_HTML fires again on every in-reader navigation (see
// `initChannel()` below), not just the very first load — but the menu
// overlay (menu-overlay.ts) is the site's navigation, which is the same on
// every page of a given site. Rebuilding it on every navigation churned the
// whole overlay's DOM (fresh toggle button, fresh list items, ...) for no
// reason, which in turn made Reading Mode's own LisioMutationObserverController
// debounce and rescale the entire document again on every single page change.
// So it's only rebuilt when the payload's domain changes (e.g. following an
// external link to another site) rather than on every navigation.
let menuOverlayDomain: string | undefined;

export const intersectionObserver = new IntersectionObserver(intersectionCallback, {
  root: null,
  rootMargin: "0px 0px -95% 0px",
  threshold: 0.9,
});

const divArrowContainer = document.createElement("div");
divArrowContainer.attachShadow({ mode: "open" });
const shadowArrow = divArrowContainer.shadowRoot;
const shadowStyle = document.createElement("style");
// @media screen and (min-width: 200px) and (max-width: 980px) {
//     div#arrows-container{
//       top: 0;
//       height: 75%;
//       row-gap : 32px;
//     }
// }
shadowStyle.textContent = `
  :root{
    --theme-primary: ${
      __BUILD_TARGET__ === "extension"
        ? "#15548a"
        : //@ts-expect-error lisioConfig are global in page
          lisioConfig.userSettings.colorPrimary
    };
  }

  div#right-controls{
    display : flex;
    flex-direction : column;
    row-gap : 8px;
  }

  button{
    background-color: var(--theme-primary);
    border: none;
    border-radius: 25%;
    padding: 10px;
    cursor: pointer;
  }

  button svg{
    width: 24px;
    height: 24px;
    aspect-ratio: 1;
  }
`;
shadowArrow?.append(shadowStyle);
divArrowContainer.id = "arrows-container";
const rightContainer = document.createElement("div");
rightContainer.id = "right-controls";
shadowArrow?.append(rightContainer);
for (const name of ["top", "up", "down"]) {
  createArrow(name, rightContainer);
}
document.body.append(divArrowContainer);

let shadowArrow2: ShadowRoot | undefined;
if (__BUILD_TARGET__ === "site") {
  const shadowStyle2 = document.createElement("style");
  shadowStyle2.textContent = `
    :root{
      --theme-primary: ${
        //@ts-expect-error lisioConfig are global in page
        lisioConfig.userSettings.colorPrimary
      };
    }
  
    div#top-controls{
      display : flex;
      position : absolute;
      top : 0;
      left: 0;
      column-gap : 8px;
      background-color: #fff;
      width: 100vw;
      padding: 8px;
      box-shadow: 0px 5px 10px -5px #000000;
    }
  
    button svg{
      width: 24px;
      height: 24px;
      aspect-ratio: 1;
    }
  `;
  const divArrowContainer2 = document.createElement("div");
  divArrowContainer2.attachShadow({ mode: "open" });
  shadowArrow2 = divArrowContainer2.shadowRoot as ShadowRoot;
  const topContainer = document.createElement("div");
  topContainer.id = "top-controls";
  shadowArrow2?.append(topContainer);
  shadowArrow2?.append(shadowStyle2);
  divArrowContainer2.id = "arrows-container2";
  for (const name of ["back", "home"]) {
    createArrow(name, topContainer);
  }
  document.body.append(divArrowContainer2);
}

function createArrow(name: string, container: HTMLElement) {
  const button = document.createElement("button");
  if (name === "back") {
    const svg = upSVG.documentElement.cloneNode(true) as SVGElement;
    svg.style.transform = "rotate(-90deg)";
    button.title = "Retourner à la page précédente";
    button.append(svg);
  } else if (name === "top") {
    const svg = topSVG.documentElement.cloneNode(true);
    button.title = "Remonter au début de la page";
    button.append(svg);
  } else if (name === "up") {
    const svg = upSVG.documentElement.cloneNode(true);
    button.title = "Faire défiler vers le haut";
    button.append(svg);
  } else if (name === "down") {
    const svg = upSVG.documentElement.cloneNode(true) as SVGElement;
    svg.style.transform = "rotate(180deg)";
    button.title = "Faire défiler vers le bas";
    button.append(svg);
  } else if (name === "home") {
    const svg = homeSVG.documentElement.cloneNode(true);
    button.title = "Aller à la page d'accueil";
    button.append(svg);
  }
  button.addEventListener("click", () => {
    let scroll;
    if (name === "back") {
      if (history[history.length] === origin) {
        history.pop();
        window?.opener?.history?.go(-1);
      } else {
        broadcastChannel.sendGoBackMessage();
      }
    } else if (name === "top") {
      scroll = 0;
    } else if (name === "up") {
      scroll = window.scrollY - window.innerHeight;
    } else if (name === "down") {
      scroll = window.scrollY + window.innerHeight;
    } else if (name === "home" && window?.opener?.location?.href) {
      window.opener.location.href = domain;
    }
    window.scroll({ left: 0, top: scroll, behavior: "smooth" });
  });
  container.append(button);
}

function initChannel() {

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_INIT_HTML,
    async (datas) => {
      console.debug("init html");
      const value = JSON.parse(datas) as {
        back: string;
        isMobile: boolean;
        texts: ReaderObject[];
        styles: Record<string, string>;
        domain: string;
        base64s: Record<string, string>;
        calendarLang: "fr" | "en";
        structure?: SerializedBlockTree;
        hiddenTexts?: ReaderObject[];
      };
      if (menuOverlayDomain !== value.domain) {
        // Fully rendered up front, before any of the (much heavier) main
        // content work below — table deconstruction, v1/v2 tree rendering,
        // etc. — rather than after it. Only re-rendered when the domain
        // changes — see `menuOverlayDomain`'s declaration above.
        menuOverlayDomain = value.domain;
        try {
          renderMenuOverlay(value.hiddenTexts, broadcastChannel, document.body);
        } catch (error) {
          console.error("[reading-mode] menu overlay render failed, continuing without it", error);
        }
      }

      const askedBy = value.back || value.domain || document.referrer || "";
      debugReadingModeContentAction("receive_texts", {
        askedBy,
        reason: "received new reader content to display",
        messageName: LisioBroadcastChannelMessagesNames.BCMR_INIT_HTML,
        textCount: value.texts.length,
      });
      history.push(value.back);
      isMobile = value.isMobile;
      if (loadingImg != undefined) {
        loadingImg.style = "display: none";
      }
      ready = false;
      sources = new Map(Object.entries(value.base64s));
      window.scroll(0, 0);

      if (domain == undefined) {
        domain = value.domain;
      }
      const removableChildren = getRemovableMainChildren();
      if (removableChildren.length > 0) {
        debugReadingModeContentAction("clear_all_texts", {
          askedBy,
          reason: "replacing previously displayed reader content before rendering the new payload",
          messageName: LisioBroadcastChannelMessagesNames.BCMR_INIT_HTML,
          removableNodes: removableChildren.length,
          textCount: value.texts.length,
        });
      }
      for (const child of removableChildren) {
        child.remove();
      }

      if (
        (value.texts as ReaderObject[]).filter((text) => text.name === "table")
          .length /
          value.texts.length >=
        0.5
      ) {
        for (let i = 0; i < value.texts.length; i++) {
          if (value.texts[i].name === "table") {
            const toAddBody: ReaderObject[] = [];
            deconstructTable(value.texts[i], toAddBody);
            value.texts.splice(i, 1, ...toAddBody);
          }
        }
      }

      let sliderDiv: HTMLDivElement | undefined = undefined;
      let captionSliderDiv: HTMLDivElement | undefined = undefined;
      for (const [className, style] of Object.entries(value.styles)) {
        Array.from(document.styleSheets)
          .find((styleSheet) => styleSheet.href == undefined)
          ?.insertRule(`.${className}{${style}}`);
      }

      function renderFlat(): void {
      // v1: flat rendering, byte-identical to pre-v2 behaviour. Untouched
      // on purpose — this whole branch is never modified by v2 work, so it
      // can never be the thing that drifts between the two renderers.
      let dropdown;
      for (const [i, tag] of value.texts.entries()) {
        if (dropdown == undefined && tag.isHidden) {
          dropdown = createDropdown();
        } else if (!tag.isHidden && dropdown != undefined) {
          const textChild = Array.from(dropdown.children).find(
            (child) => child.textContent?.trim() != "",
          );
          if (textChild == undefined) {
            dropdown.remove();
          } else {
            const summary = dropdown.querySelector<HTMLElement>("summary");
            if (summary != undefined) {
              const sliced = textChild.textContent?.trim().slice(0, 35);
              summary.textContent = `${
                sliced === "" ? "Afficher textes" : sliced
              }...`;
            }
          }
          dropdown = undefined;
        }
        if (tag.name === "img") {
          try {
            const img = createImage(
              tag,
              i === 0 ? undefined : value.texts[i - 1],
              dropdown?.querySelector<HTMLElement>(".dropdown-content") as HTMLElement | undefined,
              !tag.isInSlider,
            );
            if (tag.isInSlider && img != undefined) {
              if (sliderDiv == undefined) {
                sliderDiv = createSliderDiv();
              }
              if (tag.hasCaption) {
                captionSliderDiv = createCaptionSliderDiv();
                captionSliderDiv.append(img);
              } else {
                sliderDiv.append(img);
                sliderDiv.dataset.position = tag.position.toString();
              }
            }
          } catch (error) {
            console.log(error, tag);
            return;
          }
        } else if (tag.name === "video") {
          try {
            createVideo(
              tag,
              i === 0 ? undefined : value.texts[i - 1],
              dropdown?.querySelector<HTMLElement>(".dropdown-content") as HTMLElement | undefined,
            );
          } catch (error) {
            console.log(error, tag);
            return;
          }
        } else if (tag.name === "table") {
          createTable(
            tag,
            tag.isHidden && (dropdown?.querySelector<HTMLElement>(".dropdown-content") as HTMLElement | undefined) != undefined ? (dropdown?.querySelector<HTMLElement>(".dropdown-content") as HTMLElement) : main,
          );
        } else if (tag.name === "form") {
          createForm(
            tag,
            tag.isHidden && (dropdown?.querySelector<HTMLElement>(".dropdown-content") as HTMLElement | undefined) != undefined ? (dropdown?.querySelector<HTMLElement>(".dropdown-content") as HTMLElement) : main,
            value.calendarLang,
          );
        } else if (
          tag.name === "object" ||
          tag.name === "embed" ||
          tag.name === "iframe"
        ) {
          const element = document.createElement(
            tag.name === "iframe" &&
              tag.src != "" &&
              !["youtube", "cdpn", "maps"].some((origin) =>
                new URL(tag.src || "localhost").origin.includes(origin),
              )
              ? "lisio-iframe"
              : tag.name,
          );
          element.dataset.position = tag.position.toString();
          if (
            element instanceof HTMLEmbedElement ||
            element instanceof HTMLIFrameElement
          ) {
            if (element instanceof HTMLIFrameElement) {
              if (
                tag.src != "" &&
                new URL(tag.src || "localhost").origin.includes("youtube")
              ) {
                element.srcdoc = `<style>*{padding:0;margin:0;overflow:hidden}html,body{height:100%}img,span{position:absolute;width:100%;top:0;bottom:0;margin:auto}span{height:1.5em;text-align:center;font:48px/1.5 sans-serif;color:white;text-shadow:0 0 0.5em black}</style><a href="${
                  tag.src
                }"><img src="${
                  import.meta.env.VITE_LISIO_DOMAIN
                }/solution/assets/youtube-placeholder.webp" alt='Cliquez 2 fois sur la vidéo pour lancer la lecture'><span>▶</span></a>`;
              }
              element.setAttribute("loading", tag.loading || "true");
            }
            element.src = tag.src || "";
          } else if (element instanceof HTMLObjectElement) {
            element.data = tag.src || "";
          }
          element.setAttribute("width", tag.width || "-1");
          element.setAttribute("height", tag.height || "-1");
          main.append(element);
        } else if (tag.name === "calendar") {
          createCalendar(
            tag,
            tag.isHidden && dropdown != undefined ? dropdown : main,
            value.calendarLang,
          );
        } else {
          const parsed = domParser.parseFromString(
            tag.content || "",
            "text/html",
          );
          const element = document.createElement(tag.name || "div");
          for (const child of parsed.body.childNodes) {
            if (child.textContent?.trim() != "") {
              element.append(child.cloneNode(true));
            }
          }
          element.className = tag.classes || "";
          element.dataset.position = tag.position.toString();
          if (tag.name === "a") {
            element.dataset.blank = tag.blank;
            element.dataset.href = tag.href;
          }
          if (tag.isHidden) {
            if (sliderDiv) {
              main.append(sliderDiv);
              sliderDiv = undefined;
            }
            const content = dropdown?.querySelector<HTMLElement>(".dropdown-content") as HTMLElement | undefined;
            if(content != undefined){
              if(content.children.length > 0){
                content.append(document.createElement("hr"));
              }
              content.append(element);
            }
          } else if (tag.isInSlider && captionSliderDiv && sliderDiv) {
            captionSliderDiv.append(element);
            sliderDiv.append(captionSliderDiv);
            captionSliderDiv = undefined;
            sliderDiv.dataset.position = tag.position.toString();
          } else {
            if (sliderDiv) {
              main.append(sliderDiv);
              sliderDiv = undefined;
            }
            main.append(element);
          }
          if (tag.clickable != undefined) {
            element.dataset.clickable = tag.clickable.toString();
            element.addEventListener("click", handleOnClick);
          }
          for (const child of element.querySelectorAll<HTMLElement>(
            "[data-lisio-clickable]",
          )) {
            child.dataset.clickable = child.dataset.lisioClickable;
            child.removeAttribute("data-lisio-clickable");
          }
          for (const child of element.querySelectorAll<HTMLElement>(
            "[data-clickable]",
          )) {
            child.addEventListener("click", handleOnClick);
          }
          intersectionObserver.observe(element);
        }
      }

      if (dropdown != undefined) {
        const textChild = Array.from(dropdown.children).find(
          (child) => child.textContent?.trim() != "",
        );
        if (textChild == undefined) {
          dropdown.remove();
        } else {
          const summary = dropdown.querySelector<HTMLElement>("summary");
          if (summary != undefined) {
            const sliced = textChild.textContent?.trim().slice(0, 35);
            summary.textContent = `${
              sliced === "" ? "Afficher textes" : sliced
            }...`;
          }
        }
        dropdown = undefined;
      }
      }

      if (value.structure == undefined || value.structure.blocks.length === 0) {
        renderFlat();
      } else {
        // v2: block-aware rendering. `value.structure` is only present when
        // the source explicitly opted in AND segmentation produced at least
        // one block — see isReaderV2Enabled()/segmentPage(). Rendering is
        // comparatively new, not-yet-field-tested code, so it gets the same
        // safety net segmentPage already has for classification failures:
        // render into a detached scratch container first and only commit it
        // into `main` if it actually produced something. Any throw, or an
        // empty result, falls back to the exact same flat renderer as the
        // branch above — Reading Mode must never show a blank page.
        const scratch = document.createElement("div");
        scratch.style.display = "none";
        document.body.append(scratch);
        let v2Ok = false;
        try {
          renderBlockTree(value.structure, value.texts, scratch, value.calendarLang);
          v2Ok = scratch.childNodes.length > 0;
        } catch (error) {
          console.error(
            "[reading-mode] v2 structure render failed, falling back to flat rendering",
            error,
          );
        }
        if (v2Ok) {
          main.append(...Array.from(scratch.childNodes));
        }
        scratch.remove();
        if (!v2Ok) {
          renderFlat();
        }
      }

      if (__BUILD_TARGET__ === "site") {
        window.addEventListener("visibilitychange", () => {
          if (!isNavClosing && document.visibilityState === "hidden") {
            broadcastChannel.sendNavResponseMessage(wasDisabled);
          }
        });
      }
      ready = true;

      if (__BUILD_TARGET__ === "extension") {
        window.postMessage("end-init-html");
      }
      document
        .querySelector(".lisio-disable")
        ?.addEventListener("click", () => {
          wasDisabled = true;
        });
      setTimeout(() => {
        let positionToScroll = 0;
        const firstH1 = value.texts.find((text) => text.name === "h1");
        if (firstH1 != undefined) {
          positionToScroll = firstH1.position;
        } else {
          const firstH2 = value.texts.find((text) => text.name === "h2");
          if (firstH2 != undefined) {
            positionToScroll = firstH2.position;
          } else {
            const textMore100Chars = value.texts.find(
              (text) =>
                typeof text.content === "string" && text.content.length >= 100,
            );
            if (textMore100Chars != undefined) {
              positionToScroll = textMore100Chars.position;
            }
          }
        }
        if (__BUILD_TARGET__ === "site") {
          const scrollTo = document.querySelector(
            `[data-position="${positionToScroll}"]`,
          );
          if (scrollTo != undefined) {
            const rect = scrollTo.getClientRects()[0];
            window.scrollTo({
              top:
                rect.top -
                (shadowArrow2
                  ?.querySelector<HTMLDivElement>("div")
                  ?.getBoundingClientRect()?.height || 0),
              behavior: "smooth",
            });
          }
        }
      }, 1000);
    },
  );

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  broadcastChannel.attachHandler("clear", async (message: string)=>{
    const mainElement = document.body.querySelector("main");
    if(mainElement != undefined){
      let askedBy = "broadcast-channel";
      let reason = 'received explicit "clear" request';
      try {
        const payload = JSON.parse(message) as {
          askedBy?: string;
          reason?: string;
        };
        askedBy = payload.askedBy || askedBy;
        reason = payload.reason || reason;
      } catch {
        // Keep default debug details when the clear payload is empty or not JSON.
      }
      debugReadingModeContentAction("clear_all_texts", {
        askedBy,
        reason,
        messageName: "clear",
        removableNodes: getRemovableMainChildren().length,
      });
      mainElement.textContent = "";
    }
  });

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_ANYONE_THERE,
    async (message) => {
      const { datas } = JSON.parse(message) as {
        name: string;
        datas: { value: string };
      };
      if (__BUILD_TARGET__ === "extension" ? true : domain === datas.value) {
        broadcastChannel.sendReadyMessage();
      }
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_NEW_IMAGE,
    async (datas) => {
      const data = JSON.parse(datas);
      if (ready) {
        if (data.value.base64 != undefined) {
          sources.set(data.value.src, data.value.base64);
        }
        const img = document.querySelector<HTMLImageElement>(
          `[data-position="${data.value.position}"]`,
        );
        if (img != undefined) {
          if (data.value.src.includes("http")) {
            img.addEventListener("error", loadOriginImage);
          }
          // console.log(img.);
          img.dataset.src = data.value.src;
          img.src = data.value.base64;
          img.width = data.value.width;
          img.height = data.value.height;
        }
      }
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_NEW_NODES,
    async (datas) => {
      const data = JSON.parse(datas);
      if (ready) {
        for (const node of data.value) {
          if (node.alreadyExists) {
            const existingNode = document.querySelector<HTMLElement>(
              `[data-position="${node.position}"]`,
            );
            if (existingNode != undefined) {
              existingNode.innerHTML = node.content;
            }
          } else {
            let refNode;
            for (const a of Array.from(
              document.querySelectorAll<HTMLElement>("[data-position]"),
            ).filter(
              (elt) =>
                elt.dataset.position != undefined &&
                elt.dataset.position >= node.position,
            )) {
              if (a.dataset.position == node.position) {
                refNode = a;
              }
              a.dataset.position = (
                Number.parseInt(a.dataset.position || "-2") + 1
              ).toString();
            }
            const element = document.createElement(node.name);
            element.innerHTML = node.content;
            element.className = node.classes;
            element.dataset.position = node.position;
            element.dataset.clickable = node.clickable;
            intersectionObserver.observe(element);
            if (node.clickable != undefined) {
              element.addEventListener("click", handleOnClick);
            }
            if (refNode != undefined) {
              // refNode may live inside a nested container (dropdown content,
              // block wrapper, …), not necessarily a direct child of `main`.
              refNode.parentNode?.insertBefore(element, refNode);
            }
          }
        }
      }
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_REMOVE_NODES,
    async (datas) => {
      const data = JSON.parse(datas);
      if (ready) {
        for (const node of data.value) {
          if (node.alreadyExists) {
            const existingNode = document.querySelector<HTMLElement>(
              `[data-position="${node.position}"]`,
            );
            if (existingNode != undefined) {
              existingNode.innerHTML = node.content;
            }
          } else {
            const element = document.querySelector(
              `[data-position="${node.position}"]`,
            );
            if (element != undefined) {
              intersectionObserver.unobserve(element);
              element.remove();
              for (const a of Array.from(
                document.querySelectorAll<HTMLElement>("[data-position]"),
              ).filter(
                (elt) =>
                  Number.parseInt(elt.dataset.position || "-1") >=
                  node.position,
              )) {
                a.dataset.position = (
                  Number.parseInt(a.dataset.position || "-1") - 1
                ).toString();
              }
            }
          }
        }
      }
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_MODIFIED_TEXT,
    async (datas) => {
      const data = JSON.parse(datas);
      if (ready) {
        const text = document.querySelector(
          `[data-position="${data.value.position}"]`,
        );
        if (text != undefined) {
          const nodeIterator = document.createNodeIterator(
            text,
            NodeFilter.SHOW_TEXT,
            {
              acceptNode(node) {
                return node.textContent?.trim() === ""
                  ? NodeFilter.FILTER_REJECT
                  : NodeFilter.FILTER_ACCEPT;
              },
            },
          );
          let child;
          for (let iChild = 0; iChild < data.value.childIndex; iChild++) {
            child = nodeIterator.nextNode();
          }
          if (child != undefined) {
            child.textContent = data.value.text;
          }
        }
      }
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_CLOSE,
    async () => {
      if(__BUILD_TARGET__ === "extension"){
        window.close();
      }
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_NAVIGATE,
    async () => {
      ready = true;
      if (navIntoPDF === "") {
        const removableChildren = getRemovableMainChildren();
        if (removableChildren.length > 0) {
          debugReadingModeContentAction("clear_all_texts", {
            askedBy: domain,
            reason: "navigation requested a full reader content reset",
            messageName: LisioBroadcastChannelMessagesNames.BCMR_NAVIGATE,
            removableNodes: removableChildren.length,
          });
        }
        for (const child of removableChildren) {
          child.remove();
        }
        if (loadingImg != undefined) {
          loadingImg.style = "display: block";
        }
      } else {
        navIntoPDF = "";
      }
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_EXIT,
    async () => {
      const response = window.confirm(
        "Vous quittez notre site web, le mode lecture va se fermer. Merci de votre visite.",
      );
      broadcastChannel.sendNavResponseMessage(response);
      if (response) {
        isNavClosing = true;
        history.pop();
        window.close();
      }
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_REFRESH_CALENDAR,
    async (datas) => {
      const data = JSON.parse(datas);
      const calendar = document.querySelector<HTMLElement>(
        `article[data-calendar-index="${data.value.iCalendar}"]`,
      )?.shadowRoot;
      if (calendar != undefined) {
        let i = 1;
        const monthYearElement = calendar.querySelector<HTMLElement>(
          "[data-month][data-year]",
        );
        for (const [day, clickable] of data.value.clickables.entries()) {
          if (day <= i) {
            const date = calendar.querySelector<HTMLElement>(
              `[data-date="${day}-${monthYearElement?.dataset.month}-${monthYearElement?.dataset.year}"]:not([data-clickable])`,
            );
            if (date != undefined && date.dataset.clickable != undefined) {
              date.dataset.clickable = clickable;
              date.addEventListener("click", handleOnClick);
            }
          }
          i++;
        }
      }
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_LOAD_PDF,
    async (datas) => {
      const data = JSON.parse(datas);
      navIntoPDF = data.value;
      const iframe = document.createElement("iframe");
      iframe.src = data.value;
      iframe.style.width = "100%";
      iframe.style.height = "100vh";
      if (loadingImg != undefined) {
        loadingImg.style.display = "none";
      }
      const removableChildren = getRemovableMainChildren();
      if (removableChildren.length > 0) {
        debugReadingModeContentAction("clear_all_texts", {
          askedBy: domain,
          reason: "PDF loading replaces the current reader content",
          messageName: LisioBroadcastChannelMessagesNames.BCMR_LOAD_PDF,
          removableNodes: removableChildren.length,
        });
      }
      for (const child of removableChildren) {
        child.remove();
      }
      main.append(iframe);
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_OPEN_DROPDOWN,
    async (datas) => {
      const data = JSON.parse(datas);
      document
        .querySelector(`[data-position="${data.value}"]`)
        ?.closest("details")
        ?.setAttribute("open", "true");
    },
  );

  broadcastChannel.attachHandler(
    LisioBroadcastChannelMessagesNames.BCMR_OPEN_DROPDOWN,
    async (datas) => {
      const data = JSON.parse(datas);
      document
        .querySelector(`[data-position="${data.value}"]`)
        ?.closest("details")
        ?.removeAttribute("open");
    },
  );
}

export function createDropdown(parent: HTMLElement = main) {
  const details = document.createElement("details");
  details.classList.add("dropdown");
  const summary = document.createElement("summary");
  summary.textContent = "";
  details.append(summary);
  const content = document.createElement("div");
  content.classList.add("dropdown-content");
  details.append(content);
  parent.append(details);
  return details;
}

function deconstructTable(node: ReaderObject, cache: ReaderObject[]) {
  if (
    !["thead", "tr", "tbody", "table", "th", "td"].includes(node.name || "")
  ) {
    cache.push(node);
  } else if (node.childNodes != undefined) {
    for (const child of node.childNodes) {
      deconstructTable(child, cache);
    }
  }
}

export function createSliderDiv() {
  const div = document.createElement("div");
  div.className = "lisio-slider";
  return div;
}

export function createCaptionSliderDiv() {
  const div = document.createElement("div");
  div.className = "lisio-caption-slider";
  return div;
}

function loadOriginImage(event: Event) {
  if (
    event.target instanceof HTMLImageElement &&
    event.target.dataset.src != undefined
  ) {
    event.target.src = event.target.dataset.src;
    event.target.removeEventListener("error", loadOriginImage);
  }
}

export function createImage(
  image: ReaderObject,
  previous: ReaderObject | undefined,
  dropdown: HTMLElement | undefined,
  autoInsert = true,
) {
  const img = document.createElement("img") as HTMLImageElement;
  const source = sources.get(image.src || "");
  if (source != undefined) {
    img.src = source;
  } else {
    img.src = image.src || "";
  }
  img.dataset.position = image.position.toString();
  img.width = Number.parseInt(image.width || "-1");
  img.height = Number.parseInt(image.height || "-1");
  img.style.objectFit = image.objectFit || "normal";
  img.style.objectPosition = image.objectPosition || "normal";
  img.style.cursor = image.cursor || "auto";
  img.style.display = image.display || "block";
  img.dataset.src = image.src;
  img.alt = image.alt || "";
  img.loading = "lazy";
  if (image.clickable != undefined) {
    img.dataset.clickable = image.clickable.toString();
    img.addEventListener("click", handleOnClick);
  }
  if (autoInsert) {
    if (dropdown) {
      if(dropdown.children.length > 0){
        dropdown.append(document.createElement("hr"));
      }
      dropdown.append(img);
    } else {
      if (image.insert === "after" && previous != undefined) {
        document
          .querySelector(`[data-position="${previous.position}"]`)
          ?.after(img);
      } else {
        main.prepend(img);
      }
    }
  } else {
    return img;
  }
}

export function createVideo(
  vid: ReaderObject,
  previous: ReaderObject | undefined,
  dropdown: HTMLElement | undefined,
  autoInsert = true,
) {
  const video = document.createElement("video");
  const source = document.createElement("source");
  video.append(source);
  if (vid.src != undefined) {
    source.src = vid.src;
  } else {
    source.src = "";
  }
  video.dataset.position = vid.position.toString();
  video.width = Number.parseInt(vid.width || "-1");
  video.height = Number.parseInt(vid.height || "-1");
  video.controls = true;
  intersectionObserver.observe(video);
  if (autoInsert) {
    if (dropdown) {
      if(dropdown.children.length > 0){
        dropdown.append(document.createElement("hr"));
      }
      dropdown.append(video);
    } else {
      dropdown = undefined;
      if (previous != undefined && vid.insert === "after") {
        document
          .querySelector<HTMLElement>(`[data-position="${previous.position}"]`)
          ?.after(video);
      } else {
        main.prepend(video);
      }
    }
  } else {
    return video;
  }
}

export function handleOnClick(event: Event) {
  event.preventDefault();
  if (
    event.target instanceof HTMLElement &&
    event.target.localName === "a" &&
    event.target?.dataset.blank === "true"
  ) {
    if (
      !confirm(
        "Un nouvel onglet va s'ouvrir, voulez vous continuer ? La liseuse va se fermer.",
      )
    ) {
      return;
    }
  }
  if (
    isMobile &&
    event.target instanceof HTMLElement &&
    event.target?.dataset.href != undefined
  ) {
    window.opener.location.href = event.target.dataset.href;
  } else if (!isMobile && event.target instanceof HTMLElement) {
    broadcastChannel.sendOnClickMessage({
      target: event.target.dataset.clickable || "-1",
      isShadowRootCalendar:
        event.target.dataset.isShadowRootCalendar === "true",
    });
  }
}

export function handleOnChange(event: Event) {
  event.preventDefault();
  if (
    event.target instanceof HTMLTextAreaElement ||
    event.target instanceof HTMLInputElement ||
    event.target instanceof HTMLSelectElement
  ) {
    broadcastChannel.sendOnChangeMessage({
      target: event.target.dataset.change || "-1",
      value: event.target.value,
    });
  }
}

export function createTable(data: ReaderObject, parent: HTMLElement) {
  if (data.name === "#text") {
    const node = document.createTextNode(data.content || "");
    parent.append(node);
  } else {
    let element =
      data.name === "img"
        ? createImage(data, undefined, undefined, false)
        : document.createElement(data.name || "div");
    if (data.name === "svg") {
      const value = data.value as unknown as {
        hash: string;
        width: string;
        height: string;
      };
      element = document.createElement("img");
      const source = sources.get(value.hash);
      if (source != undefined && element instanceof HTMLImageElement) {
        element.src = source;
        element.setAttribute("width", value.width);
        element.setAttribute("height", value.height);
      }
    }
    if (element != undefined) {
      element.dataset.position = data.position.toString();
      if (data.classes != undefined) {
        element.className = data.classes;
      }
      if (data.childNodes) {
        for (const child of data.childNodes) {
          createTable(child, element);
        }
      }
      if (data.content != undefined) {
        element.innerHTML = data.content;
      }
      if (data.clickable != undefined) {
        element.dataset.clickable = data.clickable.toString();
        element.addEventListener("click", handleOnClick);
      }
      parent.append(element);
    }
  }
}

export function createForm(
  data: ReaderObject,
  parent: HTMLElement,
  calendarLang: "en" | "fr",
  checkboxDiv?: HTMLElement,
) {
  if (data.name === "#text") {
    const node = document.createTextNode(data.content || "");
    parent.append(node);
  } else if (data.name === "calendar") {
    createCalendar(data, parent, calendarLang);
  } else {
    let f = parent.append.bind(parent);
    if (checkboxDiv) {
      f = checkboxDiv.append.bind(checkboxDiv);
      checkboxDiv = undefined;
    }
    const element = document.createElement(data.name || "div");
    element.dataset.position = data.position.toString();
    if (data.id != undefined) {
      element.id = data.id;
    }
    if (data.for) {
      element.setAttribute("for", data.for);
    }
    if (data.type && element instanceof HTMLInputElement) {
      if (data.type === "checkbox") {
        if (parent.localName !== "label") {
          checkboxDiv = document.createElement("div");
          parent.append(checkboxDiv);
          f = checkboxDiv.prepend.bind(checkboxDiv);
        } else {
          f = parent.prepend.bind(parent);
        }
      }
      element.type = data.type;
    }
    if (data.placeholder) {
      element.setAttribute("placeholder", data.placeholder);
    }
    if (
      data.value &&
      (element instanceof HTMLInputElement ||
        element instanceof HTMLSelectElement ||
        element instanceof HTMLTextAreaElement)
    ) {
      element.value = data.value;
    }
    if (data.clickable != undefined) {
      element.dataset.clickable = data.clickable.toString();
      element.addEventListener("click", handleOnClick);
    }
    if (data.change != undefined) {
      element.dataset.change = data.change.toString();
      element.addEventListener("change", handleOnChange);
    }
    if (data.rows != undefined) {
      element.setAttribute("rows", data.rows);
    }
    if (data.cols != undefined) {
      element.setAttribute("cols", data.cols);
    }
    try {
      f(element);
    } catch (error) {
      console.log(element, error, f, parent);
    }
    if (data.childNodes && data.name !== "calendar") {
      for (const child of data.childNodes) {
        createForm(child, element, calendarLang, checkboxDiv);
      }
    }
  }
}

function intersectionCallback(entries: IntersectionObserverEntry[]) {
  for (const entry of entries) {
    if (
      entry.isIntersecting &&
      entry.target instanceof HTMLElement &&
      entry.target.dataset.position != undefined
    ) {
      broadcastChannel.sendScrollToMessage(entry.target.dataset.position);
    }
  }
}

export function createCalendar(
  {
    position,
    iCalendar,
    isShadowRootCalendar,
    days,
    daysNumbers,
    events,
    prevButtonClickable,
    nextButtonClickable,
  }: ReaderObject,
  parent: HTMLElement,
  calendarLang: "en" | "fr",
) {
  const article = document.createElement("article");
  article.attachShadow({ mode: "open" });
  article.dataset.position = position.toString();
  article.dataset.calendarIndex = (iCalendar || -1).toString();
  parent.append(article);
  if (daysNumbers == undefined) {
    daysNumbers = [];
  }
  if (events == undefined) {
    events = [];
  }

  const shadowRoot = article.shadowRoot;

  const calendarStyle = document.createElement("style");
  calendarStyle.textContent = `
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 20px;
    }

    a{color: #2b37e0; text-decoration: underline;}

    #calendar {
      display: flex:
      flex-direct: column;
      margin: 0 auto;
    }
    #calendar-body{
      display: grid;
      grid-template-rows: 1fr;
      grid-template-columns: repeat(7, 1fr);
      gap: 5px;
    }
    #calendar-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
      font-size: 24px;
      font-weight: bold;
    }
    #calendar-header button {
      background-color: #007BFF;
      color: white;
      border: none;
      padding: 5px 10px;
      cursor: pointer;
      border-radius: 5px;
    }
    #calendar-header button:focus {
      outline: 2px solid #0056b3;
    }
    [data-clickable]{
      cursor: pointer;
    }
    .day {
      text-align: center;
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
      background-color: #f9f9f9;
    }
    .day:focus {
      outline: 2px solid #007BFF;
      background-color: #e9ecef;
    }
    #day-header {
      display: grid;
      grid-template-rows: 1fr;
      grid-template-columns: repeat(7, 1fr);
      text-align: center;
    }
    .day-text {
      font-weight: bold;
      text-transform: uppercase;
    }
    .today {
      background-color: #007BFF;
      color: white;
    }
    div.calendar-events{
        border-radius: 8px;
        padding: 8px;
    }
`;
  shadowRoot?.append(calendarStyle);

  const calendarMain = document.createElement("main");
  calendarMain.id = "calendar";
  calendarMain.role = "grid";
  shadowRoot?.append(calendarMain);

  const calendarHeader = document.createElement("header");
  calendarHeader.id = "calendar-header";
  calendarHeader.role = "row";
  calendarMain.append(calendarHeader);

  const prevButton = document.createElement("button");
  prevButton.id = "prevMonth";
  prevButton.ariaLabel = "Mois précédent";
  prevButton.textContent = "◀";
  prevButton.role = "gridcell";
  prevButton.dataset.isShadowRootCalendar = `${isShadowRootCalendar}`;
  if (prevButtonClickable != undefined) {
    prevButton.dataset.clickable = prevButtonClickable;
  }
  calendarHeader.append(prevButton);

  const monthYear = document.createElement("p");
  monthYear.id = "monthYear";
  monthYear.ariaLive = "polite";
  monthYear.role = "gridcell";
  calendarHeader.append(monthYear);

  const nextButton = document.createElement("button");
  nextButton.id = "nextMonth";
  nextButton.ariaLabel = "Mois suivant";
  nextButton.textContent = "▶";
  nextButton.role = "gridcell";
  nextButton.dataset.isShadowRootCalendar = `${isShadowRootCalendar}`;
  if (nextButtonClickable != undefined) {
    nextButton.dataset.clickable = nextButtonClickable;
  }
  calendarHeader.append(nextButton);

  const calendarSection = document.createElement("section");
  // calendarSection = "calendar";
  calendarSection.setAttribute("aria-labelledby", "monthYear");
  calendarSection.role = "rowgroup";
  calendarMain.append(calendarSection);

  const dayHeader = document.createElement("header");
  dayHeader.id = "day-header";
  dayHeader.role = "row";
  calendarSection.append(dayHeader);

  const beginM =
    days == undefined || days.length === 0
      ? true
      : new RegExp(
          `${calendarsLangDatas2[calendarLang].days[0].charAt(0)}`,
          "i",
        ).test(days[0].content || "");

  if (!beginM) {
    const sunday = calendarsLangDatas2[calendarLang].days.pop();
    if (sunday != undefined) {
      calendarsLangDatas2[calendarLang].days.unshift(sunday);
    }
  }
  for (const day of calendarsLangDatas2[calendarLang].days) {
    const dayText = document.createElement("p");
    dayText.classList.add("day-text");
    dayText.textContent = day.slice(0, 3);
    dayText.role = "columnheader";
    dayHeader.append(dayText);
  }

  const calendarBody = document.createElement("article");
  calendarBody.id = "calendar-body";
  calendarBody.role = "rowgroup";
  calendarSection.append(calendarBody);

  const currentDateCalendar = new Date(Date.now());
  currentDateCalendar.setDate(1);

  refreshCalendar(
    currentDateCalendar,
    monthYear,
    calendarBody,
    daysNumbers,
    calendarLang,
    events,
    beginM,
  );

  nextButton.addEventListener("click", (e) => {
    const nextMonth = currentDateCalendar.getMonth() + 1;
    if (nextMonth > 11) {
      currentDateCalendar.setFullYear(currentDateCalendar.getFullYear() + 1);
      currentDateCalendar.setMonth(0);
    } else {
      currentDateCalendar.setMonth(nextMonth);
    }
    refreshCalendar(
      currentDateCalendar,
      monthYear,
      calendarBody,
      daysNumbers,
      calendarLang,
      events,
      beginM,
    );
    handleOnClick(e);
  });

  prevButton.addEventListener("click", (e) => {
    const prevMonth = currentDateCalendar.getMonth() - 1;
    if (prevMonth < 0) {
      currentDateCalendar.setFullYear(currentDateCalendar.getFullYear() - 1);
      currentDateCalendar.setMonth(11);
    } else {
      currentDateCalendar.setMonth(prevMonth);
    }
    refreshCalendar(
      currentDateCalendar,
      monthYear,
      calendarBody,
      daysNumbers,
      calendarLang,
      events,
      beginM,
    );
    handleOnClick(e);
  });
}

function refreshCalendar(
  currentDateC: Date,
  monthYearElement: HTMLElement,
  calendarBody: HTMLElement,
  daysNumbers: ReaderObject[],
  calendarLang: "fr" | "en",
  events: CalendarEvents,
  beginM: boolean,
) {
  calendarBody.innerHTML = "";

  monthYearElement.textContent = `${
    calendarsLangDatas2[calendarLang].months[currentDateC.getMonth()]
  } ${currentDateC.getFullYear()}`;
  monthYearElement.dataset.month = currentDateC.getMonth().toString();
  monthYearElement.dataset.year = currentDateC.getFullYear().toString();

  const firstDay = new Date(
    currentDateC.getFullYear(),
    currentDateC.getMonth(),
    1,
  );
  const lastDay = new Date(
    currentDateC.getFullYear(),
    currentDateC.getMonth() + 1,
    0,
  );

  const firstDayIndex = (firstDay.getDay() + 6) % 7;
  const daysInMonth = lastDay.getDate();
  let iOffset = 0;
  const dateOffset = new Date(
    currentDateC.getFullYear(),
    currentDateC.getMonth(),
    1,
    12,
  );
  dateOffset.setDate(dateOffset.getDate() - firstDayIndex - 1);

  let iRow = 0;
  let iColumn = 0;

  for (iOffset; iOffset < firstDayIndex + (beginM ? 0 : 1); iOffset++) {
    const emptyDay = document.createElement("div");
    emptyDay.dataset.date = `${dateOffset.getDate()}-${dateOffset.getMonth()}-${dateOffset.getFullYear()}`;
    dateOffset.setDate(dateOffset.getDate() + 1);
    emptyDay.style.gridRow = "1";
    emptyDay.style.gridColumn = (iColumn + 1).toString();
    iColumn++;
    iRow++;
    calendarBody.appendChild(emptyDay);
  }

  const today = new Date();
  for (let day = 0; day < daysInMonth; day++) {
    const dayElement = document.createElement("p");
    dayElement.style.gridRow = (Math.floor(iRow / 7) * 2 + 1).toString();
    dayElement.style.gridColumn = ((iColumn % 7) + 1).toString();
    iColumn++;
    iRow++;
    dayElement.className = "day";
    dayElement.tabIndex = 0;
    dayElement.role = "gridcell";
    dayElement.textContent = (day + 1).toString();
    dayElement.dataset.date = `${
      day + 1
    }-${currentDateC.getMonth()}-${currentDateC.getFullYear()}`;
    const c = daysNumbers[iOffset + day];
    if (c != undefined && c.clickable != undefined) {
      dayElement.dataset.clickable = c.clickable.toString();
      dayElement.addEventListener("click", handleOnClick);
    }

    if (
      day + 1 === today.getDate() &&
      currentDateC.getMonth() === today.getMonth() &&
      currentDateC.getFullYear() === today.getFullYear()
    ) {
      dayElement.classList.add("today");
    }

    calendarBody.appendChild(dayElement);
  }

  for (const [year, eventsThisYear] of Object.entries(events)) {
    if (
      Number.parseInt(year) === currentDateC.getFullYear() ||
      Number.parseInt(year) === currentDateC.getFullYear() - 1 ||
      Number.parseInt(year) === currentDateC.getFullYear() + 1
    ) {
      for (const [month, eventsThisMonth] of Object.entries(eventsThisYear)) {
        if (
          Number.parseInt(month) === currentDateC.getMonth() ||
          Number.parseInt(month) === currentDateC.getMonth() - 1 ||
          Number.parseInt(month) === currentDateC.getMonth() + 1
        ) {
          for (const [day, eventsThisDay] of Object.entries(eventsThisMonth)) {
            const startingDate = calendarBody.querySelector(
              `[data-date="${day}-${month}-${year}"]`,
            );
            if (
              startingDate != undefined &&
              startingDate instanceof HTMLElement
            ) {
              for (const [duration, events] of Object.entries(eventsThisDay)) {
                const div = document.createElement("div");
                div.style.gridRow = (
                  Number.parseInt(startingDate.style.gridRow) + 1
                ).toString();
                const starting = Number.parseInt(startingDate.style.gridColumn);
                div.style.gridColumn = `${starting}/${
                  starting + Number.parseInt(duration)
                }`;
                div.classList.add("calendar-events");
                for (const event of events) {
                  const element = document.createElement(event.name || "div");
                  element.textContent = event.content || "";
                  element.className = event.classes || "";
                  element.dataset.position = event.position.toString();
                  if (event.clickable != undefined) {
                    element.dataset.clickable = event.clickable.toString();
                    element.addEventListener("click", handleOnClick);
                  }
                  div.style.border = `4px solid ${
                    calendarsLangDatas2.eventColors[event.numberEvent || 0]
                  }`;
                  div.appendChild(element);
                }
                startingDate.after(div);
              }
            }
          }
        }
      }
    }
  }
}

broadcastChannel.sendReadyMessage();
