"use strict";
/* Socket Lib - Built with esbuild */
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 exports_exports = {};
__export(exports_exports, {
  findTypesForSubpath: () => findTypesForSubpath,
  getExportFilePaths: () => getExportFilePaths,
  getSubpaths: () => getSubpaths,
  isConditionalExports: () => isConditionalExports,
  isSubpathExports: () => isSubpathExports,
  resolvePackageJsonEntryExports: () => resolvePackageJsonEntryExports
});
module.exports = __toCommonJS(exports_exports);
var import_core = require("#constants/core");
var import_arrays = require("../arrays");
var import_objects = require("../objects");
// @__NO_SIDE_EFFECTS__
function findTypesForSubpath(entryExports, subpath) {
  const queue = [entryExports];
  let pos = 0;
  while (pos < queue.length) {
    if (pos === import_core.LOOP_SENTINEL) {
      throw new Error(
        "Detected infinite loop in entry exports crawl of getTypesForSubpath"
      );
    }
    const value = queue[pos++];
    if ((0, import_arrays.isArray)(value)) {
      for (let i = 0, { length } = value; i < length; i += 1) {
        const item = value[i];
        if (item === subpath) {
          return value.types;
        }
        if ((0, import_objects.isObject)(item)) {
          queue.push(item);
        }
      }
    } else if ((0, import_objects.isObject)(value)) {
      const keys = Object.getOwnPropertyNames(value);
      for (let i = 0, { length } = keys; i < length; i += 1) {
        const key = keys[i];
        const item = value[key];
        if (item === subpath) {
          return value.types;
        }
        if ((0, import_objects.isObject)(item)) {
          queue.push(item);
        }
      }
    }
  }
  return void 0;
}
// @__NO_SIDE_EFFECTS__
function getSubpaths(entryExports) {
  if (!(0, import_objects.isObject)(entryExports)) {
    return [];
  }
  return Object.getOwnPropertyNames(entryExports).filter(
    (key) => key.startsWith(".")
  );
}
// @__NO_SIDE_EFFECTS__
function getExportFilePaths(entryExports) {
  if (!(0, import_objects.isObject)(entryExports)) {
    return [];
  }
  const paths = [];
  for (const key of Object.getOwnPropertyNames(entryExports)) {
    if (!key.startsWith(".")) {
      continue;
    }
    const value = entryExports[key];
    if (typeof value === "string") {
      paths.push(value);
    } else if ((0, import_objects.isObject)(value)) {
      for (const subKey of Object.getOwnPropertyNames(value)) {
        const subValue = value[subKey];
        if (typeof subValue === "string") {
          paths.push(subValue);
        } else if ((0, import_arrays.isArray)(subValue)) {
          for (const item of subValue) {
            if (typeof item === "string") {
              paths.push(item);
            } else if ((0, import_objects.isObject)(item)) {
              for (const nestedKey of Object.getOwnPropertyNames(item)) {
                const nestedValue = item[nestedKey];
                if (typeof nestedValue === "string") {
                  paths.push(nestedValue);
                }
              }
            }
          }
        }
      }
    }
  }
  return [...new Set(paths)].filter((p) => p.startsWith("./"));
}
// @__NO_SIDE_EFFECTS__
function isConditionalExports(entryExports) {
  if (!(0, import_objects.isObjectObject)(entryExports)) {
    return false;
  }
  const keys = Object.getOwnPropertyNames(entryExports);
  const { length } = keys;
  if (!length) {
    return false;
  }
  for (let i = 0; i < length; i += 1) {
    const key = keys[i];
    if (key.length > 0 && key.charCodeAt(0) === 46) {
      return false;
    }
  }
  return true;
}
// @__NO_SIDE_EFFECTS__
function isSubpathExports(entryExports) {
  if ((0, import_objects.isObjectObject)(entryExports)) {
    const keys = Object.getOwnPropertyNames(entryExports);
    for (let i = 0, { length } = keys; i < length; i += 1) {
      if (keys[i]?.charCodeAt(0) === 46) {
        return true;
      }
    }
  }
  return false;
}
// @__NO_SIDE_EFFECTS__
function resolvePackageJsonEntryExports(entryExports) {
  if (typeof entryExports === "string" || (0, import_arrays.isArray)(entryExports)) {
    return { ".": entryExports };
  }
  if (/* @__PURE__ */ isConditionalExports(entryExports)) {
    return entryExports;
  }
  return (0, import_objects.isObject)(entryExports) ? entryExports : void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  findTypesForSubpath,
  getExportFilePaths,
  getSubpaths,
  isConditionalExports,
  isSubpathExports,
  resolvePackageJsonEntryExports
});
