"use strict";const{getBooleanOption,cppdb}=require("../util");module.exports=function(e,t){if("string"!=typeof e)throw new TypeError("Expected first argument to be a string");if("object"!=typeof t||null===t)throw new TypeError("Expected second argument to be an options object");if(!e)throw new TypeError("User-defined function name cannot be an empty string");const n="start"in t?t.start:null,o=getFunctionOption(t,"step",!0),r=getFunctionOption(t,"inverse",!1),i=getFunctionOption(t,"result",!1),p="safeIntegers"in t?+getBooleanOption(t,"safeIntegers"):2,g=getBooleanOption(t,"deterministic"),s=getBooleanOption(t,"directOnly");let a=-1;if(!getBooleanOption(t,"varargs")&&(a=Math.max(getLength(o),r?getLength(r):0),a>0&&(a-=1),a>100))throw new RangeError("User-defined functions cannot have more than 100 arguments");return this[cppdb].aggregate(n,o,r,i,e,a,p,g,s),this};const getFunctionOption=(e,t,n)=>{const o=t in e?e[t]:null;if("function"==typeof o)return o;if(null!=o)throw new TypeError(`Expected the "${t}" option to be a function`);if(n)throw new TypeError(`Missing required option "${t}"`);return null},getLength=({length:e})=>{if(Number.isInteger(e)&&e>=0)return e;throw new TypeError("Expected function.length to be a positive integer")};