import { a as push, g as getContext, f as store_get, h as ensure_array_like, b as attr, m as maybe_selected, e as escape_html, j as stringify, l as attr_class, o as attr_style, u as unsubscribe_stores, q as bind_props, p as pop, t as copy_payload, v as assign_payload } from "../../../../../../chunks/index.js";
import { p as profils } from "../../../../../../chunks/grain-types.js";
import "financial";
import { X as XCircle } from "../../../../../../chunks/XCircle.js";
import { Q as Question } from "../../../../../../chunks/Question.js";
function PlusCircle($$payload) {
  $$payload.out.push(`<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6"><path stroke-linecap="round" stroke-linejoin="round" d="M12 9v6m3-3H9m12 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"></path></svg>`);
}
function Simulation($$payload, $$props) {
  push();
  var $$store_subs;
  let { data = void 0, pvgis_loading = true } = $$props;
  const data_ = data;
  getContext("simulation");
  let prms = getContext("prms");
  getContext("prms_modified");
  let profils$1 = getContext("profils");
  getContext("profils_modified");
  getContext("soleil");
  const expert = getContext("expert");
  getContext("communaute");
  (() => {
    console.log("derived by");
    let annee2 = data.annee;
    if (!annee2) {
      annee2 = {};
    }
    for (let i = 0; i < store_get($$store_subs ??= {}, "$prms", prms).length; i++) {
      const p = store_get($$store_subs ??= {}, "$prms", prms)[i];
      if (p.releve) {
        annee2[p.prm || i] = [];
        for (const j of p.releve.split("\n")) {
          const l = j.split(/;|,| /);
          if (l.length >= 2) {
            const d = new Date(l[0]);
            const v = parseInt(l[1]);
            if (d && !isNaN(v)) {
              annee2[p.prm || i].push(v);
            }
          }
        }
        console.log(annee2[p.prm || i]);
      }
    }
    return annee2;
  })();
  getContext("pvgis");
  let plotStyle = "";
  let profils_ = profils(store_get($$store_subs ??= {}, "$expert", expert));
  expert.subscribe((value) => profils_ = profils(value));
  performance.now();
  let totalProd = 0;
  let totalConso = 0;
  $$payload.out.push(`<div class="p-10 flex flex-col"><h1 class="my-8 text-4xl font-bold">Production… <span class="text-primary">Consommation</span></h1> <form class="mt-10" method="POST" action="/api/grain" id="pdl"></form> `);
  {
    $$payload.out.push("<!--[!-->");
  }
  $$payload.out.push(`<!--]--> <h2 class="text-2xl my-5">Profils de consommation</h2> `);
  if (store_get($$store_subs ??= {}, "$profils", profils$1)?.length) {
    $$payload.out.push("<!--[-->");
    const each_array = ensure_array_like(store_get($$store_subs ??= {}, "$profils", profils$1));
    if (store_get($$store_subs ??= {}, "$expert", expert)) {
      $$payload.out.push("<!--[-->");
      $$payload.out.push(`<div>Vous êtes en mode expert. Dans ce mode, vous avez un accès
                direct à tous les profils utilisés par les opérateurs du réseau
                pour simuler les consommations. Voir <a href="https://www.enedis.fr/responsable-dequilibre-profilage-et-profils">la documentation</a>.</div>`);
    } else {
      $$payload.out.push("<!--[!-->");
    }
    $$payload.out.push(`<!--]--> <table class="table w-auto mx-auto my-5"><thead><tr><th>Profil</th><th>Consommation annuelle (kWh)</th><th></th></tr></thead><tbody><!--[-->`);
    for (let i = 0, $$length = each_array.length; i < $$length; i++) {
      let pr = each_array[i];
      const each_array_1 = ensure_array_like(Object.keys(profils_));
      $$payload.out.push(`<tr><td><select class="select">`);
      $$payload.select_value = pr.type;
      $$payload.out.push(`<!--[-->`);
      for (let $$index = 0, $$length2 = each_array_1.length; $$index < $$length2; $$index++) {
        let p = each_array_1[$$index];
        $$payload.out.push(`<option${attr("value", p)}${maybe_selected($$payload, p)}>${escape_html(p)}</option>`);
      }
      $$payload.out.push(`<!--]-->`);
      $$payload.select_value = void 0;
      $$payload.out.push(`</select></td><td><input type="number" class="input svelte-1a79xey" placeholder="Abonnement (kW)" min="0" step="500"${attr("value", pr.conso)}/></td><td><button class="text-primary" aria-label="Supprimer ce profil">`);
      XCircle($$payload);
      $$payload.out.push(`<!----></button></td></tr>`);
    }
    $$payload.out.push(`<!--]--></tbody></table>`);
  } else {
    $$payload.out.push("<!--[!-->");
  }
  $$payload.out.push(`<!--]--> <button class="mx-auto btn btn-primary">`);
  PlusCircle($$payload);
  $$payload.out.push(`<!----> Ajouter un profil</button> <h2 class="text-2xl mt-10 my-5">Relevés de compteurs</h2> `);
  if (data_.active && data_.id && data_.id != "nouveau") {
    $$payload.out.push("<!--[-->");
    $$payload.out.push(`<div class="mb-5">Vous pouvez ajouter des compteurs d'entreprises ou d'associations
            par leur numéro de compteur et leur numéro de SIRET.</div> `);
    if (store_get($$store_subs ??= {}, "$prms", prms)?.length) {
      $$payload.out.push("<!--[-->");
      const each_array_2 = ensure_array_like(store_get($$store_subs ??= {}, "$prms", prms));
      $$payload.out.push(`<!--[-->`);
      for (let $$index_2 = 0, $$length = each_array_2.length; $$index_2 < $$length; $$index_2++) {
        let prm = each_array_2[$$index_2];
        let i = $$index_2;
        $$payload.out.push(`<div class="flex flex-wrap gap-x-10 gap-5"><div class="flex flex-col w-full gap-2"><div class="flex"><label class="label"${attr("for", `prm-${stringify(i)}`)}>PRM</label> `);
        Question($$payload, {
          datatip: "Un PRM, aussi appelé PDL, est l'identifiant d'un compteur électrique. On peut le trouver sur les factures d'électricité, ou en appuyant sur le bouton + du compteur Linky."
        });
        $$payload.out.push(`<!----></div> <div class="flex gap-1"><input class="input input-sm svelte-1a79xey"${attr("id", `prm-${stringify(i)}`)}${attr("value", prm.prm)} type="number"/> <button class="btn btn-outline btn-primary btn-sm ms-auto" aria-label="Supprimer ce compteur">`);
        XCircle($$payload);
        $$payload.out.push(`<!----></button></div></div> <div class="flex flex-col gap-2"><label class="label"${attr("for", `prm-${stringify(i)}`)}>Relevé au format CSV</label> <div class="flex"><input class="file-input file-input-sm svelte-1a79xey"${attr("id", `releve-${stringify(i)}`)} type="file"/> `);
        if (prm.releve?.length) {
          $$payload.out.push("<!--[-->");
          Question($$payload, {
            datatip: "Nous avons enregistré votre fichier, mais vous pouvez le mettre à jour à tout moment."
          });
        } else {
          $$payload.out.push("<!--[!-->");
        }
        $$payload.out.push(`<!--]--></div></div> <div class="flex gap-5"><div class="flex gap-2 items-center"><input type="radio" class="radio radio-sm svelte-1a79xey" id="particulier"${attr("value", false)}${attr("checked", prm.pro === false, true)}/> <label class="label" for="particulier">Particulier</label></div> <div class="flex gap-2 items-center"><input type="radio" class="radio radio-sm svelte-1a79xey" id="pro"${attr("value", true)}${attr("checked", prm.pro === true, true)}/> <label class="label" for="pro">Professionnel</label></div></div> `);
        if (prm.pro) {
          $$payload.out.push("<!--[-->");
          $$payload.out.push(`<div class="flex flex-col my-2 gap-2 w-full"><label class="label"${attr("for", `siret-${stringify(i)}`)}>SIRET du titulaire du compteur</label> <input class="input input-sm svelte-1a79xey"${attr("id", `siret-${stringify(i)}`)}${attr("value", prm.siret)}/></div> <div class="flex flex-col my-2 gap-2"><label class="label"${attr("for", `prenom-${stringify(i)}`)}>Prénom du titulaire du compteur</label> <input class="input input-sm svelte-1a79xey"${attr("id", `prenom-${stringify(i)}`)}${attr("value", prm.prenom)}/></div> <div class="flex flex-col my-2 gap-2"><label class="label"${attr("for", `nom-${stringify(i)}`)}>Nom du titulaire du compteur</label> <input class="input input-sm svelte-1a79xey"${attr("id", `nom-${stringify(i)}`)}${attr("value", prm.nom)}/></div>`);
        } else {
          $$payload.out.push("<!--[!-->");
          $$payload.out.push(`<div class="flex flex-col my-2 gap-2"><label class="label"${attr("for", `prenom-${stringify(i)}`)}>Prénom du titulaire du compteur</label> <input class="input input-sm svelte-1a79xey"${attr("id", `prenom-${stringify(i)}`)}${attr("value", prm.prenom)}/></div> <div class="flex flex-col my-2 gap-2"><label class="label"${attr("for", `nom-${stringify(i)}`)}>Nom du titulaire du compteur</label> <input class="input input-sm svelte-1a79xey"${attr("id", `nom-${stringify(i)}`)}${attr("value", prm.nom)}/></div> <div class="flex flex-col my-2 gap-2"><label class="label"${attr("for", `numerorue-${stringify(i)}`)}>Numéro et rue</label> <input class="input input-sm svelte-1a79xey"${attr("id", `numerorue-${stringify(i)}`)}${attr("value", prm.numerorue)}/></div> <div class="flex flex-col my-2 gap-2"><label class="label"${attr("for", `codepostal-${stringify(i)}`)}>Code INSEE de la commune</label> <input class="input input-sm svelte-1a79xey"${attr("id", `codepostal-${stringify(i)}`)}${attr("value", prm.codeinsee)}/></div> <div class="flex flex-col my-2 gap-2"><label class="label"${attr("for", `serie-${stringify(i)}`)}>Numéro de série</label> <input class="input input-sm svelte-1a79xey"${attr("id", `serie-${stringify(i)}`)}${attr("value", prm.serie)}/></div>`);
        }
        $$payload.out.push(`<!--]--> <div class="flex items-center gap-2"><input class="checkbox checkbox-primary checkbox-sm svelte-1a79xey" type="checkbox"${attr("id", `consentement-${stringify(i)}`)}${attr("checked", prm.consentement, true)}/> <label class="label"${attr("for", `consentement-${stringify(i)}`)}>Cet abonné consent à ce que j'accède à ses données.</label></div> <hr class="my-4"/></div>`);
      }
      $$payload.out.push(`<!--]--> <div class="my-3">Ce service ne consulte la courbe de charge du compteur
                communiquant qu'une seule fois, et ce consentement expire dès
                que ces données pour l'année écoulée sont récupérées. Les
                données collectées depuis les compteurs communiquants sont
                stockées temporairement sur nos serveurs pour des raisons de
                performance, ne sont utilisées que pour afficher cette page et
                produire le rapport, et ne sont jamais conservées plus de 30
                jours.</div> <div class="my-3">Les données envoyées via un fichier sont conservées jusqu'à ce
                que vous supprimiez ce projet.</div>`);
    } else {
      $$payload.out.push("<!--[!-->");
    }
    $$payload.out.push(`<!--]--> <div class="flex justify-center my-10"><button class="mx-2 btn btn-primary">`);
    PlusCircle($$payload);
    $$payload.out.push(`<!----> Ajouter un compteur</button> `);
    if (store_get($$store_subs ??= {}, "$prms", prms).length) {
      $$payload.out.push("<!--[-->");
      $$payload.out.push(`<button class="mx-2 btn btn-primary" form="pdl">Mettre à jour</button> <div${attr_class("mx-2 loading text-primary", void 0, { "hidden": true })}></div>`);
    } else {
      $$payload.out.push("<!--[!-->");
    }
    $$payload.out.push(`<!--]--></div>`);
  } else {
    $$payload.out.push("<!--[!-->");
    if (data_.active) {
      $$payload.out.push("<!--[-->");
      $$payload.out.push(`<div class="my-3">Sauvegardez ce projet pour avoir accès aux relevés de compteurs
            détaillés.</div>`);
    } else {
      $$payload.out.push("<!--[!-->");
      $$payload.out.push(`<div class="my-3">Vous devez être abonné pour avoir accès aux relevés de compteurs
            détaillés. Cependant, si vous disposez des relevés au format CSV (au
            format de ligne: <code>2025-11-18T14:00:00+01:00; 440</code>),
            n'hésitez pas à les fournir ici.</div>`);
    }
    $$payload.out.push(`<!--]-->`);
  }
  $$payload.out.push(`<!--]--> `);
  if (store_get($$store_subs ??= {}, "$prms", prms)?.length) {
    $$payload.out.push("<!--[-->");
    const each_array_3 = ensure_array_like(store_get($$store_subs ??= {}, "$prms", prms));
    $$payload.out.push(`<!--[-->`);
    for (let i = 0, $$length = each_array_3.length; i < $$length; i++) {
      let prm = each_array_3[i];
      $$payload.out.push(`<div class="flex flex-col my-2 gap-2"><label class="label"${attr("for", `prm-${stringify(i)}`)}>Relevé au format CSV</label> <div class="flex items-center gap-2"><input class="file-input file-input-sm svelte-1a79xey"${attr("id", `releve-${stringify(i)}`)} type="file"/> `);
      if (prm.releve?.length) {
        $$payload.out.push("<!--[-->");
        $$payload.out.push(`<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6"><path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"></path></svg>`);
      } else {
        $$payload.out.push("<!--[!-->");
      }
      $$payload.out.push(`<!--]--> <button class="btn btn-outline btn-primary btn-sm" aria-label="Supprimer ce compteur">`);
      XCircle($$payload);
      $$payload.out.push(`<!----></button></div></div>`);
    }
    $$payload.out.push(`<!--]-->`);
  } else {
    $$payload.out.push("<!--[!-->");
  }
  $$payload.out.push(`<!--]--> <div class="flex justify-center my-5"><button class="mx-2 btn btn-primary">`);
  PlusCircle($$payload);
  $$payload.out.push(`<!----> Ajouter un compteur</button> `);
  if (store_get($$store_subs ??= {}, "$prms", prms).length) {
    $$payload.out.push("<!--[-->");
    $$payload.out.push(`<button class="mx-2 btn btn-primary" form="pdl">Mettre à jour</button> <div${attr_class("mx-2 loading text-primary", void 0, { "hidden": true })}></div>`);
  } else {
    $$payload.out.push("<!--[!-->");
  }
  $$payload.out.push(`<!--]--></div> <h2 class="text-2xl mt-5 my-10">Simulation</h2> <div class="me-auto"><table class="table inline-block"><tbody><tr><td>Production totale:</td><td>`);
  if (isNaN(totalProd)) {
    $$payload.out.push("<!--[-->");
    $$payload.out.push(`Calcul: impossible`);
  } else {
    $$payload.out.push("<!--[!-->");
    $$payload.out.push(`${escape_html(Math.round(totalProd))}kWh`);
  }
  $$payload.out.push(`<!--]--></td></tr><tr><td>Consommation totale:</td><td>${escape_html(Math.round(totalConso))}kWh</td></tr></tbody></table></div> `);
  if (pvgis_loading) {
    $$payload.out.push("<!--[-->");
    $$payload.out.push(`<div class="flex items-center text-primary my-5 gap-3"><div class="loading text-primary"></div> Chargement de la production solaire</div>`);
  } else {
    $$payload.out.push("<!--[!-->");
  }
  $$payload.out.push(`<!--]--> <div class="my-3"><div class="mx-auto" style="min-height:450px" id="plot"></div></div> <div class="flex flex-wrap"><div class="p-0 m-0 sm:w-1/2 w-full"${attr_style(plotStyle)} id="autoProd"></div> <div class="p-0 m-0 sm:w-1/2 w-full"${attr_style(plotStyle)} id="autoConso"></div></div></div>`);
  if ($$store_subs) unsubscribe_stores($$store_subs);
  bind_props($$props, { data });
  pop();
}
function _page($$payload, $$props) {
  push();
  const { $$slots, $$events, ...props } = $$props;
  let data = props.data;
  let $$settled = true;
  let $$inner_payload;
  function $$render_inner($$payload2) {
    Simulation($$payload2, {
      get data() {
        return data;
      },
      set data($$value) {
        data = $$value;
        $$settled = false;
      }
    });
  }
  do {
    $$settled = true;
    $$inner_payload = copy_payload($$payload);
    $$render_inner($$inner_payload);
  } while (!$$settled);
  assign_payload($$payload, $$inner_payload);
  pop();
}
export {
  _page as default
};
