import*as i18n from"../i18n/i18n.js";import*as ObjectUI from"../object_ui/object_ui.js";import*as Platform from"../platform/platform.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";const UIStrings={find:"Find"},str_=i18n.i18n.registerUIStrings("source_frame/JSONView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class JSONView extends UI.Widget.VBox{constructor(e,t){super(),this._initialized=!1,this.registerRequiredCSS("source_frame/jsonView.css",{enableLegacyPatching:!1}),this._parsedJSON=e,this._startCollapsed=Boolean(t),this.element.classList.add("json-view"),this._currentSearchFocusIndex=0,this._currentSearchTreeElements=[],this._searchRegex=null}static async createView(e){const t=await JSONView._parseJSON(e);if(!t||"object"!==typeof t.data)return null;const r=new JSONView(t),s=new UI.SearchableView.SearchableView(r,null);return s.setPlaceholder(i18nString(UIStrings.find)),r._searchableView=s,r.show(s.element),s}static createViewSync(e){const t=new JSONView(new ParsedJSON(e,"","")),r=new UI.SearchableView.SearchableView(t,null);return r.setPlaceholder(i18nString(UIStrings.find)),t._searchableView=r,t.show(r.element),t.element.tabIndex=0,r}static _parseJSON(e){let t=null;if(e&&(t=JSONView._extractJSON(e)),!t)return Promise.resolve(null);try{const e=JSON.parse(t.data);if(!e)return Promise.resolve(null);t.data=e}catch(r){t=null}return Promise.resolve(t)}static _extractJSON(e){if(e.startsWith("<"))return null;let t=JSONView._findBrackets(e,"{","}");const r=JSONView._findBrackets(e,"[","]");if(t=r.length>t.length?r:t,-1===t.length||e.length-t.length>80)return null;const s=e.substring(0,t.start),i=e.substring(t.end+1);return e=e.substring(t.start,t.end+1),!i.trim().length||i.trim().startsWith(")")&&s.trim().endsWith("(")?new ParsedJSON(e,s,i):null}static _findBrackets(e,t,r){const s=e.indexOf(t),i=e.lastIndexOf(r);let n=i-s-1;return(-1===s||-1===i||i<s)&&(n=-1),{start:s,end:i,length:n}}wasShown(){this._initialize()}_initialize(){if(this._initialized)return;this._initialized=!0;const e=SDK.RemoteObject.RemoteObject.fromLocalObject(this._parsedJSON.data),t=this._parsedJSON.prefix+e.description+this._parsedJSON.suffix;this._treeOutline=new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection(e,t,void 0,void 0,void 0,void 0,!0),this._treeOutline.enableContextMenu(),this._treeOutline.setEditable(!1),this._startCollapsed||this._treeOutline.expand(),this.element.appendChild(this._treeOutline.element);const r=this._treeOutline.firstChild();r&&r.select(!0,!1)}_jumpToMatch(e){if(!this._searchRegex)return;const t=this._currentSearchTreeElements[this._currentSearchFocusIndex];t&&t.setSearchRegex(this._searchRegex);const r=this._currentSearchTreeElements[e];r?(this._updateSearchIndex(e),r.setSearchRegex(this._searchRegex,UI.UIUtils.highlightedCurrentSearchResultClassName),r.reveal()):this._updateSearchIndex(0)}_updateSearchCount(e){this._searchableView&&this._searchableView.updateSearchMatchesCount(e)}_updateSearchIndex(e){this._currentSearchFocusIndex=e,this._searchableView&&this._searchableView.updateCurrentMatchIndex(e)}searchCanceled(){let e;for(this._searchRegex=null,this._currentSearchTreeElements=[],e=this._treeOutline.rootElement();e;e=e.traverseNextTreeElement(!1))e instanceof ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement&&e.revertHighlightChanges();this._updateSearchCount(0),this._updateSearchIndex(0)}performSearch(e,t,r){let s=this._currentSearchFocusIndex;const i=this._currentSearchTreeElements[s];let n;for(this.searchCanceled(),this._searchRegex=e.toSearchRegex(!0),n=this._treeOutline.rootElement();n;n=n.traverseNextTreeElement(!1)){if(!(n instanceof ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement))continue;const e=n.setSearchRegex(this._searchRegex);if(e&&this._currentSearchTreeElements.push(n),i===n){const t=this._currentSearchTreeElements.length-1;s=e||r?t:t+1}}this._updateSearchCount(this._currentSearchTreeElements.length),this._currentSearchTreeElements.length?(s=Platform.NumberUtilities.mod(s,this._currentSearchTreeElements.length),this._jumpToMatch(s)):this._updateSearchIndex(-1)}jumpToNextSearchResult(){if(!this._currentSearchTreeElements.length)return;const e=Platform.NumberUtilities.mod(this._currentSearchFocusIndex+1,this._currentSearchTreeElements.length);this._jumpToMatch(e)}jumpToPreviousSearchResult(){if(!this._currentSearchTreeElements.length)return;const e=Platform.NumberUtilities.mod(this._currentSearchFocusIndex-1,this._currentSearchTreeElements.length);this._jumpToMatch(e)}supportsCaseSensitiveSearch(){return!0}supportsRegexSearch(){return!0}}export class ParsedJSON{constructor(e,t,r){this.data=e,this.prefix=t,this.suffix=r}}