"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 serialize_exports = {};
__export(serialize_exports, {
  serialize: () => serialize
});
module.exports = __toCommonJS(serialize_exports);
var import_Bytes = require("./Bytes.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function serialize(value) {
  const visited = /* @__PURE__ */ new Set();
  function asValidKey(key) {
    if (key.match(/[a-z_$][\w$]*/i)) {
      return key;
    }
    return JSON.stringify(key);
  }
  function serializeOne(value2) {
    if (value2 === void 0) {
      return;
    }
    if (value2 === null) {
      return "null";
    }
    if (value2[serialize.SERIALIZE]) {
      return value2[serialize.SERIALIZE]();
    }
    if (typeof value2 === "function") {
      return;
    }
    if (typeof value2 === "bigint" || value2 instanceof BigInt) {
      return value2.toString();
    }
    if (typeof value2 === "number" || value2 instanceof Number) {
      return value2.toString();
    }
    if (typeof value2 === "string" || value2 instanceof String) {
      return JSON.stringify(value2);
    }
    if (typeof value2 === "boolean") {
      return value2 ? "true" : "false";
    }
    if (import_Bytes.Bytes.isBytes(value2)) {
      return import_Bytes.Bytes.toHex(value2);
    }
    if (visited.has(value2)) {
      return;
    }
    if (value2.toJSON) {
      value2 = JSON.parse(JSON.stringify(value2));
    }
    try {
      visited.add(value2);
      if (Array.isArray(value2)) {
        if (value2.length) {
          return `[ ${value2.map(serializeOne).join(", ")} ]`;
        }
        return "[]";
      }
      const entries = Object.entries(value2).map(([k, v]) => [k, serializeOne(v)]).filter(([_k, v]) => v !== void 0).map(([k, v]) => `${asValidKey(k ?? "")}: ${v}`);
      if (!entries.length) {
        return "{}";
      }
      return `{ ${entries.join(", ")} }`;
    } finally {
      visited.delete(value2);
    }
  }
  return serializeOne(value);
}
((serialize2) => {
  serialize2.SERIALIZE = /* @__PURE__ */ Symbol("SERIALIZE");
  function asIs(value) {
    if (typeof value === "string") {
      value = new String(value);
    }
    if (value !== void 0 && value !== null) {
      value[serialize2.SERIALIZE] = function() {
        return this.toString();
      };
    }
    return value;
  }
  serialize2.asIs = asIs;
  function isPrimitive(value) {
    if (value === void 0 || value === null || value instanceof Date || ArrayBuffer.isView(value) || value[serialize2.SERIALIZE]) {
      return true;
    }
    if (Array.isArray(value)) {
      return false;
    }
    return typeof value !== "object";
  }
  serialize2.isPrimitive = isPrimitive;
})(serialize || (serialize = {}));
//# sourceMappingURL=serialize.js.map
