import { OUTER_LABEL_CONFIG } from "./constants.js";
import { getCssVariableValue } from "./colors.js";
function calculateSumTotal(data) {
  return (data ?? []).reduce((sum, d) => sum + Number(d.value || 0), 0);
}
function drawOuterLabel(ctx, text, middleAngle, anchorX, anchorY) {
  const THRESHOLD = 5 * (Math.PI / 180);
  const normalizedAngle = (middleAngle % (Math.PI * 2) + Math.PI * 2) % (Math.PI * 2);
  let textX;
  let textY;
  if (Math.abs(normalizedAngle - Math.PI * 1.5) < THRESHOLD) {
    ctx.textAlign = "center";
    ctx.textBaseline = "bottom";
    textX = anchorX;
    textY = anchorY;
  } else if (Math.abs(normalizedAngle - Math.PI * 0.5) < THRESHOLD) {
    ctx.textAlign = "center";
    ctx.textBaseline = "top";
    textX = anchorX;
    textY = anchorY;
  } else if (normalizedAngle < THRESHOLD || normalizedAngle > Math.PI * 2 - THRESHOLD) {
    ctx.textAlign = "left";
    ctx.textBaseline = "middle";
    textX = anchorX;
    textY = anchorY;
  } else if (Math.abs(normalizedAngle - Math.PI) < THRESHOLD) {
    ctx.textAlign = "right";
    ctx.textBaseline = "middle";
    textX = anchorX;
    textY = anchorY;
  } else if (normalizedAngle > THRESHOLD && normalizedAngle < Math.PI * 0.5 - THRESHOLD) {
    ctx.textAlign = "left";
    ctx.textBaseline = "top";
    textX = anchorX;
    textY = anchorY;
  } else if (normalizedAngle > Math.PI * 0.5 + THRESHOLD && normalizedAngle < Math.PI - THRESHOLD) {
    ctx.textAlign = "right";
    ctx.textBaseline = "top";
    textX = anchorX;
    textY = anchorY;
  } else if (normalizedAngle > Math.PI + THRESHOLD && normalizedAngle < Math.PI * 1.5 - THRESHOLD) {
    ctx.textAlign = "right";
    ctx.textBaseline = "bottom";
    textX = anchorX;
    textY = anchorY;
  } else {
    ctx.textAlign = "left";
    ctx.textBaseline = "bottom";
    textX = anchorX;
    textY = anchorY;
  }
  ctx.fillText(text, textX, textY);
}
function createArcOuterLabelPlugin(host, options) {
  return {
    id: "outerLabels",
    afterDatasetsDraw: (chart) => {
      const ctx = chart.ctx;
      const fontFamily = getCssVariableValue(
        host,
        OUTER_LABEL_CONFIG.fontFamily
      );
      const fontWeight = getCssVariableValue(
        host,
        OUTER_LABEL_CONFIG.fontWeightVar
      );
      const fontSize = getCssVariableValue(
        host,
        OUTER_LABEL_CONFIG.fontSizeVar
      );
      const fontColor = getCssVariableValue(
        host,
        OUTER_LABEL_CONFIG.fontColorVar
      );
      ctx.save();
      ctx.font = `${fontWeight} ${fontSize} ${fontFamily}`;
      ctx.fillStyle = fontColor;
      ctx.textBaseline = "middle";
      if (!options.type || options.type === "arc") {
        const dataset = chart.data.datasets?.[0];
        const labels = chart.data.labels ?? [];
        if (!dataset) return;
        const meta = chart.getDatasetMeta(0);
        if (!meta?.data.length) return;
        const values = Array.isArray(dataset.data) ? dataset.data : [];
        meta.data.forEach((element, index) => {
          const arc = element;
          const label = labels[index];
          if (options.hideIndex !== null && options.hideIndex !== void 0 && index === options.hideIndex)
            return;
          if (!arc || label === "Remaining") return;
          const rawValue = Number(values[index] ?? 0);
          if (!Number.isFinite(rawValue) || rawValue <= 0) return;
          const middleAngle = (arc.startAngle + arc.endAngle) / 2;
          const cosAngle = Math.cos(middleAngle);
          const sinAngle = Math.sin(middleAngle);
          const anchorRadius = arc.outerRadius + OUTER_LABEL_CONFIG.labelGap;
          const anchorX = arc.x + cosAngle * anchorRadius;
          const anchorY = arc.y + sinAngle * anchorRadius;
          const text = options.formattedLabels[index] || "";
          drawOuterLabel(ctx, text, middleAngle, anchorX, anchorY);
        });
      } else if (options.type === "polar") {
        const chartAny = chart;
        const scale = chartAny.scales.r;
        if (!scale) return;
        const centerX = scale.xCenter;
        const centerY = scale.yCenter;
        const chartRadius = (options.chartDiameter ?? scale.drawingArea * 2) / 2;
        const numSectors = options.formattedLabels.length;
        if (numSectors === 0) return;
        const anglePerSector = Math.PI * 2 / numSectors;
        const rotationOffset = options.rotationOffset ?? 0;
        options.formattedLabels.forEach((label, i) => {
          const startAngle = i * anglePerSector + rotationOffset - Math.PI / 2;
          const endAngle = startAngle + anglePerSector;
          const middleAngle = (startAngle + endAngle) / 2;
          const cosAngle = Math.cos(middleAngle);
          const sinAngle = Math.sin(middleAngle);
          const anchorRadius = chartRadius + OUTER_LABEL_CONFIG.labelGap;
          const anchorX = centerX + cosAngle * anchorRadius;
          const anchorY = centerY + sinAngle * anchorRadius;
          drawOuterLabel(ctx, label, middleAngle, anchorX, anchorY);
        });
      }
      ctx.restore();
    }
  };
}
export {
  calculateSumTotal,
  createArcOuterLabelPlugin
};
//# sourceMappingURL=outer-labels.js.map
