"use strict";
/* Socket Lib - Built with esbuild */
"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 exports_exports = {};
__export(exports_exports, {
  findTypesForSubpath: () => findTypesForSubpath,
  getExportFilePaths: () => getExportFilePaths,
  getSubpaths: () => getSubpaths,
  isConditionalExports: () => isConditionalExports,
  isSubpathExports: () => isSubpathExports,
  resolvePackageJsonEntryExports: () => resolvePackageJsonEntryExports
});
module.exports = __toCommonJS(exports_exports);
var import_predicates = require("../arrays/predicates");
var import_sentinels = require("../constants/sentinels");
var import_predicates2 = require("../objects/predicates");
var import_array = require("../primordials/array");
var import_error = require("../primordials/error");
var import_map_set = require("../primordials/map-set");
var import_object = require("../primordials/object");
var import_string = require("../primordials/string");
// @__NO_SIDE_EFFECTS__
function findTypesForSubpath(entryExports, subpath) {
  const queue = [entryExports];
  let pos = 0;
  while (pos < queue.length) {
    if (pos === import_sentinels.LOOP_SENTINEL) {
      throw new import_error.ErrorCtor(
        "Detected infinite loop in entry exports crawl of getTypesForSubpath"
      );
    }
    const value = queue[pos++];
    if ((0, import_predicates.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_predicates2.isObject)(item)) {
          queue.push(item);
        }
      }
    } else if ((0, import_predicates2.isObject)(value)) {
      const keys = (0, import_object.ObjectGetOwnPropertyNames)(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_predicates2.isObject)(item)) {
          queue.push(item);
        }
      }
    }
  }
  return void 0;
}
// @__NO_SIDE_EFFECTS__
function getExportFilePaths(entryExports) {
  if (!(0, import_predicates2.isObject)(entryExports)) {
    return [];
  }
  const paths = [];
  for (const key of (0, import_object.ObjectGetOwnPropertyNames)(entryExports)) {
    if (!(0, import_string.StringPrototypeStartsWith)(key, ".")) {
      continue;
    }
    const value = entryExports[key];
    if (typeof value === "string") {
      (0, import_array.ArrayPrototypePush)(paths, value);
    } else if ((0, import_predicates2.isObject)(value)) {
      for (const subKey of (0, import_object.ObjectGetOwnPropertyNames)(value)) {
        const subValue = value[subKey];
        if (typeof subValue === "string") {
          (0, import_array.ArrayPrototypePush)(paths, subValue);
        } else if ((0, import_predicates.isArray)(subValue)) {
          for (const item of subValue) {
            if (typeof item === "string") {
              (0, import_array.ArrayPrototypePush)(paths, item);
            } else if ((0, import_predicates2.isObject)(item)) {
              for (const nestedKey of (0, import_object.ObjectGetOwnPropertyNames)(item)) {
                const nestedValue = item[nestedKey];
                if (typeof nestedValue === "string") {
                  (0, import_array.ArrayPrototypePush)(paths, nestedValue);
                }
              }
            }
          }
        }
      }
    }
  }
  return [...new import_map_set.SetCtor(paths)].filter((p) => (0, import_string.StringPrototypeStartsWith)(p, "./"));
}
// @__NO_SIDE_EFFECTS__
function getSubpaths(entryExports) {
  if (!(0, import_predicates2.isObject)(entryExports)) {
    return [];
  }
  return (0, import_object.ObjectGetOwnPropertyNames)(entryExports).filter(
    (key) => (0, import_string.StringPrototypeStartsWith)(key, ".")
  );
}
// @__NO_SIDE_EFFECTS__
function isConditionalExports(entryExports) {
  if (!(0, import_predicates2.isPlainObject)(entryExports)) {
    return false;
  }
  const keys = (0, import_object.ObjectGetOwnPropertyNames)(entryExports);
  const { length } = keys;
  if (!length) {
    return false;
  }
  for (let i = 0; i < length; i += 1) {
    const key = keys[i];
    if (key.length > 0 && (0, import_string.StringPrototypeCharCodeAt)(key, 0) === 46) {
      return false;
    }
  }
  return true;
}
// @__NO_SIDE_EFFECTS__
function isSubpathExports(entryExports) {
  if ((0, import_predicates2.isPlainObject)(entryExports)) {
    const keys = (0, import_object.ObjectGetOwnPropertyNames)(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_predicates.isArray)(entryExports)) {
    return { ".": entryExports };
  }
  if (/* @__PURE__ */ isConditionalExports(entryExports)) {
    return entryExports;
  }
  return (0, import_predicates2.isObject)(entryExports) ? entryExports : void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  findTypesForSubpath,
  getExportFilePaths,
  getSubpaths,
  isConditionalExports,
  isSubpathExports,
  resolvePackageJsonEntryExports
});
