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