{"version":3,"file":"index.cjs","sources":["../src/lib/export-plugin.ts","../src/lib/manifest.ts","../src/lib/index.ts"],"sourcesContent":["import { BasePlugin, createEmitter, Listener, PluginRegistry } from '@embedpdf/core';\nimport { PdfErrorCode, PdfErrorReason, PdfTaskHelper, Task } from '@embedpdf/models';\n\nimport {\n  BufferAndName,\n  ExportCapability,\n  ExportPluginConfig,\n  ExportScope,\n  DownloadRequestEvent,\n} from './types';\n\nexport class ExportPlugin extends BasePlugin<ExportPluginConfig, ExportCapability> {\n  static readonly id = 'export' as const;\n\n  private readonly downloadRequest$ = createEmitter<DownloadRequestEvent>();\n  private readonly config: ExportPluginConfig;\n\n  constructor(id: string, registry: PluginRegistry, config: ExportPluginConfig) {\n    super(id, registry);\n    this.config = config;\n  }\n\n  // ─────────────────────────────────────────────────────────\n  // Capability\n  // ─────────────────────────────────────────────────────────\n\n  protected buildCapability(): ExportCapability {\n    return {\n      // Active document operations\n      saveAsCopy: () => this.saveAsCopy(),\n      download: () => this.download(),\n\n      // Document-scoped operations\n      forDocument: (documentId: string) => this.createExportScope(documentId),\n    };\n  }\n\n  // ─────────────────────────────────────────────────────────\n  // Document Scoping\n  // ─────────────────────────────────────────────────────────\n\n  private createExportScope(documentId: string): ExportScope {\n    return {\n      saveAsCopy: () => this.saveAsCopy(documentId),\n      download: () => this.download(documentId),\n    };\n  }\n\n  // ─────────────────────────────────────────────────────────\n  // Core Operations\n  // ─────────────────────────────────────────────────────────\n\n  private download(documentId?: string): void {\n    const id = documentId ?? this.getActiveDocumentId();\n    this.downloadRequest$.emit({ documentId: id });\n  }\n\n  private saveAsCopy(documentId?: string): Task<ArrayBuffer, PdfErrorReason> {\n    const id = documentId ?? this.getActiveDocumentId();\n    const coreDoc = this.coreState.core.documents[id];\n\n    if (!coreDoc?.document) {\n      return PdfTaskHelper.reject({\n        code: PdfErrorCode.DocNotOpen,\n        message: `Document ${id} not found`,\n      });\n    }\n\n    return this.engine.saveAsCopy(coreDoc.document);\n  }\n\n  public saveAsCopyAndGetBufferAndName(documentId: string): Task<BufferAndName, PdfErrorReason> {\n    const task = new Task<BufferAndName, PdfErrorReason>();\n    const coreDoc = this.coreState.core.documents[documentId];\n\n    if (!coreDoc?.document) {\n      return PdfTaskHelper.reject({\n        code: PdfErrorCode.DocNotOpen,\n        message: `Document ${documentId} not found`,\n      });\n    }\n\n    this.saveAsCopy(documentId).wait(\n      (result) => {\n        task.resolve({\n          buffer: result,\n          name: coreDoc.name ?? this.config.defaultFileName,\n        });\n      },\n      (error) => task.fail(error),\n    );\n\n    return task;\n  }\n\n  // ─────────────────────────────────────────────────────────\n  // Event Listeners\n  // ─────────────────────────────────────────────────────────\n\n  public onRequest(listener: Listener<DownloadRequestEvent>) {\n    return this.downloadRequest$.on(listener);\n  }\n\n  // ─────────────────────────────────────────────────────────\n  // Lifecycle\n  // ─────────────────────────────────────────────────────────\n\n  async initialize(_: ExportPluginConfig): Promise<void> {\n    this.logger.info('ExportPlugin', 'Initialize', 'Export plugin initialized');\n  }\n\n  async destroy(): Promise<void> {\n    this.downloadRequest$.clear();\n    await super.destroy();\n  }\n}\n","import { PluginManifest } from '@embedpdf/core';\nimport { ExportPluginConfig } from './types';\n\nexport const EXPORT_PLUGIN_ID = 'export';\n\nexport const manifest: PluginManifest<ExportPluginConfig> = {\n  id: EXPORT_PLUGIN_ID,\n  name: 'Export Plugin',\n  version: '1.0.0',\n  provides: ['export'],\n  requires: [],\n  optional: [],\n  defaultConfig: {\n    defaultFileName: 'document.pdf',\n  },\n};\n","import { PluginPackage } from '@embedpdf/core';\n\nimport { ExportPlugin } from './export-plugin';\nimport { manifest, EXPORT_PLUGIN_ID } from './manifest';\nimport { ExportPluginConfig } from './types';\n\nexport const ExportPluginPackage: PluginPackage<ExportPlugin, ExportPluginConfig> = {\n  manifest,\n  create: (registry, config) => new ExportPlugin(EXPORT_PLUGIN_ID, registry, config),\n  reducer: () => {},\n  initialState: {},\n};\n\nexport * from './export-plugin';\nexport * from './types';\nexport * from './manifest';\n"],"names":["_ExportPlugin","BasePlugin","constructor","id","registry","config","super","this","downloadRequest$","createEmitter","buildCapability","saveAsCopy","download","forDocument","documentId","createExportScope","getActiveDocumentId","emit","coreDoc","coreState","core","documents","document","engine","PdfTaskHelper","reject","code","PdfErrorCode","DocNotOpen","message","saveAsCopyAndGetBufferAndName","task","Task","wait","result","resolve","buffer","name","defaultFileName","error","fail","onRequest","listener","on","initialize","_","logger","info","destroy","clear","ExportPlugin","EXPORT_PLUGIN_ID","manifest","version","provides","requires","optional","defaultConfig","ExportPluginPackage","create","reducer","initialState"],"mappings":"gJAWaA,EAAN,cAA2BC,EAAAA,WAMhC,WAAAC,CAAYC,EAAYC,EAA0BC,GAChDC,MAAMH,EAAIC,GAJZG,KAAiBC,iBAAmBC,kBAKlCF,KAAKF,OAASA,CAChB,CAMU,eAAAK,GACR,MAAO,CAELC,WAAY,IAAMJ,KAAKI,aACvBC,SAAU,IAAML,KAAKK,WAGrBC,YAAcC,GAAuBP,KAAKQ,kBAAkBD,GAEhE,CAMQ,iBAAAC,CAAkBD,GACxB,MAAO,CACLH,WAAY,IAAMJ,KAAKI,WAAWG,GAClCF,SAAU,IAAML,KAAKK,SAASE,GAElC,CAMQ,QAAAF,CAASE,GACf,MAAMX,EAAKW,GAAcP,KAAKS,sBAC9BT,KAAKC,iBAAiBS,KAAK,CAAEH,WAAYX,GAC3C,CAEQ,UAAAQ,CAAWG,GACjB,MAAMX,EAAKW,GAAcP,KAAKS,sBACxBE,EAAUX,KAAKY,UAAUC,KAAKC,UAAUlB,GAE9C,aAAKe,WAASI,UAOPf,KAAKgB,OAAOZ,WAAWO,EAAQI,UAN7BE,EAAAA,cAAcC,OAAO,CAC1BC,KAAMC,EAAAA,aAAaC,WACnBC,QAAS,YAAY1B,eAK3B,CAEO,6BAAA2B,CAA8BhB,GACnC,MAAMiB,EAAO,IAAIC,OACXd,EAAUX,KAAKY,UAAUC,KAAKC,UAAUP,GAE9C,aAAKI,WAASI,WAOdf,KAAKI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