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 graph_exports = {};
__export(graph_exports, {
  RELATION_TYPE_VALUES: () => RELATION_TYPE_VALUES,
  VERSION_NODE_KIND_VALUES: () => VERSION_NODE_KIND_VALUES,
  formatContentVersionRef: () => formatContentVersionRef,
  isRelationType: () => isRelationType,
  isVersionNodeKind: () => isVersionNodeKind,
  parseContentVersionRef: () => parseContentVersionRef
});
module.exports = __toCommonJS(graph_exports);
var import_asset_spec = require("./asset-spec.js");
const VERSION_NODE_KIND_VALUES = ["APP_VERSION", "ASSET_VERSION", "PACK_VERSION", "ASSET_SPEC_VERSION"];
const RELATION_TYPE_VALUES = ["USES", "CONTAINS", "REMIXES"];
const SEMVER_VERSION_REF_REGEX = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
function parseContentVersionRef(raw) {
  if (typeof raw !== "string") {
    return null;
  }
  const trimmed = raw.trim();
  if (!trimmed.length) {
    return null;
  }
  const match = /^(app|asset|pack|asset-spec):([^/]+)\/([^@]+)@(.+)$/i.exec(trimmed);
  if (!match) {
    return null;
  }
  const [, rawKind, rawCreatorUsername, rawName, rawVersionValue] = match;
  if (!rawKind || !rawCreatorUsername || !rawName || !rawVersionValue) {
    return null;
  }
  const kind = rawKind.toLowerCase();
  const creatorUsername = rawCreatorUsername.trim();
  const name = rawName.trim();
  const rawVersion = rawVersionValue.trim();
  if (!creatorUsername.length || !name.length || !rawVersion.length) {
    return null;
  }
  if (kind === "asset") {
    const revisionMatch = /^r(\d+)$/i.exec(rawVersion);
    if (!revisionMatch) {
      return null;
    }
    const revisionValue = revisionMatch[1];
    if (!revisionValue) {
      return null;
    }
    const revision = Number.parseInt(revisionValue, 10);
    if (!Number.isInteger(revision) || revision <= 0) {
      return null;
    }
    return {
      kind: "asset",
      creatorUsername,
      name,
      revision
    };
  }
  if (!SEMVER_VERSION_REF_REGEX.test(rawVersion)) {
    return null;
  }
  if (kind === "app") {
    return {
      kind: "app",
      creatorUsername,
      name,
      version: rawVersion
    };
  }
  if (kind === "pack") {
    return {
      kind: "pack",
      creatorUsername,
      name,
      version: rawVersion
    };
  }
  if (kind === "asset-spec") {
    return (0, import_asset_spec.parseAssetSpecVersionRef)(trimmed);
  }
  return null;
}
function formatContentVersionRef(ref) {
  if (ref.kind === "asset") {
    return `asset:${ref.creatorUsername}/${ref.name}@r${ref.revision}`;
  }
  if (ref.kind === "app") {
    return `app:${ref.creatorUsername}/${ref.name}@${ref.version}`;
  }
  if (ref.kind === "asset-spec") {
    return (0, import_asset_spec.formatAssetSpecVersionRef)(ref);
  }
  return `pack:${ref.creatorUsername}/${ref.name}@${ref.version}`;
}
function isVersionNodeKind(value) {
  return typeof value === "string" && VERSION_NODE_KIND_VALUES.includes(value);
}
function isRelationType(value) {
  return typeof value === "string" && RELATION_TYPE_VALUES.includes(value);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  RELATION_TYPE_VALUES,
  VERSION_NODE_KIND_VALUES,
  formatContentVersionRef,
  isRelationType,
  isVersionNodeKind,
  parseContentVersionRef
});
