import { svg } from "lit";
var RotType = /* @__PURE__ */ ((RotType2) => {
  RotType2["dots"] = "dots";
  RotType2["bar"] = "bar";
  return RotType2;
})(RotType || {});
var RotPosition = /* @__PURE__ */ ((RotPosition2) => {
  RotPosition2["scale"] = "scale";
  RotPosition2["innerCircle"] = "innerCircle";
  return RotPosition2;
})(RotPosition || {});
const DOT_COUNT = 5;
const DOT_ANGLES = Array.from(
  { length: DOT_COUNT },
  (_, i) => 360 / DOT_COUNT * i
);
const SCALE_RADIUS = 172;
const INNER_CIRCLE_RADIUS = 100;
const BAR_HALF_THICKNESS = 8;
const DOT_RADIUS = BAR_HALF_THICKNESS;
const BAR_DOT_RADIUS = BAR_HALF_THICKNESS * 0.75;
const BAR_DOT_STROKE = BAR_HALF_THICKNESS * 0.5;
const BAR_DOT_INNER_RADIUS = BAR_DOT_RADIUS - BAR_DOT_STROKE / 2;
function getTrackRadius(position, radiusOffset = 0) {
  const base = position === "scale" ? SCALE_RADIUS : INNER_CIRCLE_RADIUS;
  return base + radiusOffset;
}
function dotOnTrack(angle, radius) {
  const rad = angle * Math.PI / 180;
  return {
    cx: Math.sin(rad) * radius,
    cy: -Math.cos(rad) * radius
  };
}
function renderRotDots(color, position, radiusOffset = 0) {
  const radius = getTrackRadius(position, radiusOffset);
  return svg`${DOT_ANGLES.map((angle) => {
    const { cx, cy } = dotOnTrack(angle, radius);
    return svg`<circle cx="${cx}" cy="${cy}" r="${DOT_RADIUS}" fill="${color}" />`;
  })}`;
}
function rotBarPath(startAngle, endAngle, trackRadius) {
  const R = trackRadius + BAR_HALF_THICKNESS;
  const r = trackRadius - BAR_HALF_THICKNESS;
  const a1 = startAngle * Math.PI / 180;
  const a2 = endAngle * Math.PI / 180;
  const outerStartX = Math.sin(a1) * R;
  const outerStartY = -Math.cos(a1) * R;
  const innerStartX = Math.sin(a1) * r;
  const innerStartY = -Math.cos(a1) * r;
  const outerEndX = Math.sin(a2) * R;
  const outerEndY = -Math.cos(a2) * R;
  const innerEndX = Math.sin(a2) * r;
  const innerEndY = -Math.cos(a2) * r;
  const cwSpan = ((a2 - a1) % (2 * Math.PI) + 2 * Math.PI) % (2 * Math.PI);
  let outerSweep;
  let innerSweep;
  if (cwSpan <= Math.PI) {
    outerSweep = 1;
    innerSweep = 0;
  } else {
    outerSweep = 0;
    innerSweep = 1;
  }
  return [
    `M ${outerStartX} ${outerStartY}`,
    `A ${R} ${R} 0 0 ${outerSweep} ${outerEndX} ${outerEndY}`,
    `L ${innerEndX} ${innerEndY}`,
    `A ${r} ${r} 0 0 ${innerSweep} ${innerStartX} ${innerStartY}`,
    `Z`
  ].join(" ");
}
function shortestAngularDeltaDeg(startAngle, endAngle) {
  const cw = ((endAngle - startAngle) % 360 + 360) % 360;
  return cw <= 180 ? cw : 360 - cw;
}
function renderRotBarStatic(config) {
  const {
    startAngle,
    endAngle,
    color,
    barColor,
    position,
    endDotFill = "var(--instrument-frame-primary-color)",
    endDotStroke = color,
    maskId = "rot-bar-mask",
    radiusOffset = 0
  } = config;
  if (shortestAngularDeltaDeg(startAngle, endAngle) < 0.1) {
    return svg``;
  }
  const trackRadius = getTrackRadius(position, radiusOffset);
  const arcPath = rotBarPath(startAngle, endAngle, trackRadius);
  const { cx: endCx, cy: endCy } = dotOnTrack(endAngle, trackRadius);
  const dotAngularPad = Math.asin(BAR_DOT_INNER_RADIUS / trackRadius) * 180 / Math.PI;
  const cwSpan = ((endAngle - startAngle) % 360 + 360) % 360;
  const isCW = cwSpan <= 180;
  const clipPath = rotBarPath(
    startAngle,
    endAngle + (isCW ? dotAngularPad : -dotAngularPad),
    trackRadius
  );
  return svg`
    <defs>
      <clipPath id="${maskId}">
        <path d="${clipPath}" />
      </clipPath>
    </defs>
    <path d="${arcPath}" fill="${barColor}" />
    <circle
      cx="${endCx}" cy="${endCy}" r="${BAR_DOT_RADIUS}"
      fill="${endDotFill}"
      stroke="${endDotStroke}"
      stroke-width="${BAR_DOT_STROKE}"
    />
  `;
}
function renderRotBarDots(color, position, radiusOffset = 0) {
  const radius = getTrackRadius(position, radiusOffset);
  return svg`
    ${DOT_ANGLES.map((angle) => {
    const { cx, cy } = dotOnTrack(angle, radius);
    return svg`<circle cx="${cx}" cy="${cy}" r="${BAR_DOT_INNER_RADIUS}" fill="${color}" />`;
  })}
  `;
}
const LINEAR_DOT_ANGLE_SPACING = 360 / DOT_COUNT;
const LINEAR_DOT_RADIUS = BAR_HALF_THICKNESS / 2;
var LinearRotPosition = /* @__PURE__ */ ((LinearRotPosition2) => {
  LinearRotPosition2["track"] = "track";
  LinearRotPosition2["scale"] = "scale";
  return LinearRotPosition2;
})(LinearRotPosition || {});
function linearDotStrip(color, trackY, dotSpacing, visibleWidth, r) {
  if (dotSpacing <= 0) return svg``;
  const cyclePx = DOT_COUNT * dotSpacing;
  const halfRange = visibleWidth / 2 + cyclePx;
  const firstIdx = Math.floor(-halfRange / dotSpacing);
  const lastIdx = Math.ceil(halfRange / dotSpacing);
  const dots = [];
  for (let i = firstIdx; i <= lastIdx; i++) {
    dots.push(
      svg`<circle cx="${i * dotSpacing}" cy="${trackY}" r="${r}" fill="${color}" />`
    );
  }
  return svg`${dots}`;
}
function renderLinearRotDots(color, trackY, dotSpacing, visibleWidth) {
  return linearDotStrip(
    color,
    trackY,
    dotSpacing,
    visibleWidth,
    LINEAR_DOT_RADIUS
  );
}
function renderLinearRotBarStatic(config) {
  const {
    startX,
    endX,
    color,
    barColor,
    trackY,
    endDotFill = "var(--instrument-frame-primary-color)",
    endDotStroke = color,
    maskId = "rot-bar-mask-linear"
  } = config;
  if (Math.abs(endX - startX) < 1) return svg``;
  const h = BAR_HALF_THICKNESS;
  const top = trackY - h;
  const bot = trackY + h;
  const isEndRight = endX >= startX;
  const flatX = startX;
  const roundX = endX;
  const barPath = isEndRight ? `M ${flatX} ${top} L ${roundX} ${top} A ${h} ${h} 0 0 1 ${roundX} ${bot} L ${flatX} ${bot} Z` : `M ${flatX} ${top} L ${roundX} ${top} A ${h} ${h} 0 0 0 ${roundX} ${bot} L ${flatX} ${bot} Z`;
  return svg`
    <defs>
      <clipPath id="${maskId}">
        <path d="${barPath}" />
      </clipPath>
    </defs>
    <path d="${barPath}" fill="${barColor}" />
    <circle
      cx="${endX}" cy="${trackY}" r="${BAR_DOT_RADIUS}"
      fill="${endDotFill}" stroke="${endDotStroke}"
      stroke-width="${BAR_DOT_STROKE}" />
  `;
}
function renderLinearRotBarDots(color, trackY, dotSpacing, visibleWidth) {
  return linearDotStrip(
    color,
    trackY,
    dotSpacing,
    visibleWidth,
    LINEAR_DOT_RADIUS
  );
}
export {
  LINEAR_DOT_ANGLE_SPACING,
  LinearRotPosition,
  RotPosition,
  RotType,
  renderLinearRotBarDots,
  renderLinearRotBarStatic,
  renderLinearRotDots,
  renderRotBarDots,
  renderRotBarStatic,
  renderRotDots,
  shortestAngularDeltaDeg
};
//# sourceMappingURL=rot-renderer.js.map
