import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";import{EditFileSystemView}from"./EditFileSystemView.js";import{IsolatedFileSystem}from"./IsolatedFileSystem.js";import{Events,IsolatedFileSystemManager}from"./IsolatedFileSystemManager.js";import{NetworkPersistenceManager}from"./NetworkPersistenceManager.js";const UIStrings={workspace:"Workspace",mappingsAreInferredAutomatically:"Mappings are inferred automatically.",addFolder:"Add folder\u2026",folderExcludePattern:"Folder exclude pattern",remove:"Remove"},str_=i18n.i18n.registerUIStrings("persistence/WorkspaceSettingsTab.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let workspaceSettingsTabInstance;export class WorkspaceSettingsTab extends UI.Widget.VBox{constructor(){super(),this.registerRequiredCSS("persistence/workspaceSettingsTab.css",{enableLegacyPatching:!0});const e=this.element.createChild("header");UI.UIUtils.createTextChild(e.createChild("h1"),i18nString(UIStrings.workspace)),this.containerElement=this.element.createChild("div","settings-container-wrapper").createChild("div","settings-tab settings-content settings-container"),IsolatedFileSystemManager.instance().addEventListener(Events.FileSystemAdded,(e=>this._fileSystemAdded(e.data)),this),IsolatedFileSystemManager.instance().addEventListener(Events.FileSystemRemoved,(e=>this._fileSystemRemoved(e.data)),this);const t=this._createFolderExcludePatternInput();t.classList.add("folder-exclude-pattern"),this.containerElement.appendChild(t);const i=this.containerElement.createChild("div","settings-info-message");UI.UIUtils.createTextChild(i,i18nString(UIStrings.mappingsAreInferredAutomatically)),this._fileSystemsListContainer=this.containerElement.createChild("div","");const s=UI.UIUtils.createTextButton(i18nString(UIStrings.addFolder),this._addFileSystemClicked.bind(this));this.containerElement.appendChild(s),this.setDefaultFocusedElement(s),this._elementByPath=new Map,this._mappingViewByPath=new Map;const n=IsolatedFileSystemManager.instance().fileSystems();for(let a=0;a<n.length;++a)this._addItem(n[a])}static instance(e={forceNew:null}){const{forceNew:t}=e;return workspaceSettingsTabInstance&&!t||(workspaceSettingsTabInstance=new WorkspaceSettingsTab),workspaceSettingsTabInstance}_createFolderExcludePatternInput(){const e=document.createElement("p"),t=e.createChild("label");t.textContent=i18nString(UIStrings.folderExcludePattern);const i=UI.UIUtils.createInput("","text");UI.ARIAUtils.bindLabelToControl(t,i),e.appendChild(i),i.style.width="270px";const s=IsolatedFileSystemManager.instance().workspaceFolderExcludePatternSetting(),n=UI.UIUtils.bindInput(i,s.set.bind(s),(function(e){let t;try{t=new RegExp(e)}catch(i){}return{valid:Boolean(t),errorMessage:void 0}}),!1);return s.addChangeListener((()=>n.call(null,s.get()))),n(s.get()),e}_addItem(e){if(!(e instanceof IsolatedFileSystem))return;const t=NetworkPersistenceManager.instance().project();if(t&&IsolatedFileSystemManager.instance().fileSystem(t.fileSystemPath())===e)return;const i=this._renderFileSystem(e);this._elementByPath.set(e.path(),i),this._fileSystemsListContainer.appendChild(i);const s=new EditFileSystemView(e.path());this._mappingViewByPath.set(e.path(),s),s.element.classList.add("file-system-mapping-view"),s.show(i)}_renderFileSystem(e){const t=e.path(),i=t.lastIndexOf("/"),s=t.substr(i+1),n=document.createElement("div");n.classList.add("file-system-container");const a=n.createChild("div","file-system-header"),r=a.createChild("div","file-system-name");r.textContent=s,UI.ARIAUtils.markAsHeading(r,2);const l=a.createChild("div","file-system-path");l.textContent=t,UI.Tooltip.Tooltip.install(l,t);const d=new UI.Toolbar.Toolbar(""),o=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.remove),"largeicon-delete");return o.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this._removeFileSystemClicked.bind(this,e)),d.appendToolbarItem(o),a.appendChild(d.element),n}_removeFileSystemClicked(e){IsolatedFileSystemManager.instance().removeFileSystem(e)}_addFileSystemClicked(){IsolatedFileSystemManager.instance().addFileSystem()}_fileSystemAdded(e){this._addItem(e)}_fileSystemRemoved(e){const t=this._mappingViewByPath.get(e.path());t&&(t.dispose(),this._mappingViewByPath.delete(e.path()));const i=this._elementByPath.get(e.path());i&&(this._elementByPath.delete(e.path()),i.remove())}}