"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var NoiseFrameParser_exports = {};
__export(NoiseFrameParser_exports, {
  NoiseFrameParser: () => NoiseFrameParser
});
module.exports = __toCommonJS(NoiseFrameParser_exports);
var import_core = require("@zwave-js/core");
var import_shared = require("@zwave-js/shared");
var import_NoiseProtocol = require("../noise/NoiseProtocol.js");
class NoiseFrameParserTransformer {
  static {
    __name(this, "NoiseFrameParserTransformer");
  }
  receiveBuffer = new import_shared.Bytes();
  transform(chunk, controller) {
    this.receiveBuffer = import_shared.Bytes.concat([this.receiveBuffer, chunk]);
    while (this.receiveBuffer.length > 0) {
      try {
        if (this.receiveBuffer.length < 3) {
          break;
        }
        if (this.receiveBuffer[0] !== import_NoiseProtocol.NOISE_INDICATOR) {
          throw new import_core.ZWaveError(`Invalid frame indicator: expected 0x01, got 0x${this.receiveBuffer[0].toString(16).padStart(2, "0")}`, import_core.ZWaveErrorCodes.PacketFormat_Invalid);
        }
        const { payload, bytesRead } = (0, import_NoiseProtocol.decodeNoiseFrame)(this.receiveBuffer);
        controller.enqueue(payload);
        this.receiveBuffer = this.receiveBuffer.subarray(bytesRead);
      } catch (error) {
        if (error instanceof import_core.ZWaveError && error.code === import_core.ZWaveErrorCodes.PacketFormat_Truncated) {
          break;
        }
        this.receiveBuffer = new import_shared.Bytes();
        break;
      }
    }
  }
}
class NoiseFrameParser extends TransformStream {
  static {
    __name(this, "NoiseFrameParser");
  }
  constructor() {
    super(new NoiseFrameParserTransformer());
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  NoiseFrameParser
});
//# sourceMappingURL=NoiseFrameParser.js.map
