import*as Bindings from"../bindings/bindings.js";import*as Common from"../common/common.js";import*as SDK from"../sdk/sdk.js";import*as Marked from"../third_party/marked/marked.js";import*as Workspace from"../workspace/workspace.js";import*as IssuesManager from"./IssuesManager.js";import{findTitleFromMarkdownAst,getMarkdownFileContent}from"./MarkdownHelpers.js";export class SourceFrameIssuesManager{constructor(e){this.locationPool=new Bindings.LiveLocation.LiveLocationPool,this.issueMessages=new Array,this.issuesManager=e,this.issuesManager.addEventListener(IssuesManager.Events.IssueAdded,this.onIssueAdded,this),this.issuesManager.addEventListener(IssuesManager.Events.FullUpdateRequired,this.onFullUpdateRequired,this)}onIssueAdded(e){const{issue:s}=e.data;this.addIssue(s)}addIssue(e){if(!this.isTrustedTypeIssue(e))return;const s=e.model();if(!s)return;const t=s.target().model(SDK.DebuggerModel.DebuggerModel),i=e.details().sourceCodeLocation;if(i&&t){const s=t.createRawLocationByURL(i.url,i.lineNumber,i.columnNumber);s&&this.addIssueMessageToScript(e,s)}}onFullUpdateRequired(){this.resetMessages();const e=this.issuesManager.issues();for(const s of e)this.addIssue(s)}createIssueDescriptionFromMarkdown(e){const s=getMarkdownFileContent(e.file),t=Marked.Marked.lexer(s);return findTitleFromMarkdownAst(t)}addIssueMessageToScript(e,s){const t=e.getDescription();if(t&&"file"in t){const i=this.createIssueDescriptionFromMarkdown(t);if(i){const t=()=>{Common.Revealer.reveal(e)};this.issueMessages.push(new IssueMessage(i,s,this.locationPool,t))}}}isTrustedTypeIssue(e){return e instanceof SDK.ContentSecurityPolicyIssue.ContentSecurityPolicyIssue&&e.code()===SDK.ContentSecurityPolicyIssue.trustedTypesSinkViolationCode||e.code()===SDK.ContentSecurityPolicyIssue.trustedTypesPolicyViolationCode}resetMessages(){for(const e of this.issueMessages)e.dispose();this.issueMessages=[],this.locationPool.disposeAll()}}export class IssueMessage{constructor(e,s,t,i){this.text=e,this.level=Workspace.UISourceCode.Message.Level.Issue,this.uiMessage=void 0,this.clickHandler=i,Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance().createLiveLocation(s,this.updateLocation.bind(this),t)}async updateLocation(e){this.uiMessage&&this.uiMessage.remove();const s=await e.uiLocation();s&&(this.uiMessage=s.uiSourceCode.addLineMessage(this.level,this.text,s.lineNumber,s.columnNumber,this.clickHandler))}dispose(){this.uiMessage?.remove()}getText(){return this.text}getLevel(){return this.level}}