{"version":3,"sources":["../../../src/schema/processing/collection-processor.ts"],"sourcesContent":["import Collection from '../../core/collection';\r\nimport Definitions from '../../core/definitions';\r\nimport InternetObject from '../../core/internet-object';\r\nimport CollectionNode from '../../parser/nodes/collections';\r\nimport ObjectNode from '../../parser/nodes/objects';\r\nimport TokenNode from '../../parser/nodes/tokens';\r\nimport ErrorNode from '../../parser/nodes/error';\r\nimport processObject from '../object-processor';\r\nimport Schema from '../schema';\r\nimport { SchemaResolver } from '../utils/schema-resolver';\r\nimport { ProcessingContext } from './processing-context';\r\n\r\nexport default function processCollection(\r\n  data: CollectionNode,\r\n  schema: Schema | TokenNode,\r\n  defs?: Definitions,\r\n  errorCollector?: Error[]\r\n): Collection<any> {\r\n  // Pre-resolve schema once for better performance\r\n  const resolvedSchema = SchemaResolver.resolve(schema, defs);\r\n\r\n  // Pre-allocate collection with known size\r\n  const collection = new Collection<InternetObject>();\r\n  const length = data.children.length;\r\n\r\n  // Process items; include ErrorNode so UI can surface error info objects\r\n  for (let i = 0; i < length; i++) {\r\n    const item = data.children[i];\r\n\r\n    // If parsing produced an ErrorNode, preserve it in the collection\r\n    // so that downstream consumers (toJSON/UI) can render error info.\r\n    // NOTE: Parser errors are already in document._errors, so we don't add them to errorCollector.\r\n    if (item instanceof ErrorNode) {\r\n      // Push ErrorNode directly; IOCollection.toJSON handles toValue()\r\n      // which serializes error details with positions.\r\n      // Also annotate the underlying error with collectionIndex for consistency\r\n      try {\r\n        (item as any).error.collectionIndex = i;\r\n      } catch {}\r\n      collection.push(item as unknown as any);\r\n    } else {\r\n      // Create a ProcessingContext for this object to collect all its errors\r\n      const ctx = new ProcessingContext();\r\n\r\n      try {\r\n        // Pass context - processObject will add ALL validation errors to it\r\n        const result = processObject(item as ObjectNode, resolvedSchema, defs, i, ctx);\r\n\r\n        // If there were validation errors, create an ErrorNode but still have the result\r\n        if (ctx.hasErrors()) {\r\n          const errors = ctx.getErrors();\r\n          // Attach collectionIndex to each error\r\n          for (const error of errors) {\r\n            (error as any).collectionIndex = i;\r\n            // Add to document-level error collector if provided\r\n            if (errorCollector) {\r\n              errorCollector.push(error);\r\n            }\r\n            // Add to collection's own errors\r\n            collection.errors.push(error);\r\n          }\r\n          // Create error node with the first error for display\r\n          const errorNode = new ErrorNode(\r\n            errors[0],\r\n            (item as ObjectNode).getStartPos(),\r\n            (item as ObjectNode).getEndPos()\r\n          );\r\n          collection.push(errorNode as unknown as any);\r\n        } else {\r\n          // No errors - push the result directly\r\n          collection.push(result);\r\n        }\r\n      } catch (error) {\r\n        // Syntax errors and other critical errors are still thrown\r\n        if (error instanceof Error) {\r\n          // Attach boundary context for downstream serializers/UI\r\n          (error as any).collectionIndex = i;\r\n          const errorNode = new ErrorNode(\r\n            error,\r\n            (item as ObjectNode).getStartPos(),\r\n            (item as ObjectNode).getEndPos()\r\n          );\r\n          // Add error to error collector if provided\r\n          if (errorCollector) {\r\n            errorCollector.push(error);\r\n          }\r\n          // Add to collection's own errors\r\n          collection.errors.push(error);\r\n          collection.push(errorNode as unknown as any);\r\n        } else {\r\n          // Re-throw non-Error exceptions\r\n          throw error;\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  return collection;\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAuB;AAMvB,mBAAsB;AACtB,8BAA0B;AAE1B,6BAA+B;AAC/B,gCAAkC;AAEnB,SAAR,kBACL,MACA,QACA,MACA,gBACiB;AAEjB,QAAM,iBAAiB,sCAAe,QAAQ,QAAQ,IAAI;AAG1D,QAAM,aAAa,IAAI,kBAAAA,QAA2B;AAClD,QAAM,SAAS,KAAK,SAAS;AAG7B,WAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,UAAM,OAAO,KAAK,SAAS,CAAC;AAK5B,QAAI,gBAAgB,aAAAC,SAAW;AAI7B,UAAI;AACF,QAAC,KAAa,MAAM,kBAAkB;AAAA,MACxC,QAAQ;AA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