"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 LogPayload_exports = {};
__export(LogPayload_exports, {
  isLogPayload: () => isLogPayload,
  logBuffer: () => logBuffer,
  logDict: () => logDict,
  logList: () => logList,
  logPayloadBrand: () => logPayloadBrand,
  logText: () => logText,
  mergeLogDict: () => mergeLogDict,
  toLogPayload: () => toLogPayload
});
module.exports = __toCommonJS(LogPayload_exports);
var import_shared = require("@zwave-js/shared");
const logPayloadBrand = Symbol.for("@zwave-js/core:LogPayload");
function isLogPayload(value) {
  return typeof value === "object" && value != null && logPayloadBrand in value;
}
__name(isLogPayload, "isLogPayload");
function logText(lines, options) {
  return {
    [logPayloadBrand]: true,
    type: "text",
    tags: options?.tags,
    lines: typeof lines === "string" ? lines ? lines.split("\n") : [] : lines,
    nested: options?.nested
  };
}
__name(logText, "logText");
function logDict(entries, nested) {
  return {
    [logPayloadBrand]: true,
    type: "dict",
    entries: Object.entries(entries).filter(([, value]) => value !== void 0).map(([key, value]) => [
      key,
      typeof value === "object" ? value : String(value)
    ]),
    nested
  };
}
__name(logDict, "logDict");
function logList(items) {
  return {
    [logPayloadBrand]: true,
    type: "list",
    items: [...items].map(String)
  };
}
__name(logList, "logList");
function asNestedArray(nested) {
  if (nested == void 0)
    return [];
  return Array.isArray(nested) ? nested : [nested];
}
__name(asNestedArray, "asNestedArray");
function logBuffer(buffer) {
  if (buffer.length === 0)
    return void 0;
  return (0, import_shared.buffer2hex)(buffer);
}
__name(logBuffer, "logBuffer");
function toLogPayload(message) {
  return isLogPayload(message) ? message : logDict(message);
}
__name(toLogPayload, "toLogPayload");
function mergeLogDict(...sources) {
  const entries = [];
  let nested;
  for (const source of sources) {
    if (source == void 0)
      continue;
    let raw;
    if (isLogPayload(source)) {
      if (source.type !== "dict") {
        nested = [...asNestedArray(nested), source];
        continue;
      }
      if (source.nested)
        nested = source.nested;
      raw = source.entries;
    } else {
      raw = Object.entries(source);
    }
    for (const [key, value] of raw) {
      const index = entries.findIndex(([k]) => k === key);
      if (value === void 0) {
        if (index >= 0)
          entries.splice(index, 1);
        continue;
      }
      const entry = [
        key,
        typeof value === "object" ? value : String(value)
      ];
      if (index >= 0) {
        entries[index] = entry;
      } else {
        entries.push(entry);
      }
    }
  }
  return { [logPayloadBrand]: true, type: "dict", entries, nested };
}
__name(mergeLogDict, "mergeLogDict");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  isLogPayload,
  logBuffer,
  logDict,
  logList,
  logPayloadBrand,
  logText,
  mergeLogDict,
  toLogPayload
});
//# sourceMappingURL=LogPayload.js.map
