{
  "version": 3,
  "sources": ["../../../../src/packages/calc-dep-state/index.ts"],
  "sourcesContent": ["import { ENGINE_NAME } from '../constants/index.ts';\nimport {\n  getPkgIdWithPatchHash,\n  refToRelative,\n} from '../dependency-path/index.ts';\nimport type { LockfileObject } from '../lockfile.types/index.ts';\nimport type { DepPath, PkgIdWithPatchHash } from '../types/index.ts';\nimport { hashObjectWithoutSorting } from '../crypto.object-hasher/index.ts';\nimport { sortDirectKeys } from '../object.key-sorting/index.ts';\n\nexport type DepsGraph<T extends string> = Record<T, DepsGraphNode<T>>;\n\nexport type DepsGraphNode<T extends string> = {\n  children: { [alias: string]: T };\n  pkgIdWithPatchHash: PkgIdWithPatchHash;\n};\n\nexport type DepsStateCache = {\n  [depPath: string]: DepStateObj;\n};\n\nexport type DepStateObj = {\n  [depPath: string]: DepStateObj;\n};\n\nexport function calcDepState<T extends string>(\n  depsGraph: DepsGraph<T>,\n  cache: DepsStateCache,\n  depPath: string,\n  opts: {\n    patchFileHash?: string | undefined;\n    isBuilt: boolean;\n  }\n): string {\n  let result = ENGINE_NAME;\n\n  if (opts.isBuilt === true) {\n    const depStateObj = calcDepStateObj(depPath, depsGraph, cache, new Set());\n\n    result += `;deps=${hashObjectWithoutSorting(depStateObj)}`;\n  }\n\n  if (typeof opts.patchFileHash === 'string') {\n    result += `;patch=${opts.patchFileHash}`;\n  }\n\n  return result;\n}\n\nfunction calcDepStateObj<T extends string>(\n  depPath: T,\n  depsGraph: DepsGraph<T>,\n  cache: DepsStateCache,\n  parents: Set<PkgIdWithPatchHash>\n): DepStateObj {\n  if (cache[depPath]) {\n    return cache[depPath];\n  }\n\n  const node = depsGraph[depPath];\n\n  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions\n  if (!node) {\n    return {};\n  }\n\n  const nextParents = new Set([\n    ...Array.from(parents),\n    node.pkgIdWithPatchHash,\n  ]);\n\n  const state: DepStateObj = {};\n\n  for (const childId of Object.values(node.children)) {\n    const child = depsGraph[childId];\n\n    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions\n    if (!child) continue;\n\n    if (parents.has(child.pkgIdWithPatchHash)) {\n      state[child.pkgIdWithPatchHash] = {};\n\n      continue;\n    }\n\n    state[child.pkgIdWithPatchHash] = calcDepStateObj(\n      childId,\n      depsGraph,\n      cache,\n      nextParents\n    );\n  }\n\n  cache[depPath] = sortDirectKeys(state);\n\n  return cache[depPath];\n}\n\nexport function lockfileToDepGraph(\n  lockfile: LockfileObject\n): DepsGraph<DepPath> {\n  const graph: DepsGraph<DepPath> = {};\n  if (typeof lockfile.packages !== 'undefined') {\n    for (const [depPath, pkgSnapshot] of Object.entries(lockfile.packages)) {\n      const children = lockfileDepsToGraphChildren({\n        ...pkgSnapshot.dependencies,\n        ...pkgSnapshot.optionalDependencies,\n      });\n\n      graph[depPath as DepPath] = {\n        children,\n        pkgIdWithPatchHash: getPkgIdWithPatchHash(depPath as DepPath),\n      };\n    }\n  }\n\n  return graph;\n}\n\nfunction lockfileDepsToGraphChildren(\n  deps: Record<string, string>\n): Record<string, DepPath> {\n  const children: Record<string, DepPath> = {};\n\n  for (const [alias, reference] of Object.entries(deps)) {\n    const depPath = refToRelative(reference, alias);\n\n    if (depPath) {\n      children[alias] = depPath;\n    }\n  }\n\n  return children;\n}\n"],
  "mappings": "AAAA,SAAS,mBAAmB;AAC5B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAGP,SAAS,gCAAgC;AACzC,SAAS,sBAAsB;AAiBxB,SAAS,aACd,WACA,OACA,SACA,MAIQ;AACR,MAAI,SAAS;AAEb,MAAI,KAAK,YAAY,MAAM;AACzB,UAAM,cAAc,gBAAgB,SAAS,WAAW,OAAO,oBAAI,IAAI,CAAC;AAExE,cAAU,SAAS,yBAAyB,WAAW,CAAC;AAAA,EAC1D;AAEA,MAAI,OAAO,KAAK,kBAAkB,UAAU;AAC1C,cAAU,UAAU,KAAK,aAAa;AAAA,EACxC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