import { __name } from './chunk-6AXVLTU5.js';
import Diff from 'diff-match-patch';

var dmp = new Diff();
function generateSourceMap(id, source, transpiled) {
  if (!transpiled) {
    return;
  }
  const diff = dmp.diff_main(source.toString(), transpiled);
  dmp.diff_cleanupSemantic(diff);
  let offset = 0;
  for (let index = 0; index < diff.length; index++) {
    if (diff[index]) {
      const [type, text] = diff[index];
      const textLength = text.length;
      switch (type) {
        case 0: {
          offset += textLength;
          break;
        }
        case 1: {
          source.prependLeft(offset, text);
          break;
        }
        case -1: {
          const next = diff.at(index + 1);
          if (next && next[0] === 1) {
            const replaceText = next[1];
            const firstNonWhitespaceIndexOfText = text.search(/\S/);
            const offsetStart = offset + Math.max(firstNonWhitespaceIndexOfText, 0);
            source.update(offsetStart, offset + textLength, replaceText);
            index += 1;
          } else {
            source.remove(offset, offset + textLength);
          }
          offset += textLength;
          break;
        }
      }
    }
  }
  if (!source.hasChanged()) {
    return;
  }
  return {
    code: source.toString(),
    map: source.generateMap({
      source: id,
      file: `${id}.map`,
      includeContent: true
    })
  };
}
__name(generateSourceMap, "generateSourceMap");

export { generateSourceMap };
//# sourceMappingURL=chunk-4FDFE63H.js.map
//# sourceMappingURL=chunk-4FDFE63H.js.map