"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 CRC16CC_exports = {};
__export(CRC16CC_exports, {
  CRC16CC: () => CRC16CC,
  CRC16CCAPI: () => CRC16CCAPI,
  CRC16CCCommandEncapsulation: () => CRC16CCCommandEncapsulation
});
module.exports = __toCommonJS(CRC16CC_exports);
var import_tslib = require("tslib");
var import_core = require("@zwave-js/core");
var import_API = require("../lib/API.js");
var import_CommandClass = require("../lib/CommandClass.js");
var import_CommandClassDecorators = require("../lib/CommandClassDecorators.js");
var import_shared = require("@zwave-js/shared");
var import_Types = require("../lib/_Types.js");
const headerBuffer = import_shared.Bytes.from([
  import_core.CommandClasses["CRC-16 Encapsulation"],
  import_Types.CRC16Command.CommandEncapsulation
]);
let CRC16CCAPI = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.API)(import_core.CommandClasses["CRC-16 Encapsulation"])];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = import_API.CCAPI;
  var CRC16CCAPI2 = class extends _classSuper {
    static {
      __name(this, "CRC16CCAPI");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      CRC16CCAPI2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
    supportsCommand(_cmd) {
      return true;
    }
    async sendEncapsulated(encapsulatedCC) {
      this.assertSupportsCommand(import_Types.CRC16Command, import_Types.CRC16Command.CommandEncapsulation);
      const cc = new CRC16CCCommandEncapsulation({
        nodeId: this.endpoint.nodeId,
        endpointIndex: this.endpoint.index,
        encapsulated: encapsulatedCC
      });
      await this.host.sendCommand(cc, this.commandOptions);
    }
  };
  return CRC16CCAPI2 = _classThis;
})();
let CRC16CC = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.commandClass)(import_core.CommandClasses["CRC-16 Encapsulation"]), (0, import_CommandClassDecorators.implementedVersion)(1)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = import_CommandClass.CommandClass;
  var CRC16CC2 = class extends _classSuper {
    static {
      __name(this, "CRC16CC");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      CRC16CC2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
    /** Tests if a command should be supervised and thus requires encapsulation */
    static requiresEncapsulation(cc) {
      return !!(cc.encapsulationFlags & import_core.EncapsulationFlags.CRC16) && !(cc instanceof CRC16CCCommandEncapsulation);
    }
    /** Encapsulates a command in a CRC-16 CC */
    static encapsulate(cc) {
      const ret = new CRC16CCCommandEncapsulation({
        nodeId: cc.nodeId,
        encapsulated: cc
      });
      ret.encapsulationFlags = cc.encapsulationFlags & ~import_core.EncapsulationFlags.CRC16;
      return ret;
    }
  };
  return CRC16CC2 = _classThis;
})();
function getCCResponseForCommandEncapsulation(ctx, sent) {
  if (sent.encapsulated?.expectsCCResponse(ctx)) {
    return CRC16CCCommandEncapsulation;
  }
}
__name(getCCResponseForCommandEncapsulation, "getCCResponseForCommandEncapsulation");
let CRC16CCCommandEncapsulation = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.CRC16Command.CommandEncapsulation), (0, import_CommandClassDecorators.expectedCCResponse)(getCCResponseForCommandEncapsulation, () => "checkEncapsulated")];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = CRC16CC;
  var CRC16CCCommandEncapsulation2 = class extends _classSuper {
    static {
      __name(this, "CRC16CCCommandEncapsulation");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      CRC16CCCommandEncapsulation2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
    constructor(options) {
      super(options);
      this.encapsulated = options.encapsulated;
      this.encapsulated.encapsulatingCC = this;
    }
    static async from(raw, ctx) {
      (0, import_core.validatePayload)(raw.payload.length >= 3);
      const ccBuffer = raw.payload.subarray(0, -2);
      let expectedCRC = (0, import_core.CRC16_CCITT)(headerBuffer);
      expectedCRC = (0, import_core.CRC16_CCITT)(ccBuffer, expectedCRC);
      const actualCRC = raw.payload.readUInt16BE(raw.payload.length - 2);
      (0, import_core.validatePayload)(expectedCRC === actualCRC);
      const encapsulated = await import_CommandClass.CommandClass.parse(ccBuffer, ctx);
      return new this({
        nodeId: ctx.sourceNodeId,
        encapsulated
      });
    }
    encapsulated;
    async serialize(ctx) {
      const commandBuffer = await this.encapsulated.serialize(ctx);
      this.payload = import_shared.Bytes.concat([commandBuffer, new import_shared.Bytes(2)]);
      let crc = (0, import_core.CRC16_CCITT)(headerBuffer);
      crc = (0, import_core.CRC16_CCITT)(commandBuffer, crc);
      this.payload.writeUInt16BE(crc, this.payload.length - 2);
      return super.serialize(ctx);
    }
    computeEncapsulationOverhead() {
      return super.computeEncapsulationOverhead() + 2;
    }
    toLogEntry(ctx) {
      return {
        ...super.toLogEntry(ctx),
        // Hide the default payload line
        message: void 0
      };
    }
  };
  return CRC16CCCommandEncapsulation2 = _classThis;
})();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CRC16CC,
  CRC16CCAPI,
  CRC16CCCommandEncapsulation
});
//# sourceMappingURL=CRC16CC.js.map
