/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@components-1812/json-visualizer@0.0.2/src/JSONVisualizer.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
import{JSONLine}from"./JSONLine.js";import{JSONBlock}from"./JSONBlock.js";import JSONTokenizer from"./JSONTokenizer.js";import{CopyButton}from"./CopyButton.js";export class JSONVisualizer extends HTMLElement{static version="0.0.2";static stylesSheets={links:[],adopted:[],raw:[]};static getTokens=async t=>{const e=new JSONTokenizer;return e.tokenize(t),e.tokens};static defaults={renderDeep:1/0};#t=null;#e=null;#n=null;constructor(){super(),this.attachShadow({mode:"open"}),this.shadowRoot.innerHTML='\n\t\t\t<div class="JSONVisualizer"></div>\n\t\t\t<slot name="icons">\n\t\t\t\t<template>\n\t\t\t\t\t<svg data-name="toggle" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">\n\t\t\t\t\t\t<path fill-rule="evenodd" d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708"/>\n\t\t\t\t\t</svg>\n\t\t\t\t\t<svg data-name="copy" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">\n\t\t\t\t\t\t<path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1z"/>\n\t\t\t\t\t\t<path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0z"/>\n\t\t\t\t\t</svg>\n\t\t\t\t\t<svg data-name="copy-done" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">\n\t\t\t\t\t\t<path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/>\n\t\t\t\t\t</svg>\n\t\t\t\t</template>\n\t\t\t</slot>\n\t\t',Promise.allSettled(JSONVisualizer.stylesSheets.links.map((t=>{const e=document.createElement("link");e.rel="stylesheet",e.href=t;const{promise:n,resolve:s,reject:o}=Promise.withResolvers();return e.addEventListener("load",(()=>s({link:e,href:t,status:"loaded"}))),e.addEventListener("error",(()=>o({link:e,href:t,status:"error"}))),this.shadowRoot.prepend(e),n}))).then((t=>{this.dispatchEvent(new CustomEvent("ready-links",{detail:{results:t.map((t=>t.value||t.reason))}})),this.setAttribute("ready-links","")})),JSONVisualizer.stylesSheets.raw.forEach((t=>{const e=document.createElement("style");e.textContent=t,this.shadowRoot.prepend(e)})),this.shadowRoot.adoptedStyleSheets=JSONVisualizer.stylesSheets.adopted}static observedAttributes=["json","src"];attributeChangedCallback(t,e,n){"src"===t&&n!==e&&this.loadJSON(n),"json"===t&&n!==e&&this.renderJSON(n)}connectedCallback(){this.src||this.json||(this.json=this.textContent),"none"!==this.copyButton&&(this.#e=new CopyButton({data:()=>this.data,icons:{copy:this.getIcon("copy",{clone:!0}),copyDone:this.getIcon("copy-done",{clone:!0})}}),this.shadowRoot.append(this.#e.render())),this.addEventListener("toggle-lines",this.#s),this.dispatchEvent(new CustomEvent("ready")),this.setAttribute("ready","")}disconnectedCallback(){this.#e?.dispose(),this.#e=null,this.clearJSON(),this.clearListeners()}async loadJSON(t,e={}){const{method:n}=e;try{const e=await fetch(t,{method:n||"GET"});if(e.ok){const t=await e.json();this.json=t}}catch(t){}}async#o(t){if(!JSONVisualizer.getTokens)return void console.warn("JSONVisualizer.getTokens is not defined. Please ensure that the JSONTokenizer");const e=[],n=await JSONVisualizer.getTokens(t);let s=null,o=0,i=1;for(let t=0;t<n.length;t++){const{type:r,value:l,tags:a}=n.at(t);if(["brace-close","bracket-close"].includes(r)){o--;const e=n.at(t-1);["brace-open","bracket-open","comma"].includes(e?.type)||(s=null)}if(s||(s=new JSONLine({level:o,number:i++}),e.push(s)),s.addToken({type:r,value:l,tags:a}),["brace-close","bracket-close"].includes(r)){const e=n.at(t+1);if("comma"===e?.type){s.addToken(e),t++,s=null;continue}}["brace-open","bracket-open"].includes(r)&&o++,["brace-open","brace-close","bracket-open","bracket-close","comma"].includes(r)&&(s=null)}return e}async renderJSON(t,e={}){if(!t)return void console.warn("No JSON provided to renderJSON method.");console.log("render json"),this.clearJSON();const{lineNumbers:n="none"!==this.lineNumbers,toggleLines:s="none"!==this.toggleLines,renderDeep:o=this.renderDeep}=e,i=await this.#o(t),r=[];for(let t=0;t<i.length;t++){const e=i.at(t);if(e.showNumber=n,e.isOpenBlock){const t=new JSONBlock({level:e.level,showContent:e.level<o});t.openLine=e,e.block=t,s&&(e.toggleControl=!0,e.toggleActive=!t.showContent,e.toggleIcon=this.getIcon("toggle",{clone:!0})),r.push(t);continue}if(e.isCloseBlock){const t=r.pop();t.closeLine=e,e.block=t,r.length>0?r.at(-1).content.push(t):this.#t=t;continue}const l=r.at(-1);e.block=l,l.content.push(e)}if(this.shadowRoot.querySelector(".JSONVisualizer").append(this.#t.render()),n){const t=`${String(i.length).length}ch`;this.shadowRoot.querySelector(".JSONVisualizer").style.setProperty("--line-number-min-width",t)}this.setAttribute("ready-json",""),this.dispatchEvent(new CustomEvent("ready-json"))}getIcon(t,{clone:e=!1}={}){const n=this.shadowRoot.querySelector('slot[name="icons"]'),s=n.querySelector("template").content,o=n.assignedNodes().at(0)?.content,i=o?.querySelector(`[data-name="${t}"]`)??s.querySelector(`[data-name="${t}"]`);return e?i.cloneNode(!0):i}clearJSON(){this.removeAttribute("ready-json"),this.#t?.dispose(),this.#t=null}clearListeners(){this.removeEventListener("toggle-lines",this.#s)}#s=t=>{const{line:e}=t.detail;e.block.showContent?e.block.folded?e.block.unfold():e.block.fold():(e.block.showContent=!0,e.block.renderContent())};set json(t){t?(this.#n="string"==typeof t?JSON.parse(t):t,this.setAttribute("json",JSON.stringify(this.#n))):(this.removeAttribute("json"),this.#n=null)}get json(){return this.getAttribute("json")}get data(){return this.#n}set lineNumbers(t){t?this.setAttribute("line-numbers",""):this.removeAttribute("line-numbers")}get lineNumbers(){return this.getAttribute("line-numbers")}set toggleLines(t){t?this.setAttribute("toggle-lines",""):this.removeAttribute("toggle-lines")}get toggleLines(){return this.getAttribute("toggle-lines")}set indentationGuidesLines(t){t?this.setAttribute("indentation-guides-lines",""):this.removeAttribute("indentation-guides-lines")}get indentationGuidesLines(){return this.getAttribute("indentation-guides-lines")}set copyButton(t){t?this.setAttribute("copy-button",""):this.removeAttribute("copy-button")}get copyButton(){return this.getAttribute("copy-button")}set renderDeep(t){t?this.setAttribute("render-deep",t):this.removeAttribute("render-deep")}get renderDeep(){const t=this.getAttribute("render-deep");return!t||Number.isNaN(Number(t))?JSONVisualizer.defaults.renderDeep:Number(t)}set src(t){t?this.setAttribute("src",t):this.removeAttribute("src")}get src(){return this.getAttribute("src")}}export default JSONVisualizer;
//# sourceMappingURL=/sm/2af8eea20bb751e2b58507eec3d685179e2773ecba9b9f228ee8db6dcec83d27.map