//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {
	'use strict';

	exports.runPDFUATest = () => {

		//---------------------------------------------------------------------------------------
		// The following sample illustrates how to make sure a file meets the PDF/UA standard, using the PDFUAConformance class object.
		// Note: this feature is currently experimental and subject to change
		//
		// DataExtractionModule is required (Mac users can use StructuredOutputModule instead)
		// https://docs.apryse.com/documentation/core/info/modules/#data-extraction-module
		// https://docs.apryse.com/documentation/core/info/modules/#structured-output-module (Mac)
		//---------------------------------------------------------------------------------------
		const main = async () => {

			// Relative path to the folder containing test files.
			const input_path = '../TestFiles/';
			const output_path = '../TestFiles/Output/';

			// DataExtraction library location, replace if desired, should point to a folder that includes the contents of <DataExtractionModuleRoot>/Lib.
			// If using default, unzip the DataExtraction zip to the parent folder of Samples, and merge with existing "Lib" folder
			const extraction_module_path = '../Lib/';

			const input_file1 = input_path + 'autotag_input.pdf';
			const input_file2 = input_path + 'table.pdf';
			const output_file1 = output_path + 'autotag_pdfua.pdf';
			const output_file2 = output_path + 'table_pdfua_linearized.pdf';

			try {
				//-----------------------------------------------------------
				// Example: PDF/UA Conversion
				//-----------------------------------------------------------
				console.log('AutoConverting...');

				await PDFNet.addResourceSearchPath(extraction_module_path);
				if (!await PDFNet.DataExtractionModule.isModuleAvailable(PDFNet.DataExtractionModule.DataExtractionEngine.e_DocStructure)) {
					console.log('');
					console.log('Unable to run PDFUATest: Apryse SDK Data Extraction module not available.');
					console.log('---------------------------------------------------------------');
					console.log('The Data Extraction module is an optional add-on, available for download');
					console.log('at https://apryse.com/. If you have already downloaded this');
					console.log('module, ensure that the SDK is able to find the required files');
					console.log('using the PDFNet.addResourceSearchPath() function.');
					console.log('');
					return;
				}

				const pdf_ua = await PDFNet.PDFUAConformance.create();

				console.log('Simple Conversion...');
				{
					// Perform conversion using default options
					await pdf_ua.autoConvert(input_file1, output_file1);
				}

				console.log('Converting With Options...');
				{
					const pdf_ua_opts = await PDFNet.PDFUAConformance.createPDFUAOptions();
					pdf_ua_opts.setSaveLinearized(true); // Linearize when saving output
					// Note: if file is password protected, you can use pdf_ua_opts.setPassword()

					// Perform conversion using the options we specify
					await pdf_ua.autoConvert(input_file2, output_file2, pdf_ua_opts);
				}

			} catch (err) {
				console.log(err);
			}

			console.log('PDFUAConformance test completed.');
		}

		PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) {
			console.log('Error: ' + JSON.stringify(error));
		}).then(function () { return PDFNet.shutdown(); });
	};
	exports.runPDFUATest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=PDFUATest.js