import { unsafeCSS, LitElement, svg, nothing, html } from "lit";
import { property } from "lit/decorators.js";
import compentStyle from "./main-engine.css.js";
import { InstrumentState, Priority } from "../types.js";
import { atSetpoint, thrusterColors, convertThrustAdvices, thrusterTopSingleSided, renderThrusterSetpoint } from "../thruster/thruster.js";
import { customElement } from "../../decorator.js";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result) __defProp(target, key, result);
  return result;
};
let ObcMainEngine = class extends LitElement {
  constructor() {
    super(...arguments);
    this._thrustSetpointId = `me-thrust-sp-${Math.random().toString(36).slice(2, 9)}`;
    this._speedSetpointId = `me-speed-sp-${Math.random().toString(36).slice(2, 9)}`;
    this.thrust = 0;
    this.thrustTouching = false;
    this.atThrustSetpoint = false;
    this.speed = 0;
    this.speedTouching = false;
    this.atSpeedSetpoint = false;
    this.autoAtThrustSetpoint = true;
    this.autoAtSpeedSetpoint = true;
    this.autoAtThrustSetpointDeadband = 1;
    this.autoAtSpeedSetpointDeadband = 1;
    this.thrustSetpointAtZeroDeadband = 0.5;
    this.speedSetpointAtZeroDeadband = 0.5;
    this.state = InstrumentState.active;
    this.priority = Priority.regular;
    this.thrustAdvices = [];
  }
  render() {
    const thrustAtSetpoint = atSetpoint(this.thrust, this.thrustSetpoint, {
      atSetpoint: this.atThrustSetpoint,
      autoAtSetpoint: this.autoAtThrustSetpoint,
      autoAtSetpointDeadband: this.autoAtThrustSetpointDeadband,
      touching: this.thrustTouching
    });
    const cThrust = thrusterColors(
      {
        atSetpoint: thrustAtSetpoint,
        touching: this.thrustTouching
      },
      this.state,
      this.priority
    );
    const speedAtSetpoint = atSetpoint(this.speed, this.speedSetpoint, {
      atSetpoint: this.atSpeedSetpoint,
      autoAtSetpoint: this.autoAtSpeedSetpoint,
      autoAtSetpointDeadband: this.autoAtSpeedSetpointDeadband,
      touching: this.speedTouching
    });
    const cSpeed = thrusterColors(
      {
        atSetpoint: speedAtSetpoint,
        touching: this.speedTouching
      },
      this.state,
      this.priority
    );
    const container = svg`<rect x="-80" y="-176" width="160" height="352" fill="var(--instrument-frame-primary-color)" stroke="var(--instrument-frame-tertiary-color)" rx="8"/>`;
    const border = svg`<rect x="-80" y="-176" width="160" height="352" fill="none" stroke="var(--instrument-frame-tertiary-color)" rx="8" vector-effect="non-scaling-stroke"/>`;
    const frameLeft = svg`<rect x="-56" y="-176" width="48" height="352" fill="var(--instrument-frame-secondary-color)" vector-effect="non-scaling-stroke" stroke="var(--instrument-frame-secondary-color)"/>`;
    const frameRight = svg`<rect x="8" y="-176" width="48" height="352" fill="var(--instrument-frame-secondary-color)" vector-effect="non-scaling-stroke" stroke="var(--instrument-frame-secondary-color)"/>`;
    const thrustCenter = svg`<rect x="8" y="-2" height="4" width="72" fill="${cThrust.zeroLineColor}" stroke=${cThrust.zeroLineColor} vector-effect="non-scaling-stroke"/>`;
    const { topAdvices: topThrustAdvice, bottomAdvices: bottomThrustAdvice } = convertThrustAdvices(this.thrustAdvices, this.thrustSetpoint);
    const thrustTop = svg`<g transform="translate(44, 0)">
      ${thrusterTopSingleSided(
      174,
      Math.max(this.thrust, 0),
      { box: cThrust.boxColor, container: "" },
      {
        hideContainer: true,
        hideTicks: cThrust.hideTicks,
        flipAdicePattern: false,
        narrow: false,
        off: this.state === InstrumentState.off
      },
      topThrustAdvice
    )}</g>`;
    const thrusterBottom = svg`<g transform="rotate(180) scale(-1,1) translate(44)">
     ${thrusterTopSingleSided(
      174,
      Math.max(-this.thrust, 0),
      { box: cThrust.boxColor, container: "" },
      {
        hideContainer: true,
        hideTicks: cThrust.hideTicks,
        flipAdicePattern: false,
        narrow: false,
        off: this.state === InstrumentState.off
      },
      bottomThrustAdvice
    )}</g>`;
    const thrustSetpoint = this.thrustSetpoint !== void 0 ? svg`<g transform="translate(44, 0)">${renderThrusterSetpoint(
      174,
      this.thrustSetpoint,
      {
        state: this.state,
        priority: this.priority,
        atSetpoint: thrustAtSetpoint,
        touching: this.thrustTouching,
        setpointAtZeroDeadband: this.thrustSetpointAtZeroDeadband,
        singleSided: true,
        narrow: false,
        id: this._thrustSetpointId
      }
    )}</g>` : nothing;
    const speedHeight = 352 * (this.speed / 100) + 2;
    const speedY = 176 - speedHeight;
    const speedBoxColor = this.priority === Priority.enhanced ? "var(--instrument-enhanced-tertiary-color)" : "var(--instrument-regular-tertiary-color)";
    const speedBox = svg`<rect x="-56" y=${speedY} width="48" height=${speedHeight} fill=${speedBoxColor} stroke=${speedBoxColor} vector-effect="non-scaling-stroke">`;
    const speedLine = svg`<rect x="-56" y=${speedY - 2} width="48" height="4" rx="2" fill=${cSpeed.boxColor} stroke=${cSpeed.boxColor}/>
`;
    const speedSetpoint = this.speedSetpoint !== void 0 ? svg`<g transform="scale(-1 1) translate(44, 176)">${renderThrusterSetpoint(
      350,
      this.speedSetpoint,
      {
        state: this.state,
        priority: this.priority,
        atSetpoint: speedAtSetpoint,
        touching: this.speedTouching,
        setpointAtZeroDeadband: this.speedSetpointAtZeroDeadband,
        singleSided: true,
        narrow: false,
        id: this._speedSetpointId
      }
    )}</g>` : nothing;
    return html`
      <div class="container">
        <svg viewbox="-100 -200 200 400">
          ${container} ${frameLeft} ${frameRight} ${thrustCenter} ${thrustTop}
          ${thrusterBottom} ${speedBox} ${speedLine} ${border} ${thrustSetpoint}
          ${speedSetpoint}
        </svg>
      </div>
    `;
  }
};
ObcMainEngine.styles = unsafeCSS(compentStyle);
__decorateClass([
  property({ type: Number })
], ObcMainEngine.prototype, "thrust", 2);
__decorateClass([
  property({ type: Number })
], ObcMainEngine.prototype, "thrustSetpoint", 2);
__decorateClass([
  property({ type: Boolean })
], ObcMainEngine.prototype, "thrustTouching", 2);
__decorateClass([
  property({ type: Boolean })
], ObcMainEngine.prototype, "atThrustSetpoint", 2);
__decorateClass([
  property({ type: Number })
], ObcMainEngine.prototype, "speed", 2);
__decorateClass([
  property({ type: Number })
], ObcMainEngine.prototype, "speedSetpoint", 2);
__decorateClass([
  property({ type: Boolean })
], ObcMainEngine.prototype, "speedTouching", 2);
__decorateClass([
  property({ type: Boolean })
], ObcMainEngine.prototype, "atSpeedSetpoint", 2);
__decorateClass([
  property({ type: Boolean, attribute: false })
], ObcMainEngine.prototype, "autoAtThrustSetpoint", 2);
__decorateClass([
  property({ type: Boolean, attribute: false })
], ObcMainEngine.prototype, "autoAtSpeedSetpoint", 2);
__decorateClass([
  property({ type: Number })
], ObcMainEngine.prototype, "autoAtThrustSetpointDeadband", 2);
__decorateClass([
  property({ type: Number })
], ObcMainEngine.prototype, "autoAtSpeedSetpointDeadband", 2);
__decorateClass([
  property({ type: Number })
], ObcMainEngine.prototype, "thrustSetpointAtZeroDeadband", 2);
__decorateClass([
  property({ type: Number })
], ObcMainEngine.prototype, "speedSetpointAtZeroDeadband", 2);
__decorateClass([
  property({ type: String })
], ObcMainEngine.prototype, "state", 2);
__decorateClass([
  property({ type: String })
], ObcMainEngine.prototype, "priority", 2);
__decorateClass([
  property({ type: Array })
], ObcMainEngine.prototype, "thrustAdvices", 2);
ObcMainEngine = __decorateClass([
  customElement("obc-main-engine")
], ObcMainEngine);
export {
  ObcMainEngine
};
//# sourceMappingURL=main-engine.js.map
