import { jsx as u } from "@emotion/react/jsx-runtime";
import { classes as b } from "../../classes/index.js";
import A from "clsx";
import { createRef as B, useEffect as W } from "react";
import { Chart as E, registerables as P } from "chart.js";
import { LineWithErrorBarsChart as g, ScatterWithErrorBarsController as N, PointWithErrorBar as R } from "chartjs-chart-error-bars";
import { useBase as S, getPropertyByString as M, evaluateWithContext as y } from "@m4l/core";
import { ChartNodeRootStyled as j } from "../../slots/PrintingSystemSlots.js";
E.register(
  N,
  g,
  R,
  ...P
);
const q = (f) => {
  const { style: m = {}, className: d = "", points: e, messageX: h, messageY: p } = f, s = B(), x = /\$\{([^{}]+)\}/g, { sourceData: n } = S(), v = typeof e == "string" && x.exec(e) || "", a = typeof e == "string" ? M(n, v[1]) : e, r = [];
  Array.isArray(a) && a.length > 0 && a.forEach((t, o) => {
    if (r.push(t), o < a.length - 1) {
      const c = a[o + 1], l = 0.5;
      for (let i = t.avg + l; i < c.avg; i += l)
        r.push({ avg: i, correction: null, uncertainty: null });
    }
  });
  const C = Array.isArray(r) && r.length > 0 ? r.map((t) => ({
    x: t.avg,
    // Usar el valor promedio como el punto x
    y: t.correction,
    yMin: t.correction !== null && t.uncertainty !== null ? t.correction - t.uncertainty : 0,
    yMax: t.correction !== null && t.uncertainty !== null ? t.correction + t.uncertainty : 0
  })) : [];
  return W(() => {
    const t = s.current;
    if (t !== null) {
      const o = t.getContext("2d");
      o !== null && new g(o, {
        data: {
          labels: Array.isArray(r) && r.length > 0 ? r.map((c) => c.avg) : [],
          datasets: [
            {
              data: C,
              borderWidth: 0,
              pointBorderWidth: 0
            }
          ]
        },
        options: {
          plugins: {
            legend: {
              display: !1
            }
          },
          elements: {
            pointWithErrorBar: {
              radius: 5,
              backgroundColor: "black"
            }
          },
          scales: {
            x: {
              title: {
                display: !0,
                text: y(n, h)
              },
              beginAtZero: !1
              // No empezar en 0 en el eje x
            },
            y: {
              title: {
                display: !0,
                text: y(n, p)
              }
            }
          }
        }
      });
    }
  }, [s, e]), /* @__PURE__ */ u(
    j,
    {
      className: A(b.chartNodeRoot, d),
      style: {
        ...m
      },
      children: /* @__PURE__ */ u("canvas", { ref: s })
    }
  );
};
export {
  q as ChartNode
};
