import*as Diff from"../diff/diff.js";export class SourceCodeDiff{constructor(t){this._textEditor=t,this._animatedLines=[],this._animationTimeout=null}highlightModifiedLines(t,i){if("string"!==typeof t||"string"!==typeof i)return;const e=SourceCodeDiff.computeDiff(Diff.Diff.DiffWrapper.lineDiff(t.split("\n"),i.split("\n"))),o=[];for(let n=0;n<e.length;++n){const t=e[n];if(t.type!==EditType.Delete)for(let i=t.from;i<t.to;++i){const t=this._textEditor.textEditorPositionHandle(i,0);t&&o.push(t)}}this._updateHighlightedLines(o),this._animationTimeout=window.setTimeout(this._updateHighlightedLines.bind(this,[]),400)}_updateHighlightedLines(t){function i(t){for(let i=0;i<this._animatedLines.length;++i){const e=this._animatedLines[i].resolve();e&&this._textEditor.toggleLineClass(e.lineNumber,"highlight-line-modification",t)}}this._animationTimeout&&clearTimeout(this._animationTimeout),this._animationTimeout=null,this._textEditor.operation(function(){i.call(this,!1),this._animatedLines=t,i.call(this,!0)}.bind(this))}static computeDiff(t){const i=[];let e=!1,o=!1,n=0,f=0,s=!1;for(let d=0;d<t.length;++d){const i=t[d];i[0]!==Diff.Diff.Operation.Equal?(s||(s=!0,n=f),i[0]===Diff.Diff.Operation.Delete?o=!0:(f+=i[1].length,e=!0)):(s&&l(),f+=i[1].length)}if(s&&l(),i.length>1&&0===i[0].from&&0===i[1].from){const t={type:EditType.Modify,from:0,to:i[1].to};i.splice(0,2,t)}return i;function l(){let t=EditType.Insert,l=n,d=f;e&&o?t=EditType.Modify:!e&&o&&0===l&&0===d?(t=EditType.Modify,d=1):!e&&o&&(t=EditType.Delete,l-=1),i.push({type:t,from:l,to:d}),s=!1,e=!1,o=!1}}}export var EditType;!function(t){t.Insert="Insert",t.Delete="Delete",t.Modify="Modify"}(EditType||(EditType={}));