var k=Object.defineProperty;var v=(r,t)=>{for(var e in t)k(r,e,{get:t[e],enumerable:!0})};var g={};v(g,{UISourceCodeDiff:()=>a,WorkspaceDiffImpl:()=>o,workspaceDiff:()=>D});import*as p from"./../../core/common/common.js";import*as d from"./../../core/host/host.js";import*as C from"./../../third_party/diff/diff.js";import*as l from"./../formatter/formatter.js";import*as c from"./../persistence/persistence.js";import*as n from"./../text_utils/text_utils.js";import*as i from"./../workspace/workspace.js";var o=class extends p.ObjectWrapper.ObjectWrapper{#t=c.Persistence.PersistenceImpl.instance();#s=new WeakMap;loadingUISourceCodes=new Map;#e=new Set;constructor(t){super(),t.addEventListener(i.Workspace.Events.WorkingCopyChanged,this.#r,this),t.addEventListener(i.Workspace.Events.WorkingCopyCommitted,this.#r,this),t.addEventListener(i.Workspace.Events.UISourceCodeAdded,this.#c,this),t.addEventListener(i.Workspace.Events.UISourceCodeRemoved,this.#f,this),t.addEventListener(i.Workspace.Events.ProjectRemoved,this.#h,this),t.uiSourceCodes().forEach(this.#o.bind(this))}requestDiff(t){return this.#i(t).requestDiff()}subscribeToDiffChange(t,e,s){this.#i(t).addEventListener("DiffChanged",e,s)}unsubscribeFromDiffChange(t,e,s){this.#i(t).removeEventListener("DiffChanged",e,s)}modifiedUISourceCodes(){return Array.from(this.#e)}#i(t){let e=this.#s.get(t);return e||(e=new a(t),this.#s.set(t,e)),e}#r(t){let e=t.data.uiSourceCode;this.#o(e)}#c(t){let e=t.data;this.#o(e)}#f(t){let e=t.data;this.#a(e)}#h(t){let e=t.data;for(let s of e.uiSourceCodes())this.#a(s)}#a(t){this.loadingUISourceCodes.delete(t);let e=this.#s.get(t);e&&(e.dispose=!0),this.#n(t)}#n(t){this.uiSourceCodeProcessedForTest(),this.#e.delete(t)&&this.dispatchEventToListeners("ModifiedStatusChanged",{uiSourceCode:t,isModified:!1})}#d(t){this.uiSourceCodeProcessedForTest(),!this.#e.has(t)&&(this.#e.add(t),this.dispatchEventToListeners("ModifiedStatusChanged",{uiSourceCode:t,isModified:!0}))}uiSourceCodeProcessedForTest(){}#l(t){switch(t.project().type()){case i.Workspace.projectTypes.Network:return this.#t.binding(t)===null;case i.Workspace.projectTypes.FileSystem:return!0;default:return!1}}async#o(t){if(this.loadingUISourceCodes.delete(t),!this.#l(t)){this.#n(t);return}if(t.isDirty()){this.#d(t);return}if(!t.hasCommits()){this.#n(t);return}let e=Promise.all([this.requestOriginalContentForUISourceCode(t),t.requestContentData().then(f=>n.ContentData.ContentData.textOr(f,null))]);this.loadingUISourceCodes.set(t,e);let s=await e;this.loadingUISourceCodes.get(t)===e&&(this.loadingUISourceCodes.delete(t),s[0]!==null&&s[1]!==null&&s[0]!==s[1]?this.#d(t):this.#n(t))}requestOriginalContentForUISourceCode(t){return this.#i(t).originalContent()}revertToOriginal(t){function e(s){typeof s=="string"&&t.addRevision(s)}return d.userMetrics.actionTaken(d.UserMetrics.Action.RevisionApplied),this.requestOriginalContentForUISourceCode(t).then(e)}},a=class extends p.ObjectWrapper.ObjectWrapper{#t;#s=null;#e=null;dispose=!1;constructor(t){super(),this.#t=t,t.addEventListener(i.UISourceCode.Events.WorkingCopyChanged,this.#i,this),t.addEventListener(i.UISourceCode.Events.WorkingCopyCommitted,this.#i,this)}#i(){this.#e&&(clearTimeout(this.#e),this.#e=null),this.#s=null;let t=this.#t.content(),e=!t||t.length<65536?0:200;this.#e=window.setTimeout(s.bind(this),e);function s(){this.dispose||(this.dispatchEventToListeners("DiffChanged"),this.#e=null)}}requestDiff(){return this.#s||(this.#s=this.#r()),this.#s}async originalContent(){let t=c.NetworkPersistenceManager.NetworkPersistenceManager.instance().originalContentForUISourceCode(this.#t);if(t)return await t;let e=await this.#t.project().requestFileContent(this.#t);return n.ContentData.ContentData.isError(e)?e.error:e.asDeferedContent().content}async#r(){if(this.dispose)return null;let t=await this.originalContent();if(t===null||t.length>1024*1024||this.dispose)return null;let e=this.#t.workingCopy();if(!e&&!this.#t.contentLoaded()){let m=await this.#t.requestContentData();if(n.ContentData.ContentData.isError(m))return null;e=m.text}if(e.length>1024*1024||this.dispose)return null;t=(await l.ScriptFormatter.format(this.#t.contentType(),this.#t.mimeType(),t)).formattedContent;let s=await l.ScriptFormatter.format(this.#t.contentType(),this.#t.mimeType(),e);e=s.formattedContent;let f=s.formattedMapping,u=/\r\n?|\n/;return{diff:C.Diff.DiffWrapper.lineDiff(t.split(u),e.split(u)),formattedCurrentMapping:f}}},h=null;function D({forceNew:r}={}){return(!h||r)&&(h=new o(i.Workspace.WorkspaceImpl.instance())),h}export{g as WorkspaceDiff};
//# sourceMappingURL=workspace_diff.js.map
