import { SET as w, MAP as O, REGEXP as A, DATE as I, OBJECT as S, ARRAY as E, PRIMITIVE as b, BIGINT as T, ERROR as m, VOID as x } from "./types.js";
const p = "", { toString: k } = {}, { keys: B } = Object, g = (c) => {
  const f = typeof c;
  if (f !== "object" || !c)
    return [b, f];
  const s = k.call(c).slice(8, -1);
  switch (s) {
    case "Array":
      return [E, p];
    case "Object":
      return [S, p];
    case "Date":
      return [I, p];
    case "RegExp":
      return [A, p];
    case "Map":
      return [O, p];
    case "Set":
      return [w, p];
    case "DataView":
      return [E, s];
  }
  return s.includes("Array") ? [E, s] : s.includes("Error") ? [m, s] : [S, s];
}, u = ([c, f]) => c === b && (f === "function" || f === "symbol"), D = (c, f, s, h) => {
  const o = (r, i) => {
    const e = h.push(r) - 1;
    return s.set(i, e), e;
  }, y = (r) => {
    if (s.has(r))
      return s.get(r);
    let [i, e] = g(r);
    switch (i) {
      case b: {
        let t = r;
        switch (e) {
          case "bigint":
            i = T, t = r.toString();
            break;
          case "function":
          case "symbol":
            if (c)
              throw new TypeError("unable to serialize " + e);
            t = null;
            break;
          case "undefined":
            return o([x], r);
        }
        return o([i, t], r);
      }
      case E: {
        if (e) {
          let n = r;
          return e === "DataView" ? n = new Uint8Array(r.buffer) : e === "ArrayBuffer" && (n = new Uint8Array(r)), o([e, [...n]], r);
        }
        const t = [], a = o([i, t], r);
        for (const n of r)
          t.push(y(n));
        return a;
      }
      case S: {
        if (e)
          switch (e) {
            case "BigInt":
              return o([e, r.toString()], r);
            case "Boolean":
            case "Number":
            case "String":
              return o([e, r.valueOf()], r);
          }
        if (f && "toJSON" in r)
          return y(r.toJSON());
        const t = [], a = o([i, t], r);
        for (const n of B(r))
          (c || !u(g(r[n]))) && t.push([y(n), y(r[n])]);
        return a;
      }
      case I:
        return o([i, r.toISOString()], r);
      case A: {
        const { source: t, flags: a } = r;
        return o([i, { source: t, flags: a }], r);
      }
      case O: {
        const t = [], a = o([i, t], r);
        for (const [n, d] of r)
          (c || !(u(g(n)) || u(g(d)))) && t.push([y(n), y(d)]);
        return a;
      }
      case w: {
        const t = [], a = o([i, t], r);
        for (const n of r)
          (c || !u(g(n))) && t.push(y(n));
        return a;
      }
    }
    const { message: R } = r;
    return o([i, { name: e, message: R }], r);
  };
  return y;
}, P = (c, { json: f, lossy: s } = {}) => {
  const h = [];
  return D(!(f || s), !!f, /* @__PURE__ */ new Map(), h)(c), h;
};
export {
  P as serialize
};
//# sourceMappingURL=serialize.js.map
