"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 StringifyTools_exports = {};
__export(StringifyTools_exports, {
  fromJson: () => fromJson,
  toJson: () => toJson
});
module.exports = __toCommonJS(StringifyTools_exports);
var import_TimeUnit = require("#time/TimeUnit.js");
var import_MatterError = require("../MatterError.js");
var import_Bytes = require("../util/Bytes.js");
var import_Type = require("../util/Type.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const JSON_SPECIAL_KEY_TYPE = "__object__";
const JSON_SPECIAL_KEY_VALUE = "__value__";
function toJson(object, spaces) {
  return JSON.stringify(
    object,
    (_key, value) => {
      if (value === void 0) {
        return `{"${JSON_SPECIAL_KEY_TYPE}":"Undefined"}`;
      }
      if (typeof value === "bigint") {
        return `{"${JSON_SPECIAL_KEY_TYPE}":"BigInt","${JSON_SPECIAL_KEY_VALUE}":"${value.toString()}"}`;
      }
      if (import_Bytes.Bytes.isBytes(value)) {
        return `{"${JSON_SPECIAL_KEY_TYPE}":"Uint8Array","${JSON_SPECIAL_KEY_VALUE}":"${import_Bytes.Bytes.toHex(value)}"}`;
      }
      if ((0, import_Type.isObject)(value) && value.type === "Buffer" && Array.isArray(value.data)) {
        return `{"${JSON_SPECIAL_KEY_TYPE}":"Uint8Array","${JSON_SPECIAL_KEY_VALUE}":"${import_Bytes.Bytes.toHex(
          Uint8Array.from(value.data)
        )}"}`;
      }
      if (value instanceof Map) {
        return `{"${JSON_SPECIAL_KEY_TYPE}":"Map","${JSON_SPECIAL_KEY_VALUE}":${JSON.stringify(
          toJson(Array.from(value.entries()))
        )}}`;
      }
      return value;
    },
    spaces
  );
}
function fromJson(json) {
  return JSON.parse(json, (_key, value) => {
    if (typeof value === "string" && value.startsWith(`{"${JSON_SPECIAL_KEY_TYPE}":"`) && value.endsWith("}")) {
      const data = JSON.parse(value);
      const object = data[JSON_SPECIAL_KEY_TYPE];
      switch (object) {
        case "Undefined":
          return void 0;
        case "BigInt":
          return BigInt(data[JSON_SPECIAL_KEY_VALUE]);
        case "Uint8Array":
          return import_Bytes.Bytes.fromHex(data[JSON_SPECIAL_KEY_VALUE]);
        case "Map":
          return new Map(
            fromJson(data[JSON_SPECIAL_KEY_VALUE])
          );
        case "Interval":
          return (0, import_TimeUnit.Millis)(data);
        // TODO Remove in the future, leave here for now for backward compatibility?
        case "AttributeId":
        case "CaseAuthenticatedTag":
        case "ClusterId":
        case "CommandId":
        case "DataVersion":
        case "DeviceTypeId":
        case "EndpointNumber":
        case "EntryIndex":
        case "EventId":
        case "FabricIndex":
        case "FieldId":
        case "GroupId":
        case "VendorId":
          return data[JSON_SPECIAL_KEY_VALUE];
        case "EventNumber":
        case "FabricId":
        case "NodeId":
          return BigInt(data[JSON_SPECIAL_KEY_VALUE]);
        default:
          throw new import_MatterError.UnexpectedDataError(`Unknown object type: ${object}`);
      }
    }
    return value;
  });
}
//# sourceMappingURL=StringifyTools.js.map
