"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.isValidSWCHelperFunc=isValidSWCHelperFunc,exports.getHelperDeps=getHelperDeps,exports.getSWCOutputPath=getSWCOutputPath,exports.getHelperContent=getHelperContent,exports.replaceSWCHelpers=replaceSWCHelpers,exports.collectSWCHelpers=collectSWCHelpers,exports.isIgnore=isIgnore;const tslib_1=require("tslib"),path=tslib_1.__importStar(require("path")),fs=tslib_1.__importStar(require("fs-extra")),tools_1=require("./tools"),swc_config_1=require("./swc_config");function isValidSWCHelperFunc(e){return swc_config_1.DepMap.hasOwnProperty(e)}function getHelperDeps(e,r){const t=Array.from(e),n=new Set(e);for(;t.length;){const e=t.shift()||"",s=e.substr(0,e.indexOf(r)+r.length),o=e.replace(s+"/","");o in swc_config_1.DepMap&&swc_config_1.DepMap[o].forEach(e=>{const r=`${s}/${e}`;n.has(r)||(t.push(r),n.add(r))})}return Array.from(n)}function getSWCOutputPath(e){var r;let t=(null===(r=null==e?void 0:e.swcSetting)||void 0===r?void 0:r.outputPath)||"";return t?(t=(0,tools_1.normalizePath)(t),t.replace(/(^[./\\])|(\/$)/g,""),t):"@swc/runtime"}function getHelperContent(e,r){const t=e.substr(0,e.indexOf(r)+r.length),n=e.replace(t,"swc_runtime"),s=(0,tools_1.normalizePath)(path.join(__dirname,"../vendor/",n+".js"));return fs.readFile(s,"utf8")}const SWCHelperRequireReg=/require\("(@swc\/helpers\/_\/([^"]+))"\)/gi;function replaceSWCHelpers(e,r,t){const n=new Set,s=r.split("/").map((e,r)=>0===r?"":"../").join("");return{transformCode:e.replace(SWCHelperRequireReg,(e,r,o)=>{const p=r.replace("@swc/helpers/_",t);return swc_config_1.DepMap.hasOwnProperty(o)?(n.add(p),`require("${s+p}")`):e}),helpers:Array.from(n)}}function collectSWCHelpers(e){const r=new Set;return e.replace(SWCHelperRequireReg,(e,t,n)=>(r.add(n),e)),r}function isIgnore(e,r){const t=e||[],n=r.split(path.sep).join("/");for(const e of t){if(r===e)return!0;if(!/^[./\\]+/.test(e)&&/.+\/\*$/.test(e)&&-1!==n.indexOf(e.substr(0,e.length-1)))return!0}return!1}