import*as SDK from'../sdk/sdk.js';import{RemoteObjectPreviewFormatter}from'./RemoteObjectPreviewFormatter.js';export class JavaScriptREPL{static wrapObjectLiteral(code){if(!(/^\s*\{/.test(code)&&/\}\s*$/.test(code))){return code;}
const parse=(async()=>0).constructor;try{parse('return '+code+';');const wrappedCode='('+code+')';parse(wrappedCode);return wrappedCode;}catch(e){return code;}}
static preprocessExpression(text){return JavaScriptREPL.wrapObjectLiteral(text);}
static async evaluateAndBuildPreview(text,throwOnSideEffect,timeout,allowErrors,objectGroup){const executionContext=self.UI.context.flavor(SDK.RuntimeModel.ExecutionContext);const maxLength=typeof self.ObjectUI.JavaScriptREPL._MaxLengthForEvaluation!=='undefined'?self.ObjectUI.JavaScriptREPL._MaxLengthForEvaluation:MaxLengthForEvaluation;const isTextLong=text.length>maxLength;if(!text||!executionContext||(throwOnSideEffect&&isTextLong)){return{preview:createDocumentFragment(),result:null};}
const expression=JavaScriptREPL.preprocessExpression(text);const options={expression:expression,generatePreview:true,includeCommandLineAPI:true,throwOnSideEffect:throwOnSideEffect,timeout:timeout,objectGroup:objectGroup,disableBreaks:true,replMode:true};const result=await executionContext.evaluate(options,false,false);const preview=JavaScriptREPL._buildEvaluationPreview(result,allowErrors);return{preview,result};}
static _buildEvaluationPreview(result,allowErrors){const fragment=createDocumentFragment();if(result.error){return fragment;}
if(result.exceptionDetails&&result.exceptionDetails.exception&&result.exceptionDetails.exception.description){const exception=result.exceptionDetails.exception.description;if(exception.startsWith('TypeError: ')||allowErrors){fragment.createChild('span').textContent=result.exceptionDetails.text+' '+exception;}
return fragment;}
const formatter=new RemoteObjectPreviewFormatter();const{preview,type,subtype,description}=result.object;if(preview&&type==='object'&&subtype!=='node'){formatter.appendObjectPreview(fragment,preview,false);}else{const nonObjectPreview=formatter.renderPropertyPreview(type,subtype,description.trimEndWithMaxLength(400));fragment.appendChild(nonObjectPreview);}
return fragment;}}
export const MaxLengthForEvaluation=2000;