{
  "version": 3,
  "sources": ["../../../../src/packages/text.comments-parser/insertComments.ts"],
  "sourcesContent": ["import type { CommentSpecifier } from './CommentSpecifier.ts';\n\nexport function insertComments(\n  json: string,\n  comments: CommentSpecifier[]\n): string {\n  // We need to reintroduce the comments. So create an index of\n  // the lines of the manifest so we can try to match them up.\n  // We eliminate whitespace and quotes in the index entries,\n  // because pnpm may have changed them.\n  const jsonLines = json.split('\\n');\n\n  const index: Record<string, number> = {};\n\n  // eslint-disable-next-line optimize-regex/optimize-regex\n  const canonicalizer = /[\\s'\"]/g;\n\n  for (let i = 0; i < jsonLines.length; ++i) {\n    const key = jsonLines[i]?.replace(canonicalizer, '');\n\n    if (typeof key === 'undefined') {\n      continue;\n    }\n\n    if (key in index) {\n      index[key] = -1; // Mark this line as occurring twice\n    } else {\n      index[key] = i;\n    }\n  }\n\n  // A place to put comments that come _before_ the lines they are\n  // anchored to:\n  const jsonPrefix: Record<string, string> = {};\n\n  for (const comment of comments) {\n    // First if we can find the line the comment was on, that is\n    // the most reliable locator:\n    let key = comment.on.replace(canonicalizer, '');\n\n    const ik = index[key];\n\n    if (key && typeof ik !== 'undefined' && ik >= 0) {\n      jsonLines[ik] += ` ${comment.content}`;\n      continue;\n    }\n\n    // Next, if it's not before anything, it must have been at the very end:\n    if (comment.before === undefined) {\n      jsonLines[jsonLines.length - 1] += comment.whitespace + comment.content;\n\n      continue;\n    }\n\n    // Next, try to put it before something; note the comment extractor\n    // used the convention that position 0 is before the first line:\n    let location = comment.lineNumber === 0 ? 0 : -1;\n\n    if (location < 0) {\n      key = comment.before.replace(canonicalizer, '');\n\n      const ik = index[key];\n\n      if (key && typeof ik !== 'undefined') {\n        location = ik;\n      }\n    }\n\n    if (location >= 0) {\n      if (typeof jsonPrefix[location] === 'undefined') {\n        const inlineWhitespace = comment.whitespace.startsWith('\\n')\n          ? comment.whitespace.slice(1)\n          : comment.whitespace;\n\n        jsonPrefix[location] = inlineWhitespace + comment.content;\n      } else {\n        jsonPrefix[location] += ` ${comment.content}`;\n      }\n\n      continue;\n    }\n\n    // The last definite indicator we can use is that it is after something:\n    if (typeof comment.after === 'string') {\n      key = comment.after.replace(canonicalizer, '');\n\n      const ik = index[key];\n\n      if (key && typeof ik !== 'undefined' && ik >= 0) {\n        jsonLines[ik] += comment.whitespace + comment.content;\n\n        continue;\n      }\n    }\n\n    // Finally, try to get it in the right general location by using the\n    // line number, but warn the user the comment may have been relocated:\n    location = comment.lineNumber - 1; // 0 was handled above\n\n    let separator = ' ';\n\n    if (location >= jsonLines.length) {\n      location = jsonLines.length - 1;\n      separator = '\\n';\n    }\n\n    jsonLines[location] +=\n      `${separator + comment.content} /* [comment possibly relocated by pnpm] */`;\n  }\n  // Insert the accumulated prefixes:\n  for (let i = 0; i < jsonLines.length; ++i) {\n    const jp = jsonPrefix[i];\n    const jl = jsonLines[i];\n\n    if (typeof jp === 'string' && typeof jl === 'string') {\n      jsonLines[i] = `${jp}\\n${jl}`;\n    }\n  }\n\n  // And reassemble the manifest:\n  return jsonLines.join('\\n');\n}\n"],
  "mappings": "AAEO,SAAS,eACd,MACA,UACQ;AAKR,QAAM,YAAY,KAAK,MAAM,IAAI;AAEjC,QAAM,QAAgC,CAAC;AAGvC,QAAM,gBAAgB;AAEtB,WAAS,IAAI,GAAG,IAAI,UAAU,QAAQ,EAAE,GAAG;AACzC,UAAM,MAAM,UAAU,CAAC,GAAG,QAAQ,eAAe,EAAE;AAEnD,QAAI,OAAO,QAAQ,aAAa;AAC9B;AAAA,IACF;AAEA,QAAI,OAAO,OAAO;AAChB,YAAM,GAAG,IAAI;AAAA,IACf,OAAO;AACL,YAAM,GAAG,IAAI;AAAA,IACf;AAAA,EACF;AAIA,QAAM,aAAqC,CAAC;AAE5C,aAAW,WAAW,UAAU;AAG9B,QAAI,MAAM,QAAQ,GAAG,QAAQ,eAAe,EAAE;AAE9C,UAAM,KAAK,MAAM,GAAG;AAEpB,QAAI,OAAO,OAAO,OAAO,eAAe,MAAM,GAAG;AAC/C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