{"version":3,"file":"ListBatchesController.mjs","names":[],"sources":["../../../../../../../../components-base/src/list/pagination/ListBatchesController.ts"],"sourcesContent":["import type { ListModelContext } from \"../ListModelContext\";\nimport type { ListBatchesControllerShape, ListLoaderModes } from \"./types\";\n\n/**\n * The context a batches controller needs: the table, plus who does the paging.\n *\n * Declared here rather than folded into `ListModelContext`, so a model that\n * only sorts or filters is not made to carry a loader it never asks anything.\n */\nexport interface ListPaginationContext<T> extends ListModelContext<T> {\n  readonly loader: ListLoaderModes;\n}\n\n/**\n * How much of the list is shown, in batches of `batchSize`.\n *\n * Resetting on a changed filter or search is **not** wired here: the filters\n * and the search belong to the list that assembles them, and it is the list\n * that calls `reset()`. Keeping the wiring out also keeps the variance trap out\n * — a list of concrete filters is not assignable to a list of generic ones.\n */\nexport class ListBatchesController<T> {\n  public readonly batchSize: number;\n  private readonly context: ListPaginationContext<T>;\n\n  public constructor(\n    context: ListPaginationContext<T>,\n    shape: ListBatchesControllerShape = {},\n  ) {\n    const { batchSize = 20 } = shape;\n\n    this.context = context;\n    this.batchSize = batchSize;\n  }\n\n  private get dataTable() {\n    return this.context.dataTable;\n  }\n\n  public getBatchIndex(): number {\n    return this.dataTable.getState().pagination.pageIndex;\n  }\n\n  public hasNextBatch(): boolean {\n    if (this.context.loader.manualPagination) {\n      return this.dataTable.getCanNextPage();\n    }\n    return (\n      this.dataTable.getState().pagination.pageSize < this.getTotalItemsCount()\n    );\n  }\n\n  public getTotalItemsCount(): number {\n    if (this.context.loader.manualPagination) {\n      return this.dataTable.getRowCount();\n    }\n    return this.dataTable.getFilteredRowModel().rows.length;\n  }\n\n  public getVisibleItemsCount(): number {\n    return this.dataTable.getRowModel().rows.length ?? 0;\n  }\n\n  public updateItemTotalCount(value: number): void {\n    this.dataTable.setOptions((s) => ({\n      ...s,\n      rowCount: value,\n    }));\n  }\n\n  public reset(): void {\n    if (this.context.loader.manualFiltering) {\n      this.updateItemTotalCount(0);\n    }\n    this.dataTable.setPagination(() => ({\n      pageIndex: 0,\n      pageSize: this.batchSize,\n    }));\n  }\n\n  public nextBatch(): void {\n    if (this.context.loader.manualPagination) {\n      this.dataTable.nextPage();\n      return;\n    }\n\n    this.dataTable.setPagination((prev) => ({\n      ...prev,\n      pageSize: prev.pageSize + this.batchSize,\n    }));\n  }\n}\n"],"mappings":";;;;;;;;;;;AAqBA,IAAa,wBAAb,MAAsC;CACpC;CACA;CAEA,YACE,SACA,QAAoC,CAAC,GACrC;EACA,MAAM,EAAE,YAAY,OAAO;EAE3B,KAAK,UAAU;EACf,KAAK,YAAY;CACnB;CAEA,IAAY,YAAY;EACtB,OAAO,KAAK,QAAQ;CACtB;CAEA,gBAA+B;EAC7B,OAAO,KAAK,UAAU,SAAS,CAAC,CAAC,WAAW;CAC9C;CAEA,eAA+B;EAC7B,IAAI,KAAK,QAAQ,OAAO,kBACtB,OAAO,KAAK,UAAU,eAAe;EAEvC,OACE,KAAK,UAAU,SAAS,CAAC,CAAC,WAAW,WAAW,KAAK,mBAAmB;CAE5E;CAEA,qBAAoC;EAClC,IAAI,KAAK,QAAQ,OAAO,kBACtB,OAAO,KAAK,UAAU,YAAY;EAEpC,OAAO,KAAK,UAAU,oBAAoB,CAAC,CAAC,KAAK;CACnD;CAEA,uBAAsC;EACpC,OAAO,KAAK,UAAU,YAAY,CAAC,CAAC,KAAK,UAAU;CACrD;CAEA,qBAA4B,OAAqB;EAC/C,KAAK,UAAU,YAAY,OAAO;GAChC,GAAG;GACH,UAAU;EACZ,EAAE;CACJ;CAEA,QAAqB;EACnB,IAAI,KAAK,QAAQ,OAAO,iBACtB,KAAK,qBAAqB,CAAC;EAE7B,KAAK,UAAU,qBAAqB;GAClC,WAAW;GACX,UAAU,KAAK;EACjB,EAAE;CACJ;CAEA,YAAyB;EACvB,IAAI,KAAK,QAAQ,OAAO,kBAAkB;GACxC,KAAK,UAAU,SAAS;GACxB;EACF;EAEA,KAAK,UAAU,eAAe,UAAU;GACtC,GAAG;GACH,UAAU,KAAK,WAAW,KAAK;EACjC,EAAE;CACJ;AACF"}