"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 String_exports = {};
__export(String_exports, {
  asJson: () => asJson,
  describeList: () => describeList,
  hex: () => hex,
  serializeToJs: () => serializeToJs
});
module.exports = __toCommonJS(String_exports);
var import_MatterError = require("#MatterError.js");
var import_Bytes = require("./Bytes.js");
var import_Type = require("./Type.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function asJson(value, space) {
  return JSON.stringify(
    value,
    (_key, value2) => {
      if (typeof value2 === "bigint") {
        return value2.toString();
      }
      if (import_Bytes.Bytes.isBytes(value2)) {
        return import_Bytes.Bytes.toHex(value2);
      }
      return value2;
    },
    space
  );
}
function describeList(setType, ...entries) {
  const text = Array();
  if (entries.length === 1) {
    return entries[0];
  }
  for (let i = 0; i < entries.length; i++) {
    if (i === entries.length - 1) {
      text.push(setType, entries[i]);
    } else if (i === entries.length - 2) {
      text.push(entries[i]);
    } else {
      text.push(`${entries[i]},`);
    }
  }
  return text.join(" ");
}
function serializeToJs(value) {
  switch (typeof value) {
    case "bigint":
      return `0x${value.toString(16)}n`;
    case "boolean":
      return `${value}`;
    case "number":
      if (Number.isSafeInteger(value) && value > 10) {
        return `0x${value.toString(16)}`;
      }
      return `${value}`;
    case "string":
      return JSON.stringify(value);
    case "undefined":
      return "undefined";
    case "function":
    case "symbol":
      throw new import_MatterError.ImplementationError(`Cannot serialize a ${typeof value}`);
  }
  if (value instanceof Date) {
    return `new Date(${JSON.stringify(value.toISOString)})`;
  }
  if (import_Bytes.Bytes.isBytes(value)) {
    return `b$\`${import_Bytes.Bytes.toHex(value)}\``;
  }
  if (value === null) {
    return "null";
  }
  if (Array.isArray(value)) {
    const parts2 = ["["];
    for (const item of value) {
      parts2.push(serializeToJs(item), ",");
    }
    parts2.push("]");
    return parts2.join("");
  }
  if (!(0, import_Type.isObject)(value)) {
    throw new import_MatterError.ImplementationError(`Cannot serialize a ${value.constructor.name}`);
  }
  const parts = ["{"];
  for (const key in value) {
    parts.push(key, ":", serializeToJs(value[key]), ",");
  }
  parts.push("}");
  return parts.join("");
}
function hex(number) {
  return number.toString(16);
}
((hex2) => {
  function fixed(value, width) {
    return hex2(value).padStart(width, "0");
  }
  hex2.fixed = fixed;
  function byte(value) {
    return fixed(value, 2);
  }
  hex2.byte = byte;
  function word(value) {
    return fixed(value, 4);
  }
  hex2.word = word;
})(hex || (hex = {}));
//# sourceMappingURL=String.js.map
