import { svg, nothing } from "lit";
import { Priority, InstrumentState } from "../../navigation-instruments/types.js";
import { renderAdvice } from "./advice.js";
function atSetpoint(thrust, setpoint, options) {
  if (options.touching) {
    return false;
  }
  if (options.autoAtSetpoint && setpoint !== void 0) {
    return Math.abs(thrust - setpoint) < options.autoAtSetpointDeadband;
  }
  return options.atSetpoint;
}
function watchfaceLinear({
  height,
  width,
  scaleWidth,
  minValue,
  maxValue
}, box, bar, colors, options, tickmarks, advice) {
  const r = 8;
  const path = `M -${width / 2} 0  V -${height / 2 - 8}  a 8 8 0 0 1 8 -8 h ${width - r * 2} a 8 8 0 0 1 8 8 V ${height / 2 - r} a 8 8 0 0 1 -8 8 h -${width - 2 * r} a 8 8 0 0 1 -8 -8 Z`;
  const container = svg`
      <path d=${path} 
       fill=${colors.container}
       />
  `;
  const w2 = width - scaleWidth - r;
  let track = svg`
      <path d="M -${width / 2} 0  V -${height / 2 - r}  a 8 8 0 0 1 8 -8 h ${w2} V ${height / 2} h -${w2} a 8 8 0 0 1 -8 -8 Z" 
       stroke="var(--instrument-frame-secondary-color)"
       fill="var(--instrument-frame-secondary-color)"
       vector-effect="non-scaling-stroke"
       />
  `;
  const containerStroke = svg`
      <path d=${path} 
      stroke="var(--instrument-frame-tertiary-color)" 
      fill="none" 
      vector-effect="non-scaling-stroke"/>
  `;
  if (options.off) {
    track = nothing;
  }
  const { boxFill, boxStroke, barFill, barStroke } = getColors(
    options.priority === Priority.enhanced
  );
  const tickmarksSvg = [];
  const maskId = "boxMask";
  const mask = options.hideContainer ? nothing : svg`
  <defs>
  <mask id=${maskId}>
  <path d=${path} fill="white" stroke="white" vector-effect="non-scaling-stroke"/>
  </defs>`;
  const maskAttr = options.hideContainer ? void 0 : `url(#${maskId})`;
  const skipValues = [];
  if (tickmarks.mainTickmarks) {
    for (const value of tickmarks.mainTickmarks) {
      if (value < minValue || value > maxValue) continue;
      const y = valueToY(value, minValue, maxValue, height);
      tickmarksSvg.push(
        svg`<line x1=${-width / 2} x2=${width / 2} y1=${y} y2=${y} stroke="var(--instrument-frame-tertiary-color)" stroke-width="1" vector-effect="non-scaling-stroke"/>`
      );
      skipValues.push(value);
    }
  }
  const tickmarksX = width / 2 - scaleWidth + 4;
  if (tickmarks.primaryTickmarkInterval !== void 0 && tickmarks.primaryTickmarkInterval > 0 && Number.isFinite(tickmarks.primaryTickmarkInterval)) {
    const { svgs, values } = generateTickmarks({
      height,
      interval: tickmarks.primaryTickmarkInterval,
      minValue,
      maxValue,
      tickmarksX,
      tickmarksWidth: width / 2 - tickmarksX,
      skipValues
    });
    tickmarksSvg.push(...svgs);
    skipValues.push(...values);
  }
  if (tickmarks.secondaryTickmarkInterval !== void 0 && tickmarks.secondaryTickmarkInterval > 0 && Number.isFinite(tickmarks.secondaryTickmarkInterval)) {
    const { svgs, values } = generateTickmarks({
      height,
      interval: tickmarks.secondaryTickmarkInterval,
      minValue,
      maxValue,
      tickmarksX,
      tickmarksWidth: 8,
      skipValues
    });
    tickmarksSvg.push(...svgs);
    skipValues.push(...values);
  }
  const boxX = -width / 2;
  const boxWidth = width - scaleWidth;
  const boxesSvg = box.map((b) => {
    const minY = valueToY(b.min, minValue, maxValue, height);
    const maxY = valueToY(b.max, minValue, maxValue, height);
    const boxY = Math.min(minY, maxY);
    const boxHeight = Math.abs(maxY - minY);
    return svg`<rect width=${boxWidth} height=${boxHeight} x=${boxX} y=${boxY} fill=${b.fill ?? boxFill} stroke=${b.fill ?? boxStroke} vector-effect="non-scaling-stroke"/>`;
  });
  const advicesSvg = advice.map(
    (a) => renderAdvice(height, minValue, maxValue, width, scaleWidth, a)
  );
  const barSvg = bar ? svg`
<rect x=${boxX} y=${valueToY(bar.value, minValue, maxValue, height) - 4} width=${boxWidth} height="8" rx="4" fill=${barFill} stroke=${barStroke} vector-effect="non-scaling-stroke"/>
` : nothing;
  const all = [
    mask,
    containerStroke,
    svg`<g mask=${maskAttr}>${tickmarksSvg}${boxesSvg} </g>`,
    advicesSvg,
    barSvg
  ];
  if (!options.hideContainer) {
    all.splice(0, 0, [container, track]);
  }
  return all;
}
function valueToY(value, minValue, maxValue, height) {
  const range = maxValue - minValue;
  return (-value + minValue) * height / range + height / 2;
}
function generateTickmarks({
  height,
  interval,
  tickmarksX,
  tickmarksWidth,
  minValue,
  maxValue,
  skipValues
}) {
  const tickmarksSvg = [];
  const values = [];
  for (let v = 0; v < maxValue; v += interval) {
    if (skipValues.includes(v)) {
      continue;
    }
    const y = valueToY(v, minValue, maxValue, height);
    values.push(v);
    tickmarksSvg.push(
      svg`<line x1=${tickmarksX} x2=${tickmarksX + tickmarksWidth} y1=${y} y2=${y} stroke="var(--instrument-frame-tertiary-color)" stroke-width="1" vector-effect="non-scaling-stroke"/>`
    );
  }
  for (let v = -interval; v > minValue; v -= interval) {
    if (skipValues.includes(v)) {
      continue;
    }
    const y = valueToY(v, minValue, maxValue, height);
    values.push(v);
    tickmarksSvg.push(
      svg`<line x1=${tickmarksX} x2=${tickmarksX + tickmarksWidth} y1=${y} y2=${y} stroke="var(--instrument-frame-tertiary-color)" stroke-width="1" vector-effect="non-scaling-stroke"/>`
    );
  }
  return { svgs: tickmarksSvg, values };
}
function getColors(enhanced) {
  if (enhanced) {
    return {
      boxFill: "var(--instrument-enhanced-tertiary-color)",
      boxStroke: "var(--instrument-enhanced-tertiary-color)",
      barFill: "var(--instrument-enhanced-secondary-color)",
      barStroke: "var(--instrument-enhanced-tertiary-color)"
    };
  } else {
    return {
      boxFill: "var(--instrument-regular-tertiary-color)",
      boxStroke: "var(--instrument-regular-tertiary-color)",
      barFill: "var(--instrument-regular-secondary-color)",
      barStroke: "var(--instrument-regular-tertiary-color)"
    };
  }
}
function thrusterColors(options, state, priority = Priority.regular) {
  const isEnhanced = priority === Priority.enhanced;
  let boxColor = isEnhanced ? "var(--instrument-enhanced-secondary-color)" : "var(--instrument-regular-secondary-color)";
  let setPointColor = isEnhanced ? "var(--instrument-enhanced-primary-color)" : "var(--instrument-regular-primary-color)";
  let arrowColor = "var(--instrument-regular-secondary-color)";
  let containerBackgroundColor = "var(--instrument-frame-primary-color)";
  let zeroLineColor = isEnhanced ? "var(--instrument-enhanced-secondary-color)" : "var(--instrument-regular-secondary-color)";
  let hideTicks = false;
  if (options.atSetpoint) {
    setPointColor = boxColor;
  }
  if (state === InstrumentState.loading) {
    boxColor = "transparent";
    setPointColor = "var(--instrument-frame-tertiary-color)";
    zeroLineColor = "var(--instrument-frame-tertiary-color)";
    arrowColor = "var(--instrument-regular-secondary-color)";
    hideTicks = true;
  } else if (state === InstrumentState.off) {
    boxColor = "transparent";
    setPointColor = "var(--instrument-frame-tertiary-color)";
    arrowColor = "var(--instrument-frame-tertiary-color)";
    zeroLineColor = "var(--instrument-frame-tertiary-color)";
    hideTicks = true;
    containerBackgroundColor = "transparent";
  }
  return {
    zeroLineColor,
    boxColor,
    containerBackgroundColor,
    hideTicks,
    setPointColor,
    arrowColor
  };
}
export {
  atSetpoint,
  thrusterColors,
  valueToY,
  watchfaceLinear
};
//# sourceMappingURL=instrument-linear.js.map
