import { CHART_DIMENSIONS, OUTER_LABEL_CONFIG } from "./constants.js";
import { getCssVariableValue } from "./colors.js";
function generateLabels(data, denominator, config, outerLabelMaxLength = 0) {
  const formattedLabels = [];
  if (denominator === 1) {
    for (const item of data) {
      let label = item.label;
      if (outerLabelMaxLength > 0 && label.length > outerLabelMaxLength) {
        label = label.substring(0, outerLabelMaxLength) + "…";
      }
      formattedLabels.push(label);
    }
    let maxLabel = "";
    for (const item of data) {
      if (item.label.length > maxLabel.length) maxLabel = item.label;
    }
    if (outerLabelMaxLength > 0 && maxLabel.length > outerLabelMaxLength) {
      maxLabel = maxLabel.substring(0, outerLabelMaxLength) + "…";
    }
    formattedLabels.push(maxLabel);
    return formattedLabels;
  }
  for (const item of data) {
    const formattedLabel = formatSingleLabel(item.value, denominator, {
      unit: config.unit,
      showUnit: config.showUnit,
      decimalPlaces: config.decimalPlaces,
      maxLength: outerLabelMaxLength
    });
    formattedLabels.push(formattedLabel);
  }
  const maxFormattedLabel = formatSingleLabel(denominator, denominator, {
    unit: config.unit,
    showUnit: config.showUnit,
    decimalPlaces: config.decimalPlaces,
    maxLength: outerLabelMaxLength
  });
  formattedLabels.push(maxFormattedLabel);
  return formattedLabels;
}
function formatNumericValue(value, denominator, asPercentage, decimalPlaces = 0) {
  if (!asPercentage) {
    return value.toFixed(decimalPlaces);
  }
  const percentage = value / denominator * 100;
  return percentage.toFixed(decimalPlaces);
}
function formatSingleLabel(value, denominator, config) {
  const isPercentage = config.unit === "%";
  const numericValue = formatNumericValue(
    value,
    denominator,
    isPercentage,
    config.decimalPlaces ?? 0
  );
  let label = numericValue;
  if (config.showUnit && config.unit) {
    label = `${numericValue}${config.unit}`;
  }
  if (config.maxLength && config.maxLength > 0 && label.length > config.maxLength) {
    return label.substring(0, config.maxLength) + "…";
  }
  return label;
}
function calculateChartPaddingAndLabels(host, ctx, showOuterLabels, config) {
  const topBottomPadding = CHART_DIMENSIONS.CANVAS_PADDING;
  let leftRightPadding = CHART_DIMENSIONS.CANVAS_PADDING;
  let formattedLabels = [];
  if (showOuterLabels) {
    const fontFamily = getCssVariableValue(host, OUTER_LABEL_CONFIG.fontFamily);
    const fontWeight = getCssVariableValue(
      host,
      OUTER_LABEL_CONFIG.fontWeightVar
    );
    const fontSize = getCssVariableValue(host, OUTER_LABEL_CONFIG.fontSizeVar);
    ctx.save();
    ctx.font = `${fontWeight} ${fontSize} ${fontFamily}`;
    const labelsToMeasure = generateLabels(
      config.data,
      config.denominator,
      {
        unit: config.unit,
        showUnit: config.showUnit,
        decimalPlaces: config.decimalPlaces
      },
      config.outerLabelMaxLength
    );
    formattedLabels = labelsToMeasure.slice(0, -1);
    let maxLabelWidth = 0;
    for (const label of labelsToMeasure) {
      const w = ctx.measureText(label).width;
      if (w > maxLabelWidth) maxLabelWidth = w;
    }
    maxLabelWidth += OUTER_LABEL_CONFIG.labelGap;
    ctx.restore();
    leftRightPadding = Math.max(
      CHART_DIMENSIONS.CANVAS_PADDING,
      Math.ceil(maxLabelWidth)
    );
  }
  return {
    padding: {
      top: topBottomPadding,
      right: leftRightPadding,
      bottom: topBottomPadding,
      left: leftRightPadding
    },
    formattedLabels
  };
}
function calculateFixedHeightChartLayout(config) {
  const isTooSmall = config.fixedHeight < CHART_DIMENSIONS.MIN_HEIGHT_WITH_LABELS;
  const topBottomPadding = CHART_DIMENSIONS.CANVAS_PADDING * 2;
  const chartDiameter = config.isHalfMode ? (config.fixedHeight - topBottomPadding) * 2 : config.fixedHeight - topBottomPadding;
  const visibleWidth = config.isHalfMode ? config.fixedHeight - topBottomPadding : chartDiameter;
  let leftPadding = CHART_DIMENSIONS.CANVAS_PADDING;
  let rightPadding = CHART_DIMENSIONS.CANVAS_PADDING;
  let calculatedWidth = visibleWidth + leftPadding + rightPadding;
  let formattedLabels = [];
  if (config.canvasEl && config.showOuterLabels && !isTooSmall) {
    const ctx = config.canvasEl.getContext("2d");
    if (ctx) {
      const layout = calculateChartPaddingAndLabels(
        config.host,
        ctx,
        config.showOuterLabels,
        config.layoutConfig
      );
      leftPadding = layout.padding.left;
      rightPadding = layout.padding.right;
      calculatedWidth = leftPadding + visibleWidth + rightPadding;
      formattedLabels = layout.formattedLabels;
    }
  } else if (!isTooSmall && config.canvasEl) {
    const ctx = config.canvasEl.getContext("2d");
    if (ctx) {
      const layout = calculateChartPaddingAndLabels(
        config.host,
        ctx,
        config.showOuterLabels,
        config.layoutConfig
      );
      formattedLabels = layout.formattedLabels;
    }
  }
  const actualHeight = config.isHalfMode ? (config.fixedHeight - topBottomPadding) / 2 + topBottomPadding : config.fixedHeight;
  const aspectRatio = calculatedWidth / actualHeight;
  return {
    isTooSmall,
    chartDiameter,
    visibleWidth,
    actualHeight,
    calculatedWidth,
    leftPadding,
    rightPadding,
    topPadding: CHART_DIMENSIONS.CANVAS_PADDING,
    bottomPadding: CHART_DIMENSIONS.CANVAS_PADDING,
    aspectRatio,
    formattedLabels
  };
}
export {
  calculateChartPaddingAndLabels,
  calculateFixedHeightChartLayout,
  formatNumericValue,
  formatSingleLabel
};
//# sourceMappingURL=canvas-layout.js.map
