"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const types = require("./esm-types.cjs");
const EMPTY = "";
const { toString } = {};
const { keys } = Object;
const typeOf = (value) => {
  const type = typeof value;
  if (type !== "object" || !value)
    return [types.PRIMITIVE, type];
  const asString = toString.call(value).slice(8, -1);
  switch (asString) {
    case "Array":
      return [types.ARRAY, EMPTY];
    case "Object":
      return [types.OBJECT, EMPTY];
    case "Date":
      return [types.DATE, EMPTY];
    case "RegExp":
      return [types.REGEXP, EMPTY];
    case "Map":
      return [types.MAP, EMPTY];
    case "Set":
      return [types.SET, EMPTY];
    case "DataView":
      return [types.ARRAY, asString];
  }
  if (asString.includes("Array"))
    return [types.ARRAY, asString];
  if (asString.includes("Error"))
    return [types.ERROR, asString];
  return [types.OBJECT, asString];
};
const shouldSkip = ([TYPE, type]) => TYPE === types.PRIMITIVE && (type === "function" || type === "symbol");
const serializer = (strict, json, $, _) => {
  const as = (out, value) => {
    const index = _.push(out) - 1;
    $.set(value, index);
    return index;
  };
  const pair = (value) => {
    if ($.has(value))
      return $.get(value);
    let [TYPE, type] = typeOf(value);
    switch (TYPE) {
      case types.PRIMITIVE: {
        let entry = value;
        switch (type) {
          case "bigint":
            TYPE = types.BIGINT;
            entry = value.toString();
            break;
          case "function":
          case "symbol":
            if (strict)
              throw new TypeError("unable to serialize " + type);
            entry = null;
            break;
          case "undefined":
            return as([types.VOID], value);
        }
        return as([TYPE, entry], value);
      }
      case types.ARRAY: {
        if (type) {
          let spread = value;
          if (type === "DataView") {
            spread = new Uint8Array(value.buffer);
          } else if (type === "ArrayBuffer") {
            spread = new Uint8Array(value);
          }
          return as([type, [...spread]], value);
        }
        const arr = [];
        const index = as([TYPE, arr], value);
        for (const entry of value)
          arr.push(pair(entry));
        return index;
      }
      case types.OBJECT: {
        if (type) {
          switch (type) {
            case "BigInt":
              return as([type, value.toString()], value);
            case "Boolean":
            case "Number":
            case "String":
              return as([type, value.valueOf()], value);
          }
        }
        if (json && "toJSON" in value)
          return pair(value.toJSON());
        const entries = [];
        const index = as([TYPE, entries], value);
        for (const key of keys(value)) {
          if (strict || !shouldSkip(typeOf(value[key])))
            entries.push([pair(key), pair(value[key])]);
        }
        return index;
      }
      case types.DATE:
        return as([TYPE, value.toISOString()], value);
      case types.REGEXP: {
        const { source, flags } = value;
        return as([TYPE, { source, flags }], value);
      }
      case types.MAP: {
        const entries = [];
        const index = as([TYPE, entries], value);
        for (const [key, entry] of value) {
          if (strict || !(shouldSkip(typeOf(key)) || shouldSkip(typeOf(entry))))
            entries.push([pair(key), pair(entry)]);
        }
        return index;
      }
      case types.SET: {
        const entries = [];
        const index = as([TYPE, entries], value);
        for (const entry of value) {
          if (strict || !shouldSkip(typeOf(entry)))
            entries.push(pair(entry));
        }
        return index;
      }
    }
    const { message } = value;
    return as([TYPE, { name: type, message }], value);
  };
  return pair;
};
const serialize = (value, { json, lossy } = {}) => {
  const _ = [];
  return serializer(!(json || lossy), !!json, /* @__PURE__ */ new Map(), _)(value), _;
};
exports.serialize = serialize;
//# sourceMappingURL=esm-serialize.cjs.map
