"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var manifest_exports = {};
__export(manifest_exports, {
  createPackageJson: () => createPackageJson,
  fetchPackageManifest: () => fetchPackageManifest,
  fetchPackagePackument: () => fetchPackagePackument
});
module.exports = __toCommonJS(manifest_exports);
var import_packages = require("../constants/packages");
var import_abort = require("../process/abort");
var import_socket = require("../constants/socket");
var import_npm_package_arg = __toESM(require("../external/npm-package-arg"));
var import_pacote = __toESM(require("../external/pacote"));
var import_semver = __toESM(require("../external/semver"));
var import_predicates = require("../arrays/predicates");
var import_predicates2 = require("../objects/predicates");
var import_sort = require("../objects/sort");
var import_exports = require("./exports");
var import_validation = require("./validation");
var import_object = require("../primordials/object");
const abortSignal = (0, import_abort.getAbortSignal)();
const packageDefaultNodeRange = (0, import_packages.getPackageDefaultNodeRange)();
const PACKAGE_DEFAULT_SOCKET_CATEGORIES = (0, import_packages.getPackageDefaultSocketCategories)();
const packumentCache = (0, import_packages.getPackumentCache)();
const pkgScopePrefixRegExp = /^@socketregistry\//;
// @__NO_SIDE_EFFECTS__
function createPackageJson(sockRegPkgName, directory, options) {
  const {
    dependencies,
    description,
    engines,
    exports: entryExportsRaw,
    files,
    keywords,
    main,
    overrides,
    resolutions,
    sideEffects,
    socket,
    type,
    version
  } = { __proto__: null, ...options };
  const name = `@socketregistry/${sockRegPkgName.replace(pkgScopePrefixRegExp, "")}`;
  const entryExports = (0, import_exports.resolvePackageJsonEntryExports)(entryExportsRaw);
  const githubUrl = `https://github.com/${import_socket.SOCKET_GITHUB_ORG}/${import_socket.SOCKET_REGISTRY_REPO_NAME}`;
  return {
    __proto__: null,
    name,
    version,
    license: "MIT",
    description,
    keywords,
    homepage: `${githubUrl}/tree/main/${directory}`,
    repository: {
      type: "git",
      url: `git+${githubUrl}.git`,
      directory
    },
    ...type ? { type } : {},
    ...(0, import_predicates2.isPlainObject)(entryExports) ? { exports: { ...entryExports } } : {},
    ...entryExports ? {} : { main: `${main ?? "./index.js"}` },
    sideEffects: sideEffects !== void 0 && !!sideEffects,
    ...(0, import_predicates2.isPlainObject)(dependencies) ? { dependencies: { ...dependencies } } : {},
    ...(0, import_predicates2.isPlainObject)(overrides) ? { overrides: { ...overrides } } : {},
    ...(0, import_predicates2.isPlainObject)(resolutions) ? { resolutions: { ...resolutions } } : {},
    ...(0, import_predicates2.isPlainObject)(engines) ? {
      engines: (0, import_object.ObjectFromEntries)(
        (0, import_sort.objectEntries)(engines).map((pair) => {
          const strKey = String(pair[0]);
          const result = [strKey, pair[1]];
          if (strKey === "node") {
            const { 1: range } = result;
            if (typeof range === "string" && range && packageDefaultNodeRange) {
              const coercedRange = import_semver.default.coerce(range);
              if (!import_semver.default.satisfies(
                coercedRange?.version ?? "0.0.0",
                packageDefaultNodeRange
              )) {
                result[1] = packageDefaultNodeRange;
              }
            }
          }
          return result;
        })
      )
    } : { engines: { node: packageDefaultNodeRange } },
    files: (0, import_predicates.isArray)(files) ? files.slice() : ["*.d.ts", "*.js"],
    ...(0, import_predicates2.isPlainObject)(socket) ? { socket: { ...socket } } : {
      socket: {
        // Valid categories are: cleanup, levelup, speedup, tuneup
        categories: PACKAGE_DEFAULT_SOCKET_CATEGORIES
      }
    }
  };
}
// @__NO_SIDE_EFFECTS__
async function fetchPackageManifest(pkgNameOrId, options) {
  const pacoteOptions = {
    __proto__: null,
    signal: abortSignal,
    ...options,
    packumentCache,
    preferOffline: true
  };
  const { signal } = pacoteOptions;
  if (signal?.aborted) {
    return void 0;
  }
  let result;
  try {
    result = await import_pacote.default.manifest(pkgNameOrId, pacoteOptions);
  } catch {
  }
  if (signal?.aborted) {
    return void 0;
  }
  if (result) {
    const spec = (0, import_npm_package_arg.default)(pkgNameOrId, pacoteOptions.where);
    if ((0, import_validation.isRegistryFetcherType)(spec.type)) {
      return result;
    }
  }
  if (result) {
    const typedResult = result;
    return await /* @__PURE__ */ fetchPackageManifest(
      `${typedResult.name}@${typedResult.version}`,
      pacoteOptions
    );
  }
  return void 0;
}
// @__NO_SIDE_EFFECTS__
async function fetchPackagePackument(pkgNameOrId, options) {
  try {
    return await import_pacote.default.packument(pkgNameOrId, {
      __proto__: null,
      signal: abortSignal,
      ...options,
      packumentCache,
      preferOffline: true
    });
  } catch {
  }
  return void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createPackageJson,
  fetchPackageManifest,
  fetchPackagePackument
});
