{"version":3,"file":"_randomArray.mjs","names":[],"sources":["../../src/internal/_randomArray.ts"],"sourcesContent":["import type { OpenApi } from \"@typia/interface\";\n\nimport { _isUniqueItems } from \"./_isUniqueItems\";\nimport { _randomInteger } from \"./_randomInteger\";\n\nconst DEFAULT_MIN_ITEMS = 1;\nconst DEFAULT_RANGE = 5;\nconst DEFAULT_RECURSIVE_RANGE = 2;\n\n/**\n * Consecutive duplicates that end a unique draw once its floor is satisfied.\n *\n * For independent uniform draws with one unseen value among four, sixty-four\n * misses in a row have probability `0.75 ** 64`. This stopping rule does not\n * prove domain exhaustion. Below the floor it is disabled, but the total\n * attempt budget can still end before a reachable minimum is observed.\n */\nconst STALE_LIMIT = 64;\n\n/**\n * Generate a random array from an array schema.\n *\n * The length is drawn between the bounds, with a default minimum of one, or\n * zero on a recursive cycle. For a unique array the count is a preference above\n * the minimum: drawing stops at the total attempt budget or, after reaching the\n * minimum, a run of duplicates. A result below the minimum throws even when\n * enough distinct values exist but were not observed within that budget.\n *\n * The optional source supplies draws in [0, 1); it defaults to the platform\n * source resolved when this helper is called. Nested draws use the same\n * source.\n *\n * @evidence contracts/common.md#principled-implementation The length is drawn between the schema's bounds with a default minimum of one, or zero on a recursive cycle so graph-shaped data ends, and elements come from the supplied callback. Unique generation stops at the requested count, count * 100 + 1000 total attempts, or sixty-four consecutive duplicates after the minimum is met. These bounded observations do not prove domain exhaustion; a result below the minimum throws even if the callback could produce more distinct values. A returned result always meets the minimum, while the chosen count is a preference above it.\n * @evidence contracts/common.md#clear-and-simple-design One function with a plain branch and a unique branch; defaults are module constants.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts The unique branch accepts a result shorter than the preferred count when it meets the minimum. Failure means the minimum was not observed within the finite attempt budget, rather than proving it unreachable; no callback domain is inferred from repeated duplicates.\n * @evidence contracts/common.md#meaningful-documentation The doc states the length rule, the unique-draw budget and the stale limit, and the module comments explain the reasoning.\n */\nexport const _randomArray = <T>(\n  props: Omit<OpenApi.IJsonSchema.IArray, \"items\"> & {\n    element: (index: number, count: number) => T;\n    recursive?: boolean;\n  },\n  source: () => number = Math.random,\n) => {\n  const defaultMinimum: number =\n    props.recursive === true ? 0 : DEFAULT_MIN_ITEMS;\n  const minimum: number =\n    props.minItems ??\n    Math.min(props.maxItems ?? defaultMinimum, defaultMinimum);\n  const count: number = _randomInteger(\n    {\n      type: \"integer\",\n      minimum,\n      maximum:\n        props.maxItems ??\n        minimum +\n          (props.recursive === true ? DEFAULT_RECURSIVE_RANGE : DEFAULT_RANGE),\n    },\n    source,\n  );\n  if (props.uniqueItems !== true)\n    return new Array(count).fill(null).map((_, i) => props.element(i, count));\n  // How many distinct values the element can take is not knowable from here --\n  // `element` is a closure over whatever the transform emitted, from `boolean`\n  // to a recursive object -- so the count above is a preference, not a\n  // contract. Stop at the bounded attempt budget or duplicate-run limit, then\n  // keep what was reached if it meets the minimum. Neither limit proves that\n  // the callback's domain has nothing new left.\n  //\n  // Failing on that budget instead is what made `Array<boolean> &\n  // UniqueItems` throw on two draws in three: `[true, false]` satisfies the\n  // declaration at every count, and a count of five is unreachable for a\n  // domain of two however many times it is redrawn. Only a result below the\n  // floor is a real failure, which is still the honest outcome for a window\n  // like `MinItems<3>` over a domain of two.\n  //\n  // Below the floor the whole budget is available, though a rare or biased\n  // callback can still miss enough distinct values before it ends. Above the\n  // floor further attempts only increase length, so `STALE_LIMIT` consecutive\n  // duplicates end that search early. This bounds repeated work for nested\n  // small-domain unique arrays without claiming the domain is exhausted.\n  const elements: T[] = [];\n  const maximumAttempts: number = count * 100 + 1000;\n  let stale: number = 0;\n  for (\n    let attempts = 0;\n    elements.length !== count && attempts !== maximumAttempts;\n    attempts++\n  ) {\n    const candidate: T = props.element(elements.length, count);\n    if (elements.every((element) => _isUniqueItems([element, candidate]))) {\n      elements.push(candidate);\n      stale = 0;\n    } else if (elements.length >= minimum && ++stale === STALE_LIMIT) break;\n  }\n  if (elements.length < minimum)\n    throw new Error(\n      \"Unable to generate enough unique items; the element domain may be too small.\",\n    );\n  return elements;\n};\n"],"mappings":";;;AAKA,MAAM,oBAAoB;AAC1B,MAAM,gBAAgB;AACtB,MAAM,0BAA0B;;;;;;;;;AAUhC,MAAM,cAAc;;;;;;;;;;;;;;;;;;;AAoBpB,MAAa,gBACX,OAIA,SAAuB,KAAK,WACzB;CACH,MAAM,iBACJ,MAAM,cAAc,OAAO,IAAI;CACjC,MAAM,UACJ,MAAM,YACN,KAAK,IAAI,MAAM,YAAY,gBAAgB,cAAc;CAC3D,MAAM,QAAgB,eACpB;EACE,MAAM;EACN;EACA,SACE,MAAM,YACN,WACG,MAAM,cAAc,OAAO,0BAA0B;CAC5D,GACA,MACF;CACA,IAAI,MAAM,gBAAgB,MACxB,OAAO,IAAI,MAAM,KAAK,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,KAAK,GAAG,MAAM,MAAM,QAAQ,GAAG,KAAK,CAAC;CAoB1E,MAAM,WAAgB,CAAC;CACvB,MAAM,kBAA0B,QAAQ,MAAM;CAC9C,IAAI,QAAgB;CACpB,KACE,IAAI,WAAW,GACf,SAAS,WAAW,SAAS,aAAa,iBAC1C,YACA;EACA,MAAM,YAAe,MAAM,QAAQ,SAAS,QAAQ,KAAK;EACzD,IAAI,SAAS,OAAO,YAAY,eAAe,CAAC,SAAS,SAAS,CAAC,CAAC,GAAG;GACrE,SAAS,KAAK,SAAS;GACvB,QAAQ;EACV,OAAO,IAAI,SAAS,UAAU,WAAW,EAAE,UAAU,aAAa;CACpE;CACA,IAAI,SAAS,SAAS,SACpB,MAAM,IAAI,MACR,8EACF;CACF,OAAO;AACT"}