{"version":3,"file":"resolvePolicyBundleVersion-DO7IvEwb.mjs","names":[],"sources":["../src/commands/policy/helpers/resolvePolicyBundleVersion.ts"],"sourcesContent":["import type { Got } from 'got';\n\nimport type { PolicyBundleVersion, PolicyBundleVersionListResponse } from '../types.js';\nimport { policyEngineRequest } from './formatPolicyEngineRequestError.js';\n\n/** Options for resolving a policy bundle version. */\nexport interface ResolvePolicyBundleVersionOptions {\n  /** Caller-supplied version label; omitted to pick the latest by createdAt */\n  version?: string;\n}\n\n/**\n * Lists all versions for a bundle, following cursor pagination.\n *\n * @param client - Policy Engine REST client\n * @param bundleId - Parent bundle UUID\n * @returns All uploaded versions\n */\nasync function listAllPolicyBundleVersions(\n  client: Got,\n  bundleId: string,\n): Promise<PolicyBundleVersion[]> {\n  const limit = 100;\n  const versions: PolicyBundleVersion[] = [];\n  let after: string | undefined;\n\n  while (true) {\n    const searchParams: Record<string, string | number> = { limit };\n    if (after) {\n      searchParams.after = after;\n    }\n\n    const body = await policyEngineRequest(\n      client\n        .get(`v1/policy-engine/policy-bundles/${bundleId}/versions`, {\n          searchParams,\n        })\n        .json<PolicyBundleVersionListResponse>(),\n    );\n\n    versions.push(...body.nodes);\n\n    if (!body.pageInfo.hasNextPage || !body.pageInfo.endCursor) {\n      break;\n    }\n    after = body.pageInfo.endCursor;\n  }\n\n  return versions;\n}\n\n/**\n * Compares two version records by createdAt, newest first.\n *\n * @param left - First version\n * @param right - Second version\n * @returns Sort order for descending createdAt\n */\nfunction compareVersionsByCreatedAtDesc(\n  left: PolicyBundleVersion,\n  right: PolicyBundleVersion,\n): number {\n  return new Date(right.createdAt).getTime() - new Date(left.createdAt).getTime();\n}\n\n/**\n * Resolves a version label to a version record, or picks the latest by createdAt.\n *\n * @param client - Policy Engine REST client\n * @param bundleId - Parent bundle UUID\n * @param options - Version lookup options\n * @returns Matching or latest version record\n */\nexport async function resolvePolicyBundleVersion(\n  client: Got,\n  bundleId: string,\n  options: ResolvePolicyBundleVersionOptions,\n): Promise<PolicyBundleVersion> {\n  const versions = await listAllPolicyBundleVersions(client, bundleId);\n\n  if (versions.length === 0) {\n    throw new Error('No versions found for this policy bundle.');\n  }\n\n  if (options.version) {\n    const match = versions.find((entry) => entry.version === options.version);\n    if (!match) {\n      throw new Error(`Version \"${options.version}\" was not found for this policy bundle.`);\n    }\n    return match;\n  }\n\n  return versions.sort(compareVersionsByCreatedAtDesc)[0];\n}\n"],"mappings":"+CAkBA,eAAe,EACb,EACA,EACgC,CAChC,IACM,EAAkC,EAAE,CACtC,EAEJ,OAAa,CACX,IAAM,EAAgD,CAAE,UAAO,CAC3D,IACF,EAAa,MAAQ,GAGvB,IAAM,EAAO,MAAM,EACjB,EACG,IAAI,mCAAmC,EAAS,WAAY,CAC3D,eACD,CAAC,CACD,MAAuC,CAC3C,CAID,GAFA,EAAS,KAAK,GAAG,EAAK,MAAM,CAExB,CAAC,EAAK,SAAS,aAAe,CAAC,EAAK,SAAS,UAC/C,MAEF,EAAQ,EAAK,SAAS,UAGxB,OAAO,EAUT,SAAS,EACP,EACA,EACQ,CACR,OAAO,IAAI,KAAK,EAAM,UAAU,CAAC,SAAS,CAAG,IAAI,KAAK,EAAK,UAAU,CAAC,SAAS,CAWjF,eAAsB,EACpB,EACA,EACA,EAC8B,CAC9B,IAAM,EAAW,MAAM,EAA4B,EAAQ,EAAS,CAEpE,GAAI,EAAS,SAAW,EACtB,MAAU,MAAM,4CAA4C,CAG9D,GAAI,EAAQ,QAAS,CACnB,IAAM,EAAQ,EAAS,KAAM,GAAU,EAAM,UAAY,EAAQ,QAAQ,CACzE,GAAI,CAAC,EACH,MAAU,MAAM,YAAY,EAAQ,QAAQ,yCAAyC,CAEvF,OAAO,EAGT,OAAO,EAAS,KAAK,EAA+B,CAAC"}