"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 manifest_exports = {};
__export(manifest_exports, {
  createPackageJson: () => createPackageJson,
  fetchPackageManifest: () => fetchPackageManifest,
  fetchPackagePackument: () => fetchPackagePackument
});
module.exports = __toCommonJS(manifest_exports);
var import_packages = require("../constants/packages");
var import_process = require("../constants/process");
var import_socket = require("../constants/socket");
var import_arrays = require("../arrays");
var import_objects = require("../objects");
var import_exports = require("./exports");
var import_validation = require("./validation");
const abortSignal = (0, import_process.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\//;
let _npmPackageArg;
// @__NO_SIDE_EFFECTS__
function getNpmPackageArg() {
  if (_npmPackageArg === void 0) {
    _npmPackageArg = require("../external/npm-package-arg");
  }
  return _npmPackageArg;
}
let _pacote;
// @__NO_SIDE_EFFECTS__
function getPacote() {
  if (_pacote === void 0) {
    _pacote = require("../external/pacote");
  }
  return _pacote;
}
let _semver;
// @__NO_SIDE_EFFECTS__
function getSemver() {
  if (_semver === void 0) {
    _semver = require("../external/semver");
  }
  return _semver;
}
// @__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_objects.isObjectObject)(entryExports) ? { exports: { ...entryExports } } : {},
    ...entryExports ? {} : { main: `${main ?? "./index.js"}` },
    sideEffects: sideEffects !== void 0 && !!sideEffects,
    ...(0, import_objects.isObjectObject)(dependencies) ? { dependencies: { ...dependencies } } : {},
    ...(0, import_objects.isObjectObject)(overrides) ? { overrides: { ...overrides } } : {},
    ...(0, import_objects.isObjectObject)(resolutions) ? { resolutions: { ...resolutions } } : {},
    ...(0, import_objects.isObjectObject)(engines) ? {
      engines: Object.fromEntries(
        (0, import_objects.objectEntries)(engines).map((pair) => {
          const strKey = String(pair[0]);
          const result = [strKey, pair[1]];
          if (strKey === "node") {
            const semver = /* @__PURE__ */ getSemver();
            const { 1: range } = result;
            if (typeof range === "string" && range && packageDefaultNodeRange) {
              const coercedRange = semver.coerce(range);
              if (!semver.satisfies(
                coercedRange?.version ?? "0.0.0",
                packageDefaultNodeRange
              )) {
                result[1] = packageDefaultNodeRange;
              }
            }
          }
          return result;
        })
      )
    } : { engines: { node: packageDefaultNodeRange } },
    files: (0, import_arrays.isArray)(files) ? files.slice() : ["*.d.ts", "*.js"],
    ...(0, import_objects.isObjectObject)(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;
  }
  const pacote = /* @__PURE__ */ getPacote();
  let result;
  try {
    result = await pacote.manifest(pkgNameOrId, pacoteOptions);
  } catch {
  }
  if (signal?.aborted) {
    return void 0;
  }
  if (result) {
    const npmPackageArg = /* @__PURE__ */ getNpmPackageArg();
    const spec = npmPackageArg(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 null;
}
// @__NO_SIDE_EFFECTS__
async function fetchPackagePackument(pkgNameOrId, options) {
  const pacote = /* @__PURE__ */ getPacote();
  try {
    return await pacote.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
});
