{"version":3,"file":"abstract-interaction-batch.mjs","sources":["../../../../../src/core/interaction/batch/abstract-interaction-batch.ts"],"sourcesContent":["import type { IProcessingStrategy } from './processing/interface-strategy'\nimport type {\n  BatchCommandsArrayUniversal,\n  BatchCommandsObjectUniversal,\n  BatchCommandV3, BatchNamedCommandsUniversal,\n  ICallBatchOptions, ICallBatchResult\n} from '../../../types/http'\nimport type { RestrictionManager } from '../../http/limiters/manager'\nimport type { PayloadTime } from '../../../types/payloads'\nimport type { Result } from '../../result'\nimport type { AjaxResult } from '../../http/ajax-result'\nimport type { NumberString } from '../../../types/common'\nimport type { TypeDescriptionError } from '../../../types/auth'\nimport { SdkError } from '../../sdk-error'\n\nexport interface BatchResponseData<T = unknown> {\n  readonly result?: T[] | Record<string | number, T>\n  readonly result_error?: (string | TypeDescriptionError)[] | Record<string | number, string | TypeDescriptionError>\n  readonly result_total?: NumberString[] | Record<string | number, NumberString>\n  readonly result_next?: NumberString[] | Record<string | number, NumberString>\n  readonly result_time?: PayloadTime[] | Record<string | number, PayloadTime>\n}\n\n/**\n * What a `batch` call's `result` field actually holds, i.e. what\n * `AjaxResult.getData()!.result` returns for a batch response.\n *\n * `AjaxResult<X>` already means \"the payload is `{ result: X, time }`\", so the\n * type argument is the INNER value, not the whole envelope. This used to be\n * written `AjaxResult<BatchPayload<T>>`, which described one envelope too many\n * (`{ result: { result: …, time }, time }`) — every consumer then had to launder\n * the difference through `as unknown as`, and those casts were load-bearing\n * rather than cosmetic: they silenced a real mismatch.\n *\n * The two arms are the two REST versions, which genuinely differ:\n * - **v2** splits the response into `result` / `result_error` / `result_time` /\n *   `result_total` / `result_next` — {@link BatchResponseData}.\n * - **v3** puts the per-command results directly in `result`, with no\n *   per-command error or time split.\n *\n * Each version's strategy narrows the union with a plain `as`, which is a\n * narrowing the runtime really does make (the transport knows its own version)\n * rather than an unchecked reinterpretation.\n *\n * The union carries no discriminant, so nothing in the type system enforces\n * that a v2 strategy only ever sees a v2 response — that coupling is held by\n * `HttpV2`/`HttpV3` each constructing their own `InteractionBatch`. Do not read\n * `getData()!.result` generically outside the paired processing strategy: there\n * is no tag to branch on, and picking the wrong arm compiles.\n */\nexport type BatchResponsePayload<T = unknown>\n  = BatchResponseData<T>\n    | T[]\n    | Record<string | number, T>\n\nexport type InteractionBatchOptions = Required<Omit<ICallBatchOptions, 'isHaltOnError' | 'isObjectMode'>> & {\n  /**\n   * @memo this regeneration is `isHaltOnError` and it is currently `!isHaltOnError`\n   */\n  parallelDefaultValue: boolean\n  restrictionManager: RestrictionManager\n  processingStrategy?: IProcessingStrategy\n}\n\nexport type ResponseHelper = {\n  requestId: string\n  status: number\n  time: PayloadTime\n  restrictionManager: RestrictionManager\n}\n\n/**\n * Working with batch requests\n */\nexport abstract class AbstractInteractionBatch {\n  protected parallelDefaultValue: boolean\n  protected requestId: string\n  protected restrictionManager: RestrictionManager\n  // @memo this regeneration -> isObjectMode\n  protected processingStrategy?: IProcessingStrategy\n\n  protected _commands: BatchCommandV3[] = []\n\n  constructor(options: InteractionBatchOptions) {\n    this.parallelDefaultValue = options.parallelDefaultValue\n    this.requestId = options.requestId\n    this.restrictionManager = options.restrictionManager\n    this.processingStrategy = options.processingStrategy\n  }\n\n  // region Setter Strategy ////\n  public setProcessingStrategy(processingStrategy: IProcessingStrategy) {\n    this.processingStrategy = processingStrategy\n  }\n  // endregion ////\n\n  // region Getter ////\n  get size(): number {\n    return this._commands.length\n  }\n\n  get maxSize(): number {\n    return 0\n  }\n  // endregion ////\n\n  // region Request ////\n  public addCommands(\n    calls: BatchCommandsArrayUniversal | BatchCommandsObjectUniversal | BatchNamedCommandsUniversal\n  ): void {\n    if (!this.processingStrategy) {\n      throw new SdkError({\n        code: 'JSSDK_INTERACTION_BATCH_EMPTY_PROCESSING_STRATEGY',\n        description: 'ProcessingStrategy not set',\n        status: 500\n      })\n    }\n\n    this._commands = this.processingStrategy.prepareCommands(calls, {\n      parallelDefaultValue: this.parallelDefaultValue\n    })\n  }\n\n  public getCommandsForCall(): unknown {\n    if (!this.processingStrategy) {\n      throw new SdkError({\n        code: 'JSSDK_INTERACTION_BATCH_EMPTY_PROCESSING_STRATEGY',\n        description: 'ProcessingStrategy not set',\n        status: 500\n      })\n    }\n\n    return this.processingStrategy.buildCommands(this._commands)\n  }\n  // endregion ////\n\n  // region Response ////\n  public abstract prepareResponse<T>(response: AjaxResult<BatchResponsePayload<T>>): Promise<Result<ICallBatchResult<T>>>\n  // endregion ////\n}\n"],"names":[],"mappings":";;;;;;;;;;;;AA0EO,MAAe,wBAAA,CAAyB;AAAA,EA1E/C;AA0E+C,IAAA,MAAA,CAAA,IAAA,EAAA,0BAAA,CAAA;AAAA;AAAA,EACnC,oBAAA;AAAA,EACA,SAAA;AAAA,EACA,kBAAA;AAAA;AAAA,EAEA,kBAAA;AAAA,EAEA,YAA8B,EAAC;AAAA,EAEzC,YAAY,OAAA,EAAkC;AAC5C,IAAA,IAAA,CAAK,uBAAuB,OAAA,CAAQ,oBAAA;AACpC,IAAA,IAAA,CAAK,YAAY,OAAA,CAAQ,SAAA;AACzB,IAAA,IAAA,CAAK,qBAAqB,OAAA,CAAQ,kBAAA;AAClC,IAAA,IAAA,CAAK,qBAAqB,OAAA,CAAQ,kBAAA;AAAA,EACpC;AAAA;AAAA,EAGO,sBAAsB,kBAAA,EAAyC;AACpE,IAAA,IAAA,CAAK,kBAAA,GAAqB,kBAAA;AAAA,EAC5B;AAAA;AAAA;AAAA,EAIA,IAAI,IAAA,GAAe;AACjB,IAAA,OAAO,KAAK,SAAA,CAAU,MAAA;AAAA,EACxB;AAAA,EAEA,IAAI,OAAA,GAAkB;AACpB,IAAA,OAAO,CAAA;AAAA,EACT;AAAA;AAAA;AAAA,EAIO,YACL,KAAA,EACM;AACN,IAAA,IAAI,CAAC,KAAK,kBAAA,EAAoB;AAC5B,MAAA,MAAM,IAAI,QAAA,CAAS;AAAA,QACjB,IAAA,EAAM,mDAAA;AAAA,QACN,WAAA,EAAa,4BAAA;AAAA,QACb,MA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