import*as Common from"../common/common.js";import*as Host from"../host/host.js";import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import*as Workspace from"../workspace/workspace.js";import{NetworkPersistenceManager}from"./NetworkPersistenceManager.js";import{PersistenceImpl}from"./PersistenceImpl.js";const UIStrings={saveAs:"Save as...",saveImage:"Save image",saveForOverrides:"Save for overrides",openInContainingFolder:"Open in containing folder"},str_=i18n.i18n.registerUIStrings("persistence/PersistenceActions.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let contextMenuProviderInstance;export class ContextMenuProvider{static instance(e={forceNew:null}){const{forceNew:n}=e;return contextMenuProviderInstance&&!n||(contextMenuProviderInstance=new ContextMenuProvider),contextMenuProviderInstance}appendApplicableItems(e,n,t){const o=t;o.contentType().isDocumentOrScriptOrStyleSheet()?n.saveSection().appendItem(i18nString(UIStrings.saveAs),(async function(){o instanceof Workspace.UISourceCode.UISourceCode&&o.commitWorkingCopy();let e=(await o.requestContent()).content||"";await o.contentEncoded()&&(e=window.atob(e));const n=o.contentURL();Workspace.FileManager.FileManager.instance().save(n,e,!0),Workspace.FileManager.FileManager.instance().close(n)})):o instanceof SDK.Resource.Resource&&o.contentType().isImage()&&n.saveSection().appendItem(i18nString(UIStrings.saveImage),(async function(){const e=o,n=(await e.requestContent()).content||"",t=document.createElement("a");t.download=e.displayName,t.href="data:"+e.mimeType+";base64,"+n,t.click()}));const r=Workspace.Workspace.WorkspaceImpl.instance().uiSourceCodeForURL(o.contentURL());r&&NetworkPersistenceManager.instance().canSaveUISourceCodeForOverrides(r)&&n.saveSection().appendItem(i18nString(UIStrings.saveForOverrides),(()=>{r.commitWorkingCopy(),NetworkPersistenceManager.instance().saveUISourceCodeForOverrides(r),Common.Revealer.reveal(r)}));const s=r&&PersistenceImpl.instance().binding(r),i=s?s.fileSystem.contentURL():o.contentURL();if(i.startsWith("file://")){const e=Common.ParsedURL.ParsedURL.urlToPlatformPath(i,Host.Platform.isWin());n.revealSection().appendItem(i18nString(UIStrings.openInContainingFolder),(()=>Host.InspectorFrontendHost.InspectorFrontendHostInstance.showItemInFolder(e)))}}}