/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/analyze-sgf@0.4.8/src/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
#!/usr/bin/env node
const fs=require("fs"),chalk=require("chalk"),jschardet=require("jschardet"),iconv=require("iconv-lite"),getopts=require("./getopts"),sgfconv=require("./sgfconv"),katagoconv=require("./katagoconv"),katago=require("./katago"),GameTree=require("./gametree"),toSGF=require("./gib2sgf"),{httpgetSGF:httpgetSGF,isValidURL:isValidURL}=require("./httpget-sgf"),log=e=>console.error(chalk.grey(e)),getExt=e=>e.substring(1+e.lastIndexOf("."),e.length).toLowerCase(),opts=getopts();function processJSON(e){if("json"!==getExt(e))return log(`skipped: ${e}`),{};const t=fs.readFileSync(e).toString(),s=t.indexOf("\n"),o=sgfconv.correctSGFDialects(t.substring(0,s));return{newPath:e,responses:t.substring(s+1),sgf:o}}async function processSGF(e){const t=getExt(e),s=isValidURL(e);if("sgf"!==t&&"gib"!==t&&!s)return log(`skipped: ${e}`),{};const{newPath:o,sgf:r}=getNewPathAndSGF(s,e,t);return{newPath:o,responses:await katago.analyze(katagoconv.sgfToKataGoAnalysisQuery(r,opts.analysis),opts.revisit,opts.sgf.minWinrateDropForVariations/100,opts.katago),sgf:r}}function getNewPathAndSGF(e,t,s){if(e){const e=httpgetSGF(t),s=sgfconv.prettyPathFromSGF(e);return fs.writeFileSync(s,e),log(`downloaded: ${s}`),{sgf:e,newPath:s}}const o=fs.readFileSync(t),r=jschardet.detect(o),n=iconv.decode(o,r.encoding).toString();return{sgf:"gib"===s?toSGF(n):sgfconv.correctSGFDialects(n),newPath:t}}function saveAnalyzed(e,t,s,o,r){if(!e||!t||!s)return;if(0===s.search('{"error"'))throw Error(s.slice(0,-1));const n=e.substring(0,e.lastIndexOf("."));if(o){const e=`${n}.json`;fs.writeFileSync(e,`${sgfconv.removeTails(t)}\n${s}`),log(`generated: ${e}`)}const i=new GameTree(t,s,r),a=`${n}${r.fileSuffix}.sgf`;fs.writeFileSync(a,i.getSGF()),log(`generated: ${a}`);const g=i.getReport();g&&console.log(g)}(async()=>{for(const e of opts.paths)try{const{newPath:t,sgf:s,responses:o}=opts.jsonGiven?processJSON(e):await processSGF(e);saveAnalyzed(t,s,o,opts.saveGiven,opts.sgf)}catch(t){log(`${t.message}, while processing: ${e}`)}})();
//# sourceMappingURL=/sm/ba1f526b75a2f973844b7616504e650a65de677aa38ee17fafcaa630a0cf07b7.map