"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 common_exports = {};
__export(common_exports, {
  deserializeBuffer: () => deserializeBuffer,
  deserializeBufferInArray: () => deserializeBufferInArray,
  firmwareUpdateOutgoingMessage: () => firmwareUpdateOutgoingMessage,
  getFirmwareUpdateOptions: () => getFirmwareUpdateOptions,
  getRawConfigParameterValue: () => getRawConfigParameterValue,
  isBufferObject: () => isBufferObject,
  parseAndExtractFirmware: () => parseAndExtractFirmware,
  setRawConfigParameterValue: () => setRawConfigParameterValue,
  setValueOutgoingMessage: () => setValueOutgoingMessage
});
module.exports = __toCommonJS(common_exports);
var import_zwave_js = require("zwave-js");
var import_core = require("@zwave-js/core");
var import_error = require("./error.js");
function isBufferObject(obj) {
  return obj instanceof Object && Object.keys(obj).length === 2 && "type" in obj && obj.type === "Buffer" && "data" in obj && Array.isArray(obj.data) && obj.data.every((item) => typeof item === "number");
}
__name(isBufferObject, "isBufferObject");
function deserializeBufferInArray(array) {
  for (let idx = 0; idx < array.length; idx++) {
    const value = array[idx];
    if (isBufferObject(value)) {
      array[idx] = Buffer.from(value.data);
    }
  }
  return array;
}
__name(deserializeBufferInArray, "deserializeBufferInArray");
function deserializeBuffer(value) {
  if (isBufferObject(value)) {
    return Buffer.from(value.data);
  }
  return value;
}
__name(deserializeBuffer, "deserializeBuffer");
function setValueOutgoingMessage(result, schemaVersion) {
  if (schemaVersion < 29) {
    return {
      success: [
        import_zwave_js.SetValueStatus.Working,
        import_zwave_js.SetValueStatus.Success,
        import_zwave_js.SetValueStatus.SuccessUnsupervised
      ].includes(result.status)
    };
  }
  return { result };
}
__name(setValueOutgoingMessage, "setValueOutgoingMessage");
function firmwareUpdateOutgoingMessage(result, schemaVersion) {
  if (schemaVersion < 29) {
    return {
      success: result.success
    };
  }
  return { result };
}
__name(firmwareUpdateOutgoingMessage, "firmwareUpdateOutgoingMessage");
async function setRawConfigParameterValue(message, nodeOrEndpoint) {
  if (message.valueSize !== void 0 && message.valueFormat === void 0 || message.valueSize === void 0 && message.valueFormat !== void 0) {
    throw new import_error.InvalidParamsPassedToCommandError("valueFormat and valueSize must be used in combination or not at all");
  }
  if (message.valueSize !== void 0 && message.bitMask !== void 0) {
    throw new import_error.InvalidParamsPassedToCommandError("bitMask cannot be used in combination with valueFormat and valueSize");
  }
  let options;
  if (message.bitMask !== void 0) {
    options = {
      parameter: message.parameter,
      bitMask: message.bitMask,
      value: message.value
    };
  } else {
    options = {
      parameter: message.parameter,
      valueFormat: message.valueFormat,
      valueSize: message.valueSize,
      value: message.value
    };
  }
  const result = await nodeOrEndpoint.commandClasses.Configuration.set(options);
  return { result };
}
__name(setRawConfigParameterValue, "setRawConfigParameterValue");
async function getRawConfigParameterValue(message, nodeOrEndpoint) {
  const value = await nodeOrEndpoint.commandClasses.Configuration.get(message.parameter, {
    valueBitMask: message.bitMask
  });
  return { value };
}
__name(getRawConfigParameterValue, "getRawConfigParameterValue");
function parseFirmwareFile(filename, rawData, explicitFormat) {
  const unzipped = (0, import_core.tryUnzipFirmwareFile)(rawData);
  if (unzipped) {
    return explicitFormat !== void 0 ? { rawData: unzipped.rawData, format: explicitFormat } : unzipped;
  }
  if (explicitFormat !== void 0) {
    return { rawData, format: explicitFormat };
  }
  return { rawData, format: (0, import_core.guessFirmwareFileFormat)(filename, rawData) };
}
__name(parseFirmwareFile, "parseFirmwareFile");
async function parseAndExtractFirmware(filename, rawData, explicitFormat) {
  const parsed = parseFirmwareFile(filename, rawData, explicitFormat);
  return (0, import_core.extractFirmware)(parsed.rawData, parsed.format);
}
__name(parseAndExtractFirmware, "parseAndExtractFirmware");
function getFirmwareUpdateOptions(message, client) {
  return {
    apiKey: message.apiKey,
    additionalUserAgentComponents: client.additionalUserAgentComponents,
    includePrereleases: message.includePrereleases,
    rfRegion: message.rfRegion
  };
}
__name(getFirmwareUpdateOptions, "getFirmwareUpdateOptions");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  deserializeBuffer,
  deserializeBufferInArray,
  firmwareUpdateOutgoingMessage,
  getFirmwareUpdateOptions,
  getRawConfigParameterValue,
  isBufferObject,
  parseAndExtractFirmware,
  setRawConfigParameterValue,
  setValueOutgoingMessage
});
//# sourceMappingURL=common.js.map
