import { g as getContext, f as store_get, i as head, b as attr, e as escape_html, j as stringify, h as ensure_array_like, m as maybe_selected, u as unsubscribe_stores, p as pop, a as push } from "../../../../../../chunks/index.js";
import { o as onDestroy } from "../../../../../../chunks/index-server.js";
import { a as asset } from "../../../../../../chunks/index3.js";
import { Q as Question } from "../../../../../../chunks/Question.js";
import { X as XCircle } from "../../../../../../chunks/XCircle.js";
function Description($$payload, $$props) {
  push();
  var $$store_subs;
  let communaute = getContext("communaute");
  let derogation = store_get($$store_subs ??= {}, "$communaute", communaute)?.derogation ?? "non";
  let map = null;
  function derogationRayonKm() {
    if (store_get($$store_subs ??= {}, "$communaute", communaute)?.derogation == "20") {
      return 10;
    } else if (store_get($$store_subs ??= {}, "$communaute", communaute)?.derogation == "10") {
      return 5;
    } else {
      return 1;
    }
  }
  let consommateurs = [];
  let total = 0;
  onDestroy(() => {
    console.log("destroy", map);
  });
  let endroit = "";
  let fmt = (n) => Intl.NumberFormat("fr-FR", { maximumFractionDigits: n });
  function formatConso(x) {
    let f = 0;
    let s = "";
    if (Math.abs(x) <= 1e3) {
      s = " MWh/an";
      f = x;
    } else if (Math.abs(x) <= 1e6) {
      s = " GWh/an";
      f = x / 1e3;
    } else {
      s = " TWh/an";
      f = x / 1e6;
    }
    return fmt(2).format(f) + s;
  }
  head($$payload, ($$payload2) => {
    $$payload2.out.push(`<link rel="stylesheet"${attr("href", asset("/static/leaflet.css"))}/> <link rel="stylesheet"${attr("href", asset("/static/leafletfs.css"))}/> <link rel="stylesheet"${attr("href", asset("/static/leaflet-gesture-handling.min.css"))}/>`);
  });
  $$payload.out.push(`<div class="p-10" style="min-height: calc(100vh - 180px);"><h1 class="my-8 text-4xl font-bold">Quelques <span class="text-primary">détails</span> de plus.</h1> <div class="my-3 mt-10">Cliquez sur la carte ci-dessous pour ajouter des compteurs, ou cliquez
        en maintenant la touche Shift pour positionner la communauté.</div> <div class="my-3"><form><div class="flex gap-2 items-center"><input type="text" class="input"${attr("value", endroit)}/> <button class="btn btn-outline btn-primary">Recherche</button> `);
  {
    $$payload.out.push("<!--[!-->");
  }
  $$payload.out.push(`<!--]--></div></form></div> <div class="w-full" style="height:500px"></div> <div class="flex flex-col"><button class="btn btn-sm btn-outline btn-secondary my-2 ms-auto">Recentrer</button> <button class="btn btn-sm btn-outline btn-secondary my-2 ms-auto">Reset</button> `);
  if (store_get($$store_subs ??= {}, "$communaute", communaute)) {
    $$payload.out.push("<!--[-->");
    $$payload.out.push(`<div class="my-3">Total des consommations: ${escape_html(formatConso(total))}<br/>Nombre de
                consommateurs: ${escape_html(consommateurs.length)}<br/><a${attr("href", `/api/grain/consommateurs/csv?lat=${stringify(store_get($$store_subs ??= {}, "$communaute", communaute).lat)}&lng=${stringify(store_get($$store_subs ??= {}, "$communaute", communaute).lng)}&rayon=${stringify(derogationRayonKm() * 1e3)}`)} target="blank">Télécharger les adresses au format CSV</a></div>`);
  } else {
    $$payload.out.push("<!--[!-->");
  }
  $$payload.out.push(`<!--]--> `);
  if (store_get($$store_subs ??= {}, "$communaute", communaute)?.compteurs?.length) {
    $$payload.out.push("<!--[-->");
    const each_array = ensure_array_like(store_get($$store_subs ??= {}, "$communaute", communaute).compteurs);
    $$payload.out.push(`<div class="my-3"><table class="table"><thead><tr><th>Puissance</th><th>Azimuth (le Sud est à 0°, l'Est à -90°, l'Ouest
                                à 90°)</th><th>Inclinaison</th><th></th></tr></thead><tbody><!--[-->`);
    for (let i = 0, $$length = each_array.length; i < $$length; i++) {
      let c = each_array[i];
      $$payload.out.push(`<tr><td><input type="number" class="input"${attr("value", c.puissance)}/></td><td><input type="number" class="input"${attr("value", c.azimuth)}/></td><td><input type="number" class="input"${attr("value", c.inclinaison)}/></td><td><button class="btn btn-sm btn-outline btn-primary" aria-label="Supprimer le compteur">`);
      XCircle($$payload);
      $$payload.out.push(`<!----></button></td></tr>`);
    }
    $$payload.out.push(`<!--]--></tbody></table></div>`);
  } else {
    $$payload.out.push("<!--[!-->");
  }
  $$payload.out.push(`<!--]--> <div class="p-3 flex flex-col"><div class="flex"><label for="derog" class="form-label">Extension de périmètre (habitat rural et/ou dispersé)</label> `);
  Question($$payload, {
    datatip: "Par dérogation, le rayon de la communauté peut être étendu à 5km ou 10km."
  });
  $$payload.out.push(`<!---->  <select id="derog" class="select select-sm w-auto inline ms-5" name="derog">`);
  $$payload.select_value = derogation;
  $$payload.out.push(`<option value="non"${maybe_selected($$payload, "non")}>Non</option><option value="10"${maybe_selected($$payload, "10")}>10km</option><option value="20"${maybe_selected($$payload, "20")}>20km</option>`);
  $$payload.select_value = void 0;
  $$payload.out.push(`</select></div> `);
  {
    $$payload.out.push("<!--[!-->");
  }
  $$payload.out.push(`<!--]--></div></div></div>`);
  if ($$store_subs) unsubscribe_stores($$store_subs);
  pop();
}
function _page($$payload) {
  Description($$payload);
}
export {
  _page as default
};
