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


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

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

	//---------------------------------------------------------------------------------------
	// The Barcode Module is an optional PDFNet add-on that can be used to extract
	// various types of barcodes from PDF documents.
	//
	// The Apryse SDK Barcode Module can be downloaded from https://dev.apryse.com/
	//---------------------------------------------------------------------------------------
	exports.runBarcodeTest = () => {
		const main = async () => {

			PDFNet.addResourceSearchPath('../Lib/');

			if (!(await PDFNet.BarcodeModule.isModuleAvailable())) {
				console.log('\nUnable to run BarcodeTest: Apryse SDK Barcode Module not available.');
				console.log('---------------------------------------------------------------');
				console.log('The Barcode Module is an optional add-on, available for download');
				console.log('at https://dev.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.\n');

				return;
			}

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

			//--------------------------------------------------------------------------------
			// Example 1) Detect and extract all barcodes from a PDF document into a JSON file
			try {

				console.log('Example 1: extracting barcodes from barcodes.pdf to barcodes.json');

				// A) Open the .pdf document
				const doc = await PDFNet.PDFDoc.createFromFilePath(input_path + 'barcodes.pdf');

				// B) Detect PDF barcodes with the default options
				await PDFNet.BarcodeModule.extractBarcodes(doc, output_path + 'barcodes.json');

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

			//--------------------------------------------------------------------------------
			// Example 2) Limit barcode extraction to a range of pages, and retrieve the JSON into a
			// local string variable, which is then written to a file in a separate function call
			try {

				console.log('Example 2: extracting barcodes from pages 1-2 to barcodes_from_pages_1-2.json');

				// A) Open the .pdf document
				const doc = await PDFNet.PDFDoc.createFromFilePath(input_path + 'barcodes.pdf');

				// B) Detect PDF barcodes with custom options
				const options = new PDFNet.BarcodeModule.BarcodeOptions();

				// Convert only the first two pages
				options.setPages('1-2');

				const json = await PDFNet.BarcodeModule.extractBarcodesAsString(doc, options);

				// C) Save JSON to file
				fs.writeFileSync(output_path + 'barcodes_from_pages_1-2.json', json);

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

			//--------------------------------------------------------------------------------
			// Example 3) Narrow down barcode types and allow the detection of both horizontal
			// and vertical barcodes
			try {

				console.log('Example 3: extracting basic horizontal and vertical barcodes');

				// A) Open the .pdf document
				const doc = await PDFNet.PDFDoc.createFromFilePath(input_path + 'barcodes.pdf');

				// B) Detect only basic 1D barcodes, both horizontal and vertical
				const options = new PDFNet.BarcodeModule.BarcodeOptions();

				// Limit extraction to basic 1D barcode types, such as EAN 13, EAN 8, UPCA, UPCE,
				// Code 3 of 9, Code 128, Code 2 of 5, Code 93, Code 11 and GS1 Databar.
				options.setBarcodeSearchTypes(PDFNet.BarcodeModule.BarcodeOptions.BarcodeTypeGroup.e_linear);

				// Search for barcodes oriented horizontally and vertically
				options.setBarcodeOrientations(
					PDFNet.BarcodeModule.BarcodeOptions.BarcodeOrientation.e_horizontal |
					PDFNet.BarcodeModule.BarcodeOptions.BarcodeOrientation.e_vertical);

				await PDFNet.BarcodeModule.extractBarcodes(doc, output_path + 'barcodes_1D.json', options);

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

			//////////////////////////////////////////////////////////////////////////

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