"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const buffer = require("./buffer.js");
const uri = require("./uri.js");
function parse(text) {
  let data = JSON.parse(text);
  data = revive(data);
  return data;
}
function revive(obj, depth = 0) {
  if (!obj || depth > 200) {
    return obj;
  }
  if (typeof obj === "object") {
    switch (obj.$mid) {
      case 1:
        return uri.URI.revive(obj);
      case 2:
        return new RegExp(obj.source, obj.flags);
      case 14:
        return new Date(obj.source);
    }
    if (obj instanceof buffer.VSBuffer || obj instanceof Uint8Array) {
      return obj;
    }
    if (Array.isArray(obj)) {
      for (let i = 0; i < obj.length; ++i) {
        obj[i] = revive(obj[i], depth + 1);
      }
    } else {
      for (const key in obj) {
        if (Object.hasOwnProperty.call(obj, key)) {
          obj[key] = revive(obj[key], depth + 1);
        }
      }
    }
  }
  return obj;
}
exports.parse = parse;
exports.revive = revive;
