import{checkArg as t}from"./checkArg.js";import{map as a}from"./iterable.js";export function delay(t=0){return new Promise((a=>{setTimeout(a,t)}))}export async function timeout(t,a,i){let e=!1;return new Promise((async(r,s)=>{setTimeout((()=>{e||s("string"==typeof i?new Error(i):i||new Error("Timed out while waiting for promise to resolve."))}),t);try{r(await a)}catch(t){s(t)}finally{e=!0}}))}export async function map(i,e){t("values",i).isNotMissing(),t("mapper",e).isNotMissing();const r=Array.from(a(await i,(async(t,a)=>e(await t,a))));return Promise.all(r)}export async function each(a,i){t("values",a).isNotMissing(),t("iterator",i).isNotMissing();const e=await a;let r=0;for(const t of e)await i(await t,r++)}export async function parallelEach(t,a){await map(t,a)}export function reduce(a,i,e){const r=arguments.length;return(async()=>{t("values",a).isNotMissing(),t("reducer",i).isNotMissing();const s=[...await a];let n=e;if(r<3){if(s.length<2)return;n=await s.shift()}for(let t=0;t<s.length;t++)n=await i(n,await s[t],t);return n})()}export async function props(a){t("object",a).isNotMissing().satisfies((t=>"object"==typeof t));const i={},e=await a;return await parallelEach(Object.keys(e),(async t=>{const a=await e[t];e.hasOwnProperty(t)&&(i[t]=a)})),i}export async function asyncFlatMap(t,a){return(await Promise.all(t.map(a))).flat()}