{"version":3,"file":"interface-strategy.mjs","sources":["../../../../../../src/core/interaction/batch/processing/interface-strategy.ts"],"sourcesContent":["import type { BatchCommandV3, ICallBatchResult } from '../../../../types/http'\nimport type { BatchPayload } from '../../../../types/payloads'\nimport type { AjaxResult } from '../../../http/ajax-result'\nimport type { RestrictionManager } from '../../../http/limiters/manager'\nimport { Result } from '../../../result'\nimport { AjaxError } from '../../../http/ajax-error'\n\nexport type GetCommandsOptions = {\n  // @memo this regeneration `isHaltOnError` -> !isHaltOnError\n  parallelDefaultValue: boolean\n}\n\nexport type ResponseHelper<T> = {\n  requestId: string\n  parallelDefaultValue: boolean\n  response: AjaxResult<BatchPayload<T>>\n  // status: number\n  // data: BatchPayloadResult<T>\n  // time: PayloadTime\n  restrictionManager: RestrictionManager\n}\n\nexport type ResultItems<T> = Map<string | number, AjaxResult<T>>\n\nexport interface IProcessingStrategy {\n  prepareCommands(calls: unknown, options: GetCommandsOptions): BatchCommandV3[]\n  buildCommands(commands: BatchCommandV3[]): unknown\n  prepareItems<T>(commands: BatchCommandV3[], responseHelper: ResponseHelper<T>): Promise<ResultItems<T>>\n  handleResults<T>(commands: BatchCommandV3[], results: ResultItems<T>, responseHelper: ResponseHelper<T>): Promise<Result<ICallBatchResult<T>>>\n}\n\nexport abstract class AbstractProcessing implements IProcessingStrategy {\n  public abstract prepareCommands(calls: unknown, options: GetCommandsOptions): BatchCommandV3[]\n  public abstract buildCommands(commands: BatchCommandV3[]): unknown\n\n  // region prepareItems ////\n  /**\n   * Template method. The soft-error envelope guard lives here ONCE so v2 and v3\n   * can't drift on it (#228 — hoisted from the duplicated #145 fix).\n   *\n   * When the batch CALL itself soft-errors (a top-level code in the restriction\n   * manager's `exceptionCodeForSoft` set, surfaced as a soft `Result` instead of\n   * a throw), the envelope carries `{ error }` and no `result`, so\n   * `response.getData()` is `undefined`. Skip per-row parsing — it would\n   * dereference `getData()!.result` — and let {@link handleResults} surface the\n   * top-level errors. The version-specific success path is {@link _prepareItemsSuccess}.\n   */\n  public async prepareItems<T>(\n    commands: BatchCommandV3[],\n    responseHelper: ResponseHelper<T>\n  ): Promise<ResultItems<T>> {\n    const results: ResultItems<T> = new Map()\n\n    if (!responseHelper.response.isSuccess) {\n      return results\n    }\n\n    return this._prepareItemsSuccess<T>(commands, responseHelper, results)\n  }\n\n  /** Per-version success path: parse the response payload into `results` (the empty accumulator created by {@link prepareItems}). */\n  protected abstract _prepareItemsSuccess<T>(\n    commands: BatchCommandV3[],\n    responseHelper: ResponseHelper<T>,\n    results: ResultItems<T>\n  ): Promise<ResultItems<T>>\n\n  protected abstract _processResponseItem<T>(\n    command: BatchCommandV3,\n    index: string | number,\n    responseHelper: ResponseHelper<T>,\n    results: Map<string | number, AjaxResult<T>>\n  ): Promise<void>\n  // endregion ////\n\n  // region handleResults ////\n  /**\n   * Template method. Same single soft-error guard as {@link prepareItems} (#228):\n   * there is no per-row data and `getData()` is `undefined`, so surface the\n   * envelope's top-level errors and return an empty data map instead of\n   * dereferencing `getData()!.time`. The version-specific success path is\n   * {@link _handleResultsSuccess}.\n   */\n  public async handleResults<T>(\n    commands: BatchCommandV3[],\n    results: ResultItems<T>,\n    responseHelper: ResponseHelper<T>\n  ): Promise<Result<ICallBatchResult<T>>> {\n    const result = new Result<ICallBatchResult<T>>()\n\n    if (!responseHelper.response.isSuccess) {\n      for (const [index, error] of responseHelper.response.errors) {\n        result.addError(error, index)\n      }\n      result.setData({\n        result: new Map() as ResultItems<T>,\n        time: undefined\n      })\n      return result\n    }\n\n    return this._handleResultsSuccess<T>(commands, results, responseHelper, result)\n  }\n\n  /**\n   * Per-version success path: fold per-command `results` into `result` — the\n   * empty accumulator created by {@link handleResults}. Mutate and RETURN that\n   * same `result`; do not construct a new one (the base relies on the instance\n   * it passed in).\n   */\n  protected abstract _handleResultsSuccess<T>(\n    commands: BatchCommandV3[],\n    results: ResultItems<T>,\n    responseHelper: ResponseHelper<T>,\n    result: Result<ICallBatchResult<T>>\n  ): Promise<Result<ICallBatchResult<T>>>\n\n  protected abstract _processResponseError<T>(result: Result<ICallBatchResult<T>>, ajaxError: AjaxError, index: string): void\n  // endregion ////\n\n  // region Tools ////\n  protected _getBatchResultByIndex<T>(\n    data: T[] | Record<string | number, T> | undefined,\n    index: string | number\n  ): T | undefined {\n    if (!data) return undefined\n\n    if (Array.isArray(data)) {\n      return data[index as number]\n    }\n\n    return (data as Record<string | number, T>)[index]\n  }\n\n  protected _createErrorFromAjaxResult(ajaxResult: AjaxResult): AjaxError {\n    if (ajaxResult.hasError('base-error')) {\n      return ajaxResult.errors.get('base-error') as AjaxError\n    }\n\n    return new AjaxError({\n      code: 'JSSDK_BATCH_SUB_ERROR',\n      description: ajaxResult.getErrorMessages().join('; '),\n      status: ajaxResult.getStatus(),\n      requestInfo: { ...ajaxResult.getQuery() },\n      originalError: ajaxResult.getErrors().next().value\n    })\n  }\n  // endregion ////\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;AA+BO,MAAe,kBAAA,CAAkD;AAAA,EA/BxE;AA+BwE,IAAA,MAAA,CAAA,IAAA,EAAA,oBAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBtE,MAAa,YAAA,CACX,QAAA,EACA,cAAA,EACyB;AACzB,IAAA,MAAM,OAAA,uBAA8B,GAAA,EAAI;AAExC,IAAA,IAAI,CAAC,cAAA,CAAe,QAAA,CAAS,SAAA,EAAW;AACtC,MAAA,OAAO,OAAA;AAAA,IACT;AAEA,IAAA,OAAO,IAAA,CAAK,oBAAA,CAAwB,QAAA,EAAU,cAAA,EAAgB,OAAO,CAAA;AAAA,EACvE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAyBA,MAAa,aAAA,CACX,QAAA,EACA,OAAA,EACA,cAAA,EACsC;AACtC,IAAA,MAAM,MAAA,GAAS,IAAI,MAAA,EAA4B;AAE/C,IAAA,IAAI,CAAC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