'use strict';

var chunkAF3E2D32_cjs = require('./chunk-AF3E2D32.cjs');
var Diff = require('diff-match-patch');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var Diff__default = /*#__PURE__*/_interopDefault(Diff);

var dmp = new Diff__default.default();
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
    })
  };
}
chunkAF3E2D32_cjs.__name(generateSourceMap, "generateSourceMap");

exports.generateSourceMap = generateSourceMap;
//# sourceMappingURL=chunk-VPBRJQ7F.cjs.map
//# sourceMappingURL=chunk-VPBRJQ7F.cjs.map