{"version":3,"file":"groupedOrderBy.cjs","sources":["../../../src/operators/groupedOrderBy.ts"],"sourcesContent":["import { groupedTopKWithFractionalIndex } from './groupedTopKWithFractionalIndex.js'\nimport { consolidate } from './consolidate.js'\nimport type { IStreamBuilder, KeyValue } from '../types.js'\n\nexport interface GroupedOrderByOptions<Ve> {\n  comparator?: (a: Ve, b: Ve) => number\n  limit?: number\n  offset?: number\n}\n\nexport interface GroupedOrderByWithFractionalIndexOptions<\n  Ve,\n  KeyType = unknown,\n  ValueType = unknown,\n> extends GroupedOrderByOptions<Ve> {\n  setSizeCallback?: (getSize: () => number) => void\n  setWindowFn?: (\n    windowFn: (options: { offset?: number; limit?: number }) => void,\n  ) => void\n  /**\n   * Function to extract a group key from the element's key and value.\n   * Elements with the same group key will be sorted and limited together.\n   */\n  groupKeyFn: (key: KeyType, value: ValueType) => unknown\n}\n\n/**\n * Orders the elements per group and limits the number of results per group, with optional offset and\n * annotates the value with a fractional index.\n * This requires a keyed stream, and uses the `groupedTopKWithFractionalIndex` operator to order elements within each group.\n *\n * Elements are grouped by the provided groupKeyFn, and each group maintains its own sorted collection\n * with independent limit/offset.\n *\n * @param valueExtractor - A function that extracts the value to order by from the element\n * @param options - Configuration including groupKeyFn, comparator, limit, and offset\n * @returns A piped operator that orders the elements per group and limits the number of results per group\n */\nexport function groupedOrderByWithFractionalIndex<\n  T extends KeyValue<unknown, unknown>,\n  Ve = unknown,\n>(\n  valueExtractor: (\n    value: T extends KeyValue<unknown, infer V> ? V : never,\n  ) => Ve,\n  options: GroupedOrderByWithFractionalIndexOptions<\n    Ve,\n    T extends KeyValue<infer K, unknown> ? K : never,\n    T extends KeyValue<unknown, infer V> ? V : never\n  >,\n) {\n  type KeyType = T extends KeyValue<infer K, unknown> ? K : never\n  type ValueType = T extends KeyValue<unknown, infer V> ? V : never\n\n  const limit = options.limit ?? Infinity\n  const offset = options.offset ?? 0\n  const setSizeCallback = options.setSizeCallback\n  const setWindowFn = options.setWindowFn\n  const groupKeyFn = options.groupKeyFn\n  const comparator =\n    options.comparator ??\n    ((a, b) => {\n      // Default to JS like ordering\n      if (a === b) return 0\n      if (a < b) return -1\n      return 1\n    })\n\n  return (\n    stream: IStreamBuilder<T>,\n  ): IStreamBuilder<[KeyType, [ValueType, string]]> => {\n    // Cast to the expected key type for groupedTopKWithFractionalIndex\n    type StreamKey = KeyType extends string | number ? KeyType : string | number\n\n    return stream.pipe(\n      groupedTopKWithFractionalIndex<StreamKey, ValueType>(\n        (a: ValueType, b: ValueType) =>\n          comparator(valueExtractor(a), valueExtractor(b)),\n        {\n          limit,\n          offset,\n          setSizeCallback,\n          setWindowFn,\n          groupKeyFn: groupKeyFn as (\n            key: StreamKey,\n            value: ValueType,\n          ) => unknown,\n        },\n      ) as (\n        stream: IStreamBuilder<T>,\n      ) => IStreamBuilder<[KeyType, [ValueType, string]]>,\n      consolidate(),\n    )\n  }\n}\n"],"names":["groupedTopKWithFractionalIndex","consolidate"],"mappings":";;;;AAsCO,SAAS,kCAId,gBAGA,SAKA;AAIA,QAAM,QAAQ,QAAQ,SAAS;AAC/B,QAAM,SAAS,QAAQ,UAAU;AACjC,QAAM,kBAAkB,QAAQ;AAChC,QAAM,cAAc,QAAQ;AAC5B,QAAM,aAAa,QAAQ;AAC3B,QAAM,aACJ,QAAQ,eACP,CAAC,GAAG,MAAM;AAET,QAAI,MAAM,EAAG,QAAO;AACpB,QAAI,IAAI,EAAG,QAAO;AAClB,WAAO;AAAA,EACT;AAEF,SAAO,CACL,WACmD;AAInD,WAAO,OAAO;AAAA,MACZA,+BAAAA;AAAAA,QACE,CAAC,GAAc,MACb,WAAW,eAAe,CAAC,GAAG,eAAe,CAAC,CAAC;AAAA,QACjD;AAAA,UACE;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QAAA;AAAA,MAIF;AAAA,MAIFC,YAAAA,YAAA;AAAA,IAAY;AAAA,EAEhB;AACF;;"}