import{ArgumentError as n}from"../ArgumentError.js";import{checkArg as t}from"./checkArg.js";export function flatten(n){return[].concat(...n)}export function range(n,t){return t<n?[]:[...new Array(t-n+1)].map(((t,o)=>o+n))}export function count(n,t){return n.filter(t).length}export function remove(n,t){if(!n)return!1;const o=n.indexOf(t);return o>=0&&(n.splice(o,1),!0)}export function chunk(n,t){const o=[];for(let r=0;r<n.length;r+=t)o.push(n.slice(r,r+t));return o}export function groupBy(n,o){t("array",n).isNotMissing(),t("getKey",o).isNotMissing();const r=new Map;return n.forEach(((n,t)=>{const e=o(n,t),i=r.get(e)||[];i.push(n),r.set(e,i)})),r}export async function compareAsync(n,t,r){if(!r)return;const e=r.equals??((n,t)=>n===t),i=[],s=o(n),c=o(t);if(r.onMatch||r.onMissing)for(let n=0;n<s.length;n++){const t=s[n];let o;const u=c.findIndex((n=>{const r=e(t,n);return r&&(o=n),r}));r.onMatch&&u>=0?(i.push(u),await r.onMatch(t,o,n,u)):r.onMissing&&u<0&&await r.onMissing(t,n)}if(r.onNew)for(let n=0;n<c.length;n++){if(i.includes(n))continue;const t=c[n];void 0===s.find((n=>e(n,t)))&&await r.onNew(t,n)}}export function compare(n,t,r){if(!r)return;const e=r.equals||((n,t)=>n===t),i=[],s=[],c=o(n),u=o(t);if(r.onMatch||r.onMissing)for(let n=0;n<c.length;n++){const t=c[n];let o;const f=u.findIndex((n=>{const r=e(t,n);return r&&(o=n),r}));if(r.onMatch&&f>=0){s.push(f);const e=r.onMatch(t,o,n,f);e&&i.push(e)}else if(r.onMissing&&f<0){const o=r.onMissing(t,n);o&&i.push(o)}}if(r.onNew)for(let n=0;n<u.length;n++){if(s.includes(n))continue;const t=u[n];if(void 0===c.find((n=>e(n,t)))){const o=r.onNew(t,n);o&&i.push(o)}}}function o(t){if(t){if(Array.isArray(t))return t;if(t[Symbol.iterator])return[...t];if("function"==typeof t.toArray)return t.toArray();throw new n("Unable to convert value to an array.")}return[]}