import { g as getContext, f as store_get, h as ensure_array_like, e as escape_html, b as attr, l as attr_class, j as stringify, u as unsubscribe_stores, p as pop, a as push } from "../../../../../chunks/index.js";
import { u as updatePv, p as productionAnnuelle } from "../../../../../chunks/calcul.js";
import { d as derived } from "../../../../../chunks/index4.js";
function Synthese($$payload, $$props) {
  push();
  var $$store_subs;
  const _simulation = getContext("simulation");
  let communaute = getContext("communaute");
  const soleil = getContext("soleil");
  const pvgis = getContext("pvgis");
  let maxProd = 0;
  soleil.subscribe((value) => {
    console.log("Soleil", value);
    for (let v of value.weekly.y) {
      maxProd = Math.max(maxProd, v);
    }
  });
  _simulation.p.puissance.subscribe((value) => {
    console.log("Puissance change");
    updatePv(value, store_get($$store_subs ??= {}, "$communaute", communaute), pvgis, soleil);
  });
  const fmt2 = Intl.NumberFormat("fr-FR", { maximumFractionDigits: 2 });
  const fmt0 = Intl.NumberFormat("fr-FR", { maximumFractionDigits: 0 });
  const tarifLocal = _simulation.tarifLocal;
  const chiffreAffaires = _simulation.chiffreAffaires;
  const N = _simulation.N;
  const investissement = _simulation.investissement;
  const charges = _simulation.charges;
  const interetsDetteSenior = _simulation.interetsDetteSenior;
  const banque = _simulation.banque.resultat;
  const resultatNet = _simulation.resultatNet;
  const _coutElec = _simulation.p.cout_elec;
  const inflationElec = _simulation.p.inflation_elec;
  const autoprod = _simulation.p.autoprod;
  const productible = _simulation.p.productible;
  const puissance = _simulation.p.puissance;
  const degradation = _simulation.p.degradation;
  const _consoMoyenneFoyer = _simulation.p.conso_moyenne_foyer;
  const arr = 0;
  const consoMoyenneFoyer = (i) => Math.min(store_get($$store_subs ??= {}, "$_consoMoyenneFoyer", _consoMoyenneFoyer), productionAnnuelle(store_get($$store_subs ??= {}, "$productible", productible), store_get($$store_subs ??= {}, "$puissance", puissance), store_get($$store_subs ??= {}, "$degradation", degradation), i) / store_get($$store_subs ??= {}, "$autoprod", autoprod));
  const coutElec = (() => {
    let cout = 0;
    for (let i = 0; i < store_get($$store_subs ??= {}, "$N", N); i++) {
      cout += store_get($$store_subs ??= {}, "$_coutElec", _coutElec) * Math.pow(1 + store_get($$store_subs ??= {}, "$inflationElec", inflationElec) / 100, i) * consoMoyenneFoyer(i);
    }
    return cout;
  })();
  const coutElecAuto = (() => {
    let cout = 0;
    for (let i = 0; i < store_get($$store_subs ??= {}, "$N", N); i++) {
      cout += store_get($$store_subs ??= {}, "$tarifLocal", tarifLocal)[i] * consoMoyenneFoyer(i) * store_get($$store_subs ??= {}, "$autoprod", autoprod) / 100 + store_get($$store_subs ??= {}, "$_coutElec", _coutElec) * Math.pow(1 + store_get($$store_subs ??= {}, "$inflationElec", inflationElec) / 100, i) * consoMoyenneFoyer(i) * (100 - store_get($$store_subs ??= {}, "$autoprod", autoprod)) / 100;
    }
    return cout;
  })();
  function sum_(x, n) {
    let sum = 0;
    for (let i = 0; i < n; i++) {
      sum += x[i];
    }
    return sum;
  }
  function format(x) {
    let f = 0;
    let s = "";
    if (Math.abs(x) <= 1e3) {
      s = " €";
      f = x;
    } else if (Math.abs(x) < 1e6) {
      s = " k€";
      f = x / 1e3;
    } else if (Math.abs(x) < 1e9) {
      s = " M€";
      f = x / 1e6;
    } else {
      s = " mds€";
      f = x / 1e9;
    }
    return fmt2.format(f) + s;
  }
  function rentabiliteTaux(resultat) {
    return fmt0.format(store_get($$store_subs ??= {}, "$investissement", investissement) ? resultat * 100 / store_get($$store_subs ??= {}, "$investissement", investissement) - 100 : 0) + "%";
  }
  function traceArray(f, n) {
    let maxa = 0;
    let mina = 0;
    let margin = 1;
    let c = "";
    let ai = 0;
    maxa = -1 / 0;
    mina = 1 / 0;
    for (let i = 0; i < n; i++) {
      ai += f[i];
      maxa = Math.max(maxa, ai);
      mina = Math.min(mina, ai);
      c += ` ${c ? "L" : "M"} ${i.toFixed(arr)}, ${-ai.toFixed(arr)}`;
    }
    if (maxa == -1 / 0) {
      maxa = 0;
      mina = 0;
    }
    margin = Math.max(1, (maxa - mina) / 20);
    return { maxa, mina, margin, c };
  }
  let tr = derived([resultatNet, N], ([$resultatNet2, $N2]) => {
    console.log("TRACE");
    return traceArray($resultatNet2, $N2);
  });
  const roiDate = (() => {
    let sum = 0;
    let lastNeg = 0;
    for (let i = 0; i < store_get($$store_subs ??= {}, "$N", N); i += 1) {
      sum += store_get($$store_subs ??= {}, "$resultatNet", resultatNet)[i];
      if (sum < 0) lastNeg = i;
    }
    lastNeg += 1;
    if (lastNeg < store_get($$store_subs ??= {}, "$N", N)) {
      if (lastNeg <= 1 && sum >= 0) {
        return `Tout de suite`;
      } else if (lastNeg == 1) {
        return `1 an`;
      } else {
        return `${lastNeg} ans`;
      }
    } else {
      return "À la saint Glinglin";
    }
  })();
  const rembPret = (n) => {
    let totalRembAnnuel_n = store_get($$store_subs ??= {}, "$banque", banque).remboursementAnnuel.slice(0, n).reduce((a, b) => a + b, 0);
    let totalInteret_n = sum_(store_get($$store_subs ??= {}, "$interetsDetteSenior", interetsDetteSenior), n);
    let totalRembAnnuel = store_get($$store_subs ??= {}, "$banque", banque).remboursementAnnuel.reduce((a, b) => a + b, 0);
    let totalInteret = sum_(store_get($$store_subs ??= {}, "$interetsDetteSenior", interetsDetteSenior), n);
    return fmt0.format(totalRembAnnuel + totalInteret ? (totalRembAnnuel_n + totalInteret_n) * 100 / (totalRembAnnuel + totalInteret) : 0);
  };
  const economieFoyerMois = fmt2.format(coutElec ? (coutElec - coutElecAuto) / (coutElec / (store_get($$store_subs ??= {}, "$N", N) * 12)) : 0);
  const each_array = ensure_array_like(store_get($$store_subs ??= {}, "$soleil", soleil).weekly.y);
  $$payload.out.push(`<div class="p-10 svelte-1pjjt3m"><h1 class="my-8 text-4xl font-bold svelte-1pjjt3m">En un regard, <span class="text-primary svelte-1pjjt3m">tout</span> votre projet.</h1> <div class="my-5 py-2 total flex flex-col gap-20 svelte-1pjjt3m"><h2 class="text-2xl my-4 svelte-1pjjt3m">Synthèse durée totale ${escape_html(store_get($$store_subs ??= {}, "$N", N))} ans</h2> <div class="flex justify-around gap-10 px-10 svelte-1pjjt3m"><div class="flex flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$chiffreAffaires", chiffreAffaires), store_get($$store_subs ??= {}, "$N", N))))}</div> <div class="svelte-1pjjt3m">Chiffre d'affaire</div></div> <div class="flex flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$charges", charges), store_get($$store_subs ??= {}, "$N", N))))}</div> <div class="svelte-1pjjt3m">Charges d'exploitation</div></div> <div class="flex flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$resultatNet", resultatNet), store_get($$store_subs ??= {}, "$N", N))))}</div> <div class="svelte-1pjjt3m">Résultat net</div></div></div> <div class="flex justify-around gap-10 px-10 svelte-1pjjt3m"><div class="flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(store_get($$store_subs ??= {}, "$investissement", investissement)))}</div> <div class="svelte-1pjjt3m">Montant de l'investissement</div></div> <div class="flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(rentabiliteTaux(sum_(store_get($$store_subs ??= {}, "$resultatNet", resultatNet), store_get($$store_subs ??= {}, "$N", N))))}</div> <div class="svelte-1pjjt3m">Retour sur investissement</div></div> <div class="flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(roiDate)}</div> <div class="svelte-1pjjt3m">Point de rentabilité</div></div></div> <div class="flex justify-around gap-10 px-10 svelte-1pjjt3m"><div class="flex flex-col justify-around text-center gap-2 svelte-1pjjt3m"><div class="py-3 my-auto svelte-1pjjt3m"><svg${attr("viewBox", `${stringify(Math.floor(-store_get($$store_subs ??= {}, "$N", N) * 0.05))} ${stringify(Math.floor(-store_get($$store_subs ??= {}, "$tr", tr).maxa - store_get($$store_subs ??= {}, "$tr", tr).margin))} ${stringify(Math.ceil(store_get($$store_subs ??= {}, "$N", N) * 1.1))} ${stringify(Math.ceil(store_get($$store_subs ??= {}, "$tr", tr).maxa - store_get($$store_subs ??= {}, "$tr", tr).mina + 2 * store_get($$store_subs ??= {}, "$tr", tr).margin))}`)} xmlns="http://www.w3.org/2000/svg" width="250px" height="100px" preserveAspectRatio="none" class="svelte-1pjjt3m"><path${attr("d", store_get($$store_subs ??= {}, "$tr", tr).c)} fill="transparent" stroke-width="2" vector-effect="non-scaling-stroke" stroke="#b71515" class="svelte-1pjjt3m"></path></svg></div> <div class="svelte-1pjjt3m">Résultat net</div></div> <div class="flex flex-col justify-between text-center gap-2 svelte-1pjjt3m"><div style="width:250px" class="py-3 flex justify-center my-auto svelte-1pjjt3m"><div${attr_class("ms-3 loading svelte-1pjjt3m", void 0, {
    "hidden": !store_get($$store_subs ??= {}, "$pvgis", pvgis).loading
  })}></div> <svg${attr("viewBox", `-10 0 270 ${stringify(maxProd)}`)} preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg" width="250px" height="100px"${attr_class("svelte-1pjjt3m", void 0, {
    "hidden": store_get($$store_subs ??= {}, "$pvgis", pvgis).loading || !maxProd
  })}><!--[-->`);
  for (let i = 0, $$length = each_array.length; i < $$length; i++) {
    let p = each_array[i];
    $$payload.out.push(`<path${attr("d", `M ${stringify(i * 5)}, ${stringify(maxProd)} L ${stringify(i * 5)}, ${stringify(maxProd - p)}`)} fill="transparent" stroke-width="2" stroke="#ffbf00" class="svelte-1pjjt3m"></path>`);
  }
  $$payload.out.push(`<!--]--></svg> <svg${attr_class("donnee svelte-1pjjt3m", void 0, {
    "hidden": store_get($$store_subs ??= {}, "$pvgis", pvgis).loading || maxProd
  })} xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 16 16"><path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278" class="svelte-1pjjt3m"></path></svg></div> <div class="svelte-1pjjt3m">Production annuelle</div></div></div> <div class="flex justify-around p-5 svelte-1pjjt3m"><div class="me-3 flex flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(store_get($$store_subs ??= {}, "$N", N) ? format(sum_(store_get($$store_subs ??= {}, "$resultatNet", resultatNet), store_get($$store_subs ??= {}, "$N", N)) / store_get($$store_subs ??= {}, "$N", N)) : "0€")}</div> <div class="svelte-1pjjt3m">Bénéfice annuel moyen</div></div> <div class="ms-3 flex flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">`);
  if (coutElec - coutElecAuto > 0) {
    $$payload.out.push("<!--[-->");
    $$payload.out.push(`${escape_html(format(coutElec - coutElecAuto))}
                        (${escape_html(economieFoyerMois)} mois)`);
  } else {
    $$payload.out.push("<!--[!-->");
    $$payload.out.push(`Aucune 😢`);
  }
  $$payload.out.push(`<!--]--></div> <div class="svelte-1pjjt3m">Economies consommateur</div></div></div></div> <div class="my-10 py-2 dix svelte-1pjjt3m"><h2 class="text-2xl my-4 svelte-1pjjt3m">Synthèse 10 ans</h2> <div class="flex justify-around p-10 svelte-1pjjt3m"><div class="flex flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$chiffreAffaires", chiffreAffaires), 10)))}</div> <div class="svelte-1pjjt3m">Chiffre d'affaire</div></div> <div class="flex flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$charges", charges), 10)))}</div> <div class="svelte-1pjjt3m">Coût total</div></div> <div class="flex flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$resultatNet", resultatNet), 10)))}</div> <div class="svelte-1pjjt3m">Résultat net</div></div></div> <div class="flex justify-around p-5 svelte-1pjjt3m"><div class="flex flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(rembPret(10))}%</div> <div class="svelte-1pjjt3m">Prêt remboursé</div></div> <div class="flex flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(rentabiliteTaux(sum_(store_get($$store_subs ??= {}, "$resultatNet", resultatNet), 10)))}</div> <div class="svelte-1pjjt3m">Rentabilité</div></div></div></div> <div class="my-5 py-10 cinq svelte-1pjjt3m"><h2 class="text-2xl my-4 svelte-1pjjt3m">Synthèse 5 ans</h2> <div class="flex justify-around p-5 gap-3 svelte-1pjjt3m"><div class="flex flex-col text-center svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$chiffreAffaires", chiffreAffaires), 5)))}</div> <div class="svelte-1pjjt3m">Chiffre d'affaire</div></div> <div class="flex flex-col text-center svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$charges", charges), 5)))}</div> <div class="svelte-1pjjt3m">Coût total</div></div> <div class="flex flex-col text-center svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$resultatNet", resultatNet), 5)))}</div> <div class="svelte-1pjjt3m">Résultat net</div></div></div> <div class="flex justify-around p-5 svelte-1pjjt3m"><div class="flex flex-col text-center svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(rembPret(5))}%</div> <div class="svelte-1pjjt3m">Prêt remboursé</div></div> <div class="flex-col text-center gap-2 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(rentabiliteTaux(sum_(store_get($$store_subs ??= {}, "$resultatNet", resultatNet), 5)))}</div> <div class="svelte-1pjjt3m">Rentabilité</div></div></div></div> <div class="my-5 py-10 moyenne svelte-1pjjt3m"><h2 class="text-2xl my-4 svelte-1pjjt3m">Synthèse année moyenne</h2> <div class="flex justify-between flex-wrap py-5 svelte-1pjjt3m"><div class="flex flex-col text-center mx-3 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$resultatNet", resultatNet), store_get($$store_subs ??= {}, "$N", N)) / store_get($$store_subs ??= {}, "$N", N)))}</div> <div class="svelte-1pjjt3m">Bénéfices moyen</div></div> <div class="flex flex-col text-center mx-3 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(format(sum_(store_get($$store_subs ??= {}, "$chiffreAffaires", chiffreAffaires), store_get($$store_subs ??= {}, "$N", N)) / store_get($$store_subs ??= {}, "$N", N)))}</div> <div class="svelte-1pjjt3m">Chiffre d'affaire</div></div> <div class="flex flex-col text-center mx-3 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">${escape_html(fmt2.format((store_get($$store_subs ??= {}, "$investissement", investissement) ? sum_(store_get($$store_subs ??= {}, "$resultatNet", resultatNet), store_get($$store_subs ??= {}, "$N", N)) * 100 / store_get($$store_subs ??= {}, "$investissement", investissement) - 100 : 0) / store_get($$store_subs ??= {}, "$N", N)))}%</div> <div class="svelte-1pjjt3m">Rentabilité annuelle</div></div> <div class="flex flex-col text-center mx-3 svelte-1pjjt3m"><div class="donnee svelte-1pjjt3m">`);
  if (coutElec - coutElecAuto > 0) {
    $$payload.out.push("<!--[-->");
    $$payload.out.push(`${escape_html(fmt0.format((coutElec - coutElecAuto) * 100 / coutElec))}%`);
  } else {
    $$payload.out.push("<!--[!-->");
    $$payload.out.push(`Aucune 😢`);
  }
  $$payload.out.push(`<!--]--></div> <div class="svelte-1pjjt3m">Économies consommateur</div></div></div></div></div>`);
  if ($$store_subs) unsubscribe_stores($$store_subs);
  pop();
}
function _page($$payload) {
  Synthese($$payload);
}
export {
  _page as default
};
