import{ObjectWrapper as e}from"../common/common.js";import{Diff as i}from"../diff/diff.js";import{userMetrics as o,UserMetrics as t}from"../host/host.js";import{Workspace as s,UISourceCode as r}from"../workspace/workspace.js";class n extends e.ObjectWrapper{constructor(e){super(),this._uiSourceCodeDiffs=new WeakMap,this._loadingUISourceCodes=new Map,this._modifiedUISourceCodes=new Set,e.addEventListener(s.Events.WorkingCopyChanged,this._uiSourceCodeChanged,this),e.addEventListener(s.Events.WorkingCopyCommitted,this._uiSourceCodeChanged,this),e.addEventListener(s.Events.UISourceCodeAdded,this._uiSourceCodeAdded,this),e.addEventListener(s.Events.UISourceCodeRemoved,this._uiSourceCodeRemoved,this),e.addEventListener(s.Events.ProjectRemoved,this._projectRemoved,this),e.uiSourceCodes().forEach(this._updateModifiedState.bind(this))}requestDiff(e){return this._uiSourceCodeDiff(e).requestDiff()}subscribeToDiffChange(e,i,o){this._uiSourceCodeDiff(e).addEventListener(u.DiffChanged,i,o)}unsubscribeFromDiffChange(e,i,o){this._uiSourceCodeDiff(e).removeEventListener(u.DiffChanged,i,o)}modifiedUISourceCodes(){return Array.from(this._modifiedUISourceCodes)}isUISourceCodeModified(e){return this._modifiedUISourceCodes.has(e)||this._loadingUISourceCodes.has(e)}_uiSourceCodeDiff(e){return this._uiSourceCodeDiffs.has(e)||this._uiSourceCodeDiffs.set(e,new d(e)),this._uiSourceCodeDiffs.get(e)}_uiSourceCodeChanged(e){const i=e.data.uiSourceCode;this._updateModifiedState(i)}_uiSourceCodeAdded(e){const i=e.data;this._updateModifiedState(i)}_uiSourceCodeRemoved(e){const i=e.data;this._removeUISourceCode(i)}_projectRemoved(e){const i=e.data;for(const e of i.uiSourceCodes())this._removeUISourceCode(e)}_removeUISourceCode(e){this._loadingUISourceCodes.delete(e);const i=this._uiSourceCodeDiffs.get(e);i&&(i._dispose=!0),this._markAsUnmodified(e)}_markAsUnmodified(e){this._uiSourceCodeProcessedForTest(),this._modifiedUISourceCodes.delete(e)&&this.dispatchEventToListeners(u.ModifiedStatusChanged,{uiSourceCode:e,isModified:!1})}_markAsModified(e){this._uiSourceCodeProcessedForTest(),this._modifiedUISourceCodes.has(e)||(this._modifiedUISourceCodes.add(e),this.dispatchEventToListeners(u.ModifiedStatusChanged,{uiSourceCode:e,isModified:!0}))}_uiSourceCodeProcessedForTest(){}async _updateModifiedState(e){if(this._loadingUISourceCodes.delete(e),e.project().type()!==s.projectTypes.Network)return void this._markAsUnmodified(e);if(e.isDirty())return void this._markAsModified(e);if(!e.hasCommits())return void this._markAsUnmodified(e);const i=Promise.all([this.requestOriginalContentForUISourceCode(e),e.requestContent().then(e=>e.content)]);this._loadingUISourceCodes.set(e,i);const o=await i;this._loadingUISourceCodes.get(e)===i&&(this._loadingUISourceCodes.delete(e),null!==o[0]&&null!==o[1]&&o[0]!==o[1]?this._markAsModified(e):this._markAsUnmodified(e))}requestOriginalContentForUISourceCode(e){return this._uiSourceCodeDiff(e)._originalContent()}revertToOriginal(e){return o.actionTaken(t.Action.RevisionApplied),this.requestOriginalContentForUISourceCode(e).then((function(i){"string"==typeof i&&e.addRevision(i)}))}}class d extends e.ObjectWrapper{constructor(e){super(),this._uiSourceCode=e,e.addEventListener(r.Events.WorkingCopyChanged,this._uiSourceCodeChanged,this),e.addEventListener(r.Events.WorkingCopyCommitted,this._uiSourceCodeChanged,this),this._requestDiffPromise=null,this._pendingChanges=null,this._dispose=!1}_uiSourceCodeChanged(){this._pendingChanges&&(clearTimeout(this._pendingChanges),this._pendingChanges=null),this._requestDiffPromise=null;const e=this._uiSourceCode.content(),i=!e||e.length<65536?0:a;this._pendingChanges=setTimeout(function(){if(this._dispose)return;this.dispatchEventToListeners(u.DiffChanged),this._pendingChanges=null}.bind(this),i)}requestDiff(){return this._requestDiffPromise||(this._requestDiffPromise=this._innerRequestDiff()),this._requestDiffPromise}async _originalContent(){const e=self.Persistence.networkPersistenceManager.originalContentForUISourceCode(this._uiSourceCode);if(e)return e;const i=await this._uiSourceCode.project().requestFileContent(this._uiSourceCode);return i.content||i.error||""}async _innerRequestDiff(){if(this._dispose)return null;const e=await this._originalContent();if(e.length>1048576)return null;if(this._dispose)return null;let o=this._uiSourceCode.workingCopy();return o||this._uiSourceCode.contentLoaded()||(o=(await this._uiSourceCode.requestContent()).content),o.length>1048576||this._dispose||null===o||null===e?null:i.DiffWrapper.lineDiff(e.split(/\r\n|\n|\r/),o.split(/\r\n|\n|\r/))}}const u={DiffChanged:Symbol("DiffChanged"),ModifiedStatusChanged:Symbol("ModifiedStatusChanged")};let c=null;const a=200;var h=Object.freeze({__proto__:null,WorkspaceDiffImpl:n,UISourceCodeDiff:d,Events:u,workspaceDiff:function(){return c||(c=new n(s.WorkspaceImpl.instance())),c},DiffUILocation:class{constructor(e){this.uiSourceCode=e}},UpdateTimeout:a});export{h as WorkspaceDiff};
