function computeZoomToFitArcFrame(options) {
  const {
    areas,
    outerRadius,
    innerRadius,
    extension,
    targetSize,
    margin = 0.06
  } = options;
  if (areas.length === 0) {
    const side2 = targetSize;
    return {
      radiusOffset: 0,
      x: -side2 / 2,
      y: -side2 / 2,
      width: side2,
      height: side2,
      viewBox: `${-side2 / 2} ${-side2 / 2} ${side2} ${side2}`
    };
  }
  const available = targetSize * (1 - 2 * margin);
  const measureSize = (radiusOffset2) => {
    const bbox2 = computeAnnularArcBBox(
      areas,
      outerRadius + radiusOffset2,
      innerRadius + radiusOffset2,
      extension
    );
    return Math.max(bbox2.xMax - bbox2.xMin, bbox2.yMax - bbox2.yMin);
  };
  let lo = 0;
  let hi = targetSize;
  for (let i = 0; i < 16 && measureSize(hi) < available; i++) {
    hi *= 2;
  }
  for (let i = 0; i < 50; i++) {
    const mid = (lo + hi) / 2;
    const size = measureSize(mid);
    if (size < available) {
      lo = mid;
    } else {
      hi = mid;
    }
  }
  const radiusOffset = Math.max(0, lo);
  const bbox = computeAnnularArcBBox(
    areas,
    outerRadius + radiusOffset,
    innerRadius + radiusOffset,
    extension
  );
  const rawW = bbox.xMax - bbox.xMin;
  const rawH = bbox.yMax - bbox.yMin;
  const side = Math.max(rawW, rawH);
  const padded = side * (1 + margin * 2);
  const cx = (bbox.xMin + bbox.xMax) / 2;
  const cy = (bbox.yMin + bbox.yMax) / 2;
  const x = round4(cx - padded / 2);
  const y = round4(cy - padded / 2);
  const w = round4(padded);
  const h = round4(padded);
  return {
    radiusOffset,
    x,
    y,
    width: w,
    height: h,
    viewBox: `${x} ${y} ${w} ${h}`
  };
}
function computeAnnularArcBBox(areas, outerRadius, innerRadius, extension) {
  const R_vis = outerRadius + extension;
  const R_in = innerRadius;
  let xMin = Infinity;
  let xMax = -Infinity;
  let yMin = Infinity;
  let yMax = -Infinity;
  const expand = (x, y) => {
    if (x < xMin) xMin = x;
    if (x > xMax) xMax = x;
    if (y < yMin) yMin = y;
    if (y > yMax) yMax = y;
  };
  for (const area of areas) {
    const startRad = area.startAngle * Math.PI / 180;
    const endRad = area.endAngle * Math.PI / 180;
    for (const R of [R_vis, R_in]) {
      expand(R * Math.sin(startRad), -R * Math.cos(startRad));
      expand(R * Math.sin(endRad), -R * Math.cos(endRad));
    }
    const startNorm = (area.startAngle % 360 + 360) % 360;
    const endNorm = (area.endAngle % 360 + 360) % 360;
    const axes = [0, 90, 180, 270];
    for (const axis of axes) {
      if (arcContainsAngle(startNorm, endNorm, axis)) {
        const axisRad = axis * Math.PI / 180;
        for (const R of [R_vis, R_in]) {
          expand(R * Math.sin(axisRad), -R * Math.cos(axisRad));
        }
      }
    }
  }
  if (xMin === Infinity) {
    return { xMin: 0, xMax: 0, yMin: 0, yMax: 0 };
  }
  return { xMin, xMax, yMin, yMax };
}
function arcContainsAngle(startDeg, endDeg, testDeg) {
  const s = (startDeg % 360 + 360) % 360;
  const e = (endDeg % 360 + 360) % 360;
  const t = (testDeg % 360 + 360) % 360;
  if (s <= e) {
    return t >= s && t <= e;
  }
  return t >= s || t <= e;
}
function round4(n) {
  return Math.round(n * 1e4) / 1e4;
}
export {
  computeZoomToFitArcFrame
};
//# sourceMappingURL=arc-frame.js.map
