"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 parse_yarn_descriptor_exports = {};
__export(parse_yarn_descriptor_exports, {
  parseYarnDescriptor: () => parseYarnDescriptor
});
module.exports = __toCommonJS(parse_yarn_descriptor_exports);
var import_string = require("../../../../primordials/string");
const PATCH_PREFIX = "patch:";
const PATCH_PREFIX_LEN = PATCH_PREFIX.length;
const NPM_PROTO = "@npm:";
const NPM_PROTO_ENCODED = "@npm%3A";
const WORKSPACE_PROTO = "@workspace:";
function parseYarnDescriptor(descriptor) {
  if ((0, import_string.StringPrototypeIndexOf)(descriptor, PATCH_PREFIX) === 0) {
    const afterPatch = (0, import_string.StringPrototypeSlice)(descriptor, PATCH_PREFIX_LEN);
    let npmIndex = (0, import_string.StringPrototypeIndexOf)(afterPatch, NPM_PROTO);
    const npmEncodedIndex = (0, import_string.StringPrototypeIndexOf)(
      afterPatch,
      NPM_PROTO_ENCODED
    );
    const workspaceIndex = (0, import_string.StringPrototypeIndexOf)(afterPatch, WORKSPACE_PROTO);
    if (npmEncodedIndex > 0 && (npmIndex === -1 || npmEncodedIndex < npmIndex)) {
      npmIndex = npmEncodedIndex;
    }
    if (npmIndex > 0) {
      return { name: (0, import_string.StringPrototypeSlice)(afterPatch, 0, npmIndex) };
    }
    if (workspaceIndex > 0) {
      return { name: (0, import_string.StringPrototypeSlice)(afterPatch, 0, workspaceIndex) };
    }
  }
  let protocolIndex = (0, import_string.StringPrototypeIndexOf)(descriptor, NPM_PROTO);
  if (protocolIndex > 0) {
    return { name: (0, import_string.StringPrototypeSlice)(descriptor, 0, protocolIndex) };
  }
  protocolIndex = (0, import_string.StringPrototypeIndexOf)(descriptor, WORKSPACE_PROTO);
  if (protocolIndex > 0) {
    return { name: (0, import_string.StringPrototypeSlice)(descriptor, 0, protocolIndex) };
  }
  const atIdx = (0, import_string.StringPrototypeLastIndexOf)(descriptor, "@");
  if (atIdx > 0) {
    return { name: (0, import_string.StringPrototypeSlice)(descriptor, 0, atIdx) };
  }
  return { name: descriptor };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  parseYarnDescriptor
});
