"use strict";var e=this&&this.__awaiter||function(e,n,o,t){return new(o||(o=Promise))((function(i,d){function s(e){try{l(t.next(e))}catch(e){d(e)}}function r(e){try{l(t.throw(e))}catch(e){d(e)}}function l(e){var n;e.done?i(e.value):(n=e.value,n instanceof o?n:new o((function(e){e(n)}))).then(s,r)}l((t=t.apply(e,n||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.DepNodeSerializer=void 0;const n=require("../../../tools/posh"),o=require("../../../common/message");exports.DepNodeSerializer=class{constructor(e){this.isShowConflict=!!(null==e?void 0:e.isShowConflict),this.filterDepName=null==e?void 0:e.filterDepName,this.filterMaxDepth=void 0===(null==e?void 0:e.filterDepth)?1/0:e.filterDepth,this.format=(null==e?void 0:e.format)||"unicode"}run(n){return e(this,void 0,void 0,(function*(){switch(yield n.walk(this),this.format){case"unicode":return this.buildUnicodeArchyResult(n.context.walkerRootNode,n.context.tree.moduleRootDir);case"json":return this.buildJsonArchyResult(n.context.walkerRootNode,n.context.tree.moduleRootDir);default:throw new Error((0,o.format)(o.Messages.Serializer.InvalidFormat,{format:this.format}))}}))}visit(e,o){if(e.node.isRoot)return;const t=o.tree.getVersionSet(e.node.name),i=t&&t.size>1?Array.from(t).join(", "):"",d=e.node.name?`${e.node.name}`:".";e.node.label=`${d} ${n.posh.blackBright(e.node.pinnedSpec)}`,!e.node.isRoot&&i&&this.isShowConflict&&(e.node.suffixLabel=` ${n.posh.yellowBright.bgBlack(`conflict versions: ${i}`)}`),e.node.hasConflictVersion=!!i,this.filterDepName===e.node.name&&(e.isDisplayed=!0,e.node.brightLabel()),!this.filterDepName&&this.filterMaxDepth>=e.depth&&(e.isDisplayed=!0)}buildUnicodeArchy(e,n){const o=(e,n,t)=>{if(t.add(e.node.nodeKey),e.depth>=this.filterMaxDepth||!e.children||0===e.children.length)t.delete(e.node.nodeKey);else{for(const i of e.children){if(t.has(i.node.nodeKey))continue;const d={label:`${i.node.prefixLabel}${i.node.label}${i.node.suffixLabel}`,nodes:[]};o(i,d,t),e.isDisplayed||(e.isDisplayed=i.isDisplayed),i.isDisplayed&&n.nodes.push(d)}t.delete(e.node.nodeKey)}};return o(e,n,new Set)}buildJsonArchy(e,n,o){const t=(e,n,i)=>{var d;if(i.add(e.node.nodeKey),e.depth>=this.filterMaxDepth||!e.children||0===e.children.length)i.delete(e.node.nodeKey);else{for(const s of e.children){if(i.has(s.node.nodeKey))continue;const r={version:s.node.unmet?void 0:s.node.pinnedSpec,unmet:s.node.unmet,realPath:s.node.resolvePkgStoreDir(o),depType:s.node.depType,faults:s.node.unmet?[s.node.fault||""]:void 0};t(s,r,i),e.isDisplayed||(e.isDisplayed=s.isDisplayed),n.dependencies||(n.dependencies={}),s.isDisplayed&&(null===(d=n.faults)||void 0===d||d.push(...r.faults||[]),n.dependencies[s.node.name]=r)}i.delete(e.node.nodeKey)}};return t(e,n,new Set)}buildUnicodeArchyResult(e,o){const t=e.node.name?" ":"",i={label:`${e.node.prefixLabel}${e.node.label}${e.node.suffixLabel}${t}${n.posh.blackBright(e.node.resolvePkgStoreDir(o)||"")}`,nodes:[]};return this.buildUnicodeArchy(e,i),0===i.nodes.length&&i.nodes.push("(empty)"),i}buildJsonArchyResult(e,n){const o={name:e.node.name,version:e.node.pinnedSpec,faults:[],depType:e.node.depType,realPath:e.node.resolvePkgStoreDir(n),dependencies:{}};return this.buildJsonArchy(e,o,n),o}};