import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";import{Events,IsolatedFileSystemManager}from"./IsolatedFileSystemManager.js";const UIStrings={excludedFolders:"Excluded folders",add:"Add",none:"None",sViaDevtools:"{PH1} (via .devtools)",folderPath:"Folder path",enterAPath:"Enter a path",enterAUniquePath:"Enter a unique path"},str_=i18n.i18n.registerUIStrings("persistence/EditFileSystemView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class EditFileSystemView extends UI.Widget.VBox{constructor(e){super(!0),this.registerRequiredCSS("persistence/editFileSystemView.css",{enableLegacyPatching:!1}),this._fileSystemPath=e,this._excludedFolders=[],this._eventListeners=[IsolatedFileSystemManager.instance().addEventListener(Events.ExcludedFolderAdded,this._update,this),IsolatedFileSystemManager.instance().addEventListener(Events.ExcludedFolderRemoved,this._update,this)];const t=this.contentElement.createChild("div","file-system-header");t.createChild("div","file-system-header-text").textContent=i18nString(UIStrings.excludedFolders),t.appendChild(UI.UIUtils.createTextButton(i18nString(UIStrings.add),this._addExcludedFolderButtonClicked.bind(this),"add-button")),this._excludedFoldersList=new UI.ListWidget.ListWidget(this),this._excludedFoldersList.element.classList.add("file-system-list"),this._excludedFoldersList.registerRequiredCSS("persistence/editFileSystemView.css",{enableLegacyPatching:!1});const i=document.createElement("div");i.classList.add("file-system-list-empty"),i.textContent=i18nString(UIStrings.none),this._excludedFoldersList.setEmptyPlaceholder(i),this._excludedFoldersList.show(this.contentElement),this._update()}dispose(){Common.EventTarget.EventTarget.removeEventListeners(this._eventListeners)}_getFileSystem(){return IsolatedFileSystemManager.instance().fileSystem(this._fileSystemPath)}_update(){if(!this._muteUpdate){this._excludedFoldersList.clear(),this._excludedFolders=[];for(const e of this._getFileSystem().excludedFolders().values())this._excludedFolders.push(e),this._excludedFoldersList.appendItem(e,!0)}}_addExcludedFolderButtonClicked(){this._excludedFoldersList.addNewItem(0,"")}renderItem(e,t){const i=document.createElement("div");i.classList.add("file-system-list-item");const s=t?e:i18nString(UIStrings.sViaDevtools,{PH1:e}),d=i.createChild("div","file-system-value");return d.textContent=s,UI.Tooltip.Tooltip.install(d,s),i}removeItemRequested(e,t){this._getFileSystem().removeExcludedFolder(this._excludedFolders[t])}commitEdit(e,t,i){this._muteUpdate=!0,i||this._getFileSystem().removeExcludedFolder(e),this._getFileSystem().addExcludedFolder(this._normalizePrefix(t.control("pathPrefix").value)),this._muteUpdate=!1,this._update()}beginEdit(e){const t=this._createExcludedFolderEditor();return t.control("pathPrefix").value=e,t}_createExcludedFolderEditor(){if(this._excludedFolderEditor)return this._excludedFolderEditor;const e=new UI.ListWidget.Editor;this._excludedFolderEditor=e;const t=e.contentElement();t.createChild("div","file-system-edit-row").createChild("div","file-system-value").textContent=i18nString(UIStrings.folderPath);return t.createChild("div","file-system-edit-row").createChild("div","file-system-value").appendChild(e.createInput("pathPrefix","text","/path/to/folder/",function(e,t,i){const s=this._normalizePrefix(i.value.trim());if(!s)return{valid:!1,errorMessage:i18nString(UIStrings.enterAPath)};const d=this._getFileSystem().excludedFolders().size;for(let r=0;r<d;++r)if(r!==t&&this._excludedFolders[r]===s)return{valid:!1,errorMessage:i18nString(UIStrings.enterAUniquePath)};return{valid:!0,errorMessage:void 0}}.bind(this))),e}_normalizePrefix(e){return e?e+("/"===e[e.length-1]?"":"/"):""}}