{"version":3,"sources":["../../src/core/document.ts"],"sourcesContent":["import IOHeader from \"./header\";\r\nimport IOSection from \"./section\";\r\nimport IOSectionCollection from \"./section-collection\";\r\n\r\n/**\r\n * IODocument represents a complete Internet Object document.\r\n *\r\n * A document consists of:\r\n * - A header containing schema definitions, variables, and metadata\r\n * - Zero or more data sections, each optionally named and schema-bound\r\n * - Accumulated parsing and validation errors\r\n *\r\n * Features:\r\n * - Aggregates errors from all sections for single-pass diagnostics\r\n * - JSON serialization with optional header output\r\n * - Schema-aware section management\r\n *\r\n * @example\r\n * ```typescript\r\n * const doc = parse(`\r\n *   ~ $person: {name: string, age: int}\r\n *   ---\r\n *   ~ Alice, 30\r\n *   ~ Bob, 25\r\n * `);\r\n *\r\n * console.log(doc.toJSON());\r\n * // [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }]\r\n *\r\n * if (doc.errors.length > 0) {\r\n *   console.error('Parsing errors:', doc.errors);\r\n * }\r\n * ```\r\n */\r\nclass IODocument {\r\n  private _header: IOHeader;\r\n  private _sections: IOSectionCollection | null;\r\n  private _ownErrors: Error[] = []; // Accumulated errors during parsing\r\n\r\n  constructor(header: IOHeader, sections: IOSectionCollection | null, errors: Error[] = []) {\r\n    this._header = header;\r\n    this._sections = sections;\r\n    this._ownErrors = errors;\r\n  }\r\n\r\n  public get header(): IOHeader {\r\n    return this._header;\r\n  }\r\n\r\n  public get sections(): IOSectionCollection | null {\r\n    return this._sections;\r\n  }\r\n\r\n  /**\r\n   * Returns all errors accumulated during parsing and validation.\r\n   * This enables IDEs and tools to show all diagnostics in one pass.\r\n   *\r\n   * @returns A defensive copy of the errors array to prevent external mutation\r\n   */\r\n  public get errors(): Error[] {\r\n    const aggregatedErrors = [...this._ownErrors];\r\n    if (this._sections) {\r\n      for (const section of this._sections) {\r\n        aggregatedErrors.push(...section.errors);\r\n      }\r\n    }\r\n    return aggregatedErrors;\r\n  }\r\n\r\n  /**\r\n   * Returns all errors accumulated during parsing and validation.\r\n   * This enables IDEs and tools to show all diagnostics in one pass.\r\n   *\r\n   * @returns A defensive copy of the errors array to prevent external mutation\r\n   */\r\n  public getErrors(): ReadonlyArray<Error> {\r\n    return this.errors;\r\n  }\r\n\r\n  /**\r\n   * Adds validation errors to the document.\r\n   * This method is package-private and should only be called by the parser.\r\n   *\r\n   * @internal\r\n   * @param errors - Array of validation errors to append\r\n   */\r\n  public addErrors(errors: Error[]): void {\r\n    if (errors.length > 0) {\r\n      this._ownErrors.push(...errors);\r\n    }\r\n  }\r\n\r\n  /**\r\n   * Return a clean object for nodejs console logging.\r\n   */\r\n  [Symbol.for('nodejs.util.inspect.custom')]() {\r\n    return {\r\n      header: this._header,\r\n      sections: this._sections,\r\n      ...(this._ownErrors.length > 0 ? { errors: this._ownErrors } : {})\r\n    }\r\n  }\r\n\r\n  /**\r\n   * Converts the data sections into a JavaScript object.\r\n   * @param options Optional configuration for object conversion\r\n   * @param options.skipErrors If true, excludes error objects from collections (default: false)\r\n   */\r\n  public toObject(options?: { skipErrors?: boolean }): any {\r\n    const sectionsLen = this._sections?.length || 0;\r\n    let data: any = null;\r\n\r\n    if (sectionsLen === 1) {\r\n      const section = this._sections?.get(0) as IOSection;\r\n      data = section.toObject(options);\r\n    } else if (sectionsLen > 1) {\r\n      data = {};\r\n      for (let i = 0; i < sectionsLen; i++) {\r\n        const section = this._sections?.get(i) as IOSection;\r\n        data[section.name as string] = section.toObject(options);\r\n      }\r\n    }\r\n\r\n    // Only return header+data if header has non-empty definitions\r\n    const headerObject = this.header.toObject();\r\n    if (headerObject && Object.keys(headerObject).length > 0) {\r\n      return {\r\n        header: headerObject,\r\n        data,\r\n      };\r\n    }\r\n\r\n    return data;\r\n  }\r\n\r\n  /**\r\n   * Alias for toObject() method for JSON compatibility\r\n   * @param options Optional configuration for JSON conversion\r\n   */\r\n  public toJSON(options?: { skipErrors?: boolean }): any {\r\n    return this.toObject(options);\r\n  }\r\n}\r\n\r\nexport default IODocument;\r\n"],"mappings":";;;;;;;;;;;;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