"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,o){void 0===o&&(o=r);var n=Object.getOwnPropertyDescriptor(t,r);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,o,n)}:function(e,t,r,o){void 0===o&&(o=r),e[o]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&__createBinding(t,e,r);return __setModuleDefault(t,e),t};Object.defineProperty(exports,"__esModule",{value:!0});const node_worker_threads_1=require("node:worker_threads"),node_url_1=require("node:url"),symbols_1=require("./symbols"),common_1=require("./common");common_1.commonState.isWorkerThread=!0,common_1.commonState.workerData=node_worker_threads_1.workerData;const handlerCache=new Map;let importESMCached,useAtomics="1"!==process.env.PISCINA_DISABLE_ATOMICS;function getImportESM(){return void 0===importESMCached&&(importESMCached=new Function("specifier","return import(specifier)")),importESMCached}async function getHandler(e,t){let r=handlerCache.get(`${e}/${t}`);if(void 0!==r)return r;try{r=await Promise.resolve(`${e}`).then(e=>__importStar(require(e))),"function"!=typeof r&&(r=await r[t])}catch{}if("function"!=typeof r&&(r=await getImportESM()((0,node_url_1.pathToFileURL)(e).href),"function"!=typeof r&&(r=await r[t])),"function"!=typeof r)return null;if(handlerCache.size>1e3){const[[e]]=handlerCache;handlerCache.delete(e)}return handlerCache.set(`${e}/${t}`,r),r}node_worker_threads_1.parentPort.on("message",e=>{useAtomics="1"!==process.env.PISCINA_DISABLE_ATOMICS&&e.useAtomics;const{port:t,sharedBuffer:r,filename:o,name:n,niceIncrement:s}=e;(async function(){try{0!==s&&(await Promise.resolve().then(()=>__importStar(require("@napi-rs/nice")))).nice(s)}catch{}null!==o&&await getHandler(o,n);const e={[common_1.READY]:!0};node_worker_threads_1.parentPort.postMessage(e),t.on("message",onMessage.bind(null,t,r)),atomicsWaitLoop(t,r)})().catch(throwInNextTick)});let currentTasks=0,lastSeenRequestCount=0;function atomicsWaitLoop(e,t){if(useAtomics)for(;0===currentTasks;){let r;for(Atomics.wait(t,symbols_1.kRequestCountField,lastSeenRequestCount),lastSeenRequestCount=Atomics.load(t,symbols_1.kRequestCountField);void 0!==(r=(0,node_worker_threads_1.receiveMessageOnPort)(e));)onMessage(e,t,r.message)}}function onMessage(e,t,r){currentTasks++;const{taskId:o,task:n,filename:s,name:a}=r;(async function(){let r,i=[];try{const e=await getHandler(s,a);if(null===e)throw new Error(`No handler function exported from ${s}`);let t=await e(n);(0,common_1.isMovable)(t)&&(i=i.concat(t[symbols_1.kTransferable]),t=t[symbols_1.kValue]),r={taskId:o,result:t,error:null},process.stdout.writableLength>0&&await new Promise(e=>process.stdout.write("",e)),process.stderr.writableLength>0&&await new Promise(e=>process.stderr.write("",e))}catch(e){r={taskId:o,result:null,error:e}}currentTasks--,e.postMessage(r,i),Atomics.add(t,symbols_1.kResponseCountField,1),atomicsWaitLoop(e,t)})().catch(throwInNextTick)}function throwInNextTick(e){process.nextTick(()=>{throw e})}