import * as interfaces from './interfaces.js';
import { XInvoice } from './classes.xinvoice.js';

// Import format-specific encoder/decoder classes
import { FacturXEncoder } from './formats/facturx.encoder.js';
import { FacturXDecoder } from './formats/facturx.decoder.js';
import { XInvoiceEncoder } from './formats/xinvoice.encoder.js';
import { XInvoiceDecoder } from './formats/xinvoice.decoder.js';
import { DecoderFactory } from './formats/decoder.factory.js';
import { BaseDecoder } from './formats/base.decoder.js';

// Import validator classes
import { ValidatorFactory } from './formats/validator.factory.js';
import { BaseValidator } from './formats/base.validator.js';
import { FacturXValidator } from './formats/facturx.validator.js';
import { UBLValidator } from './formats/ubl.validator.js';

// Export specific interfaces for easier use
export type {
  IXInvoice,
  IParty,
  IAddress,
  IContact,
  IInvoiceItem,
  ValidationError,
  ValidationResult,
  ValidationLevel,
  InvoiceFormat,
  XInvoiceOptions,
  IValidator
} from './interfaces.js';

// Export interfaces (legacy support)
export { interfaces };

// Export main class
export { XInvoice };

// Export format classes
export { 
  // Base classes
  BaseDecoder,
  DecoderFactory,
  
  // Format-specific encoders
  FacturXEncoder,
  XInvoiceEncoder,
  
  // Format-specific decoders
  FacturXDecoder, 
  XInvoiceDecoder
};

// Export validator classes
export const Validators = {
  ValidatorFactory,
  BaseValidator,
  FacturXValidator,
  UBLValidator
};

// For backward compatibility
export { FacturXEncoder as ZugferdXmlEncoder };
export { FacturXDecoder as ZUGFeRDXmlDecoder };

/**
 * Validates an XML string against the appropriate format rules
 * @param xml XML content to validate
 * @param level Validation level (syntax, semantic, business)
 * @returns ValidationResult with the result of validation
 */
export function validateXml(
  xml: string, 
  level: interfaces.ValidationLevel = interfaces.ValidationLevel.SYNTAX
): interfaces.ValidationResult {
  try {
    const validator = ValidatorFactory.createValidator(xml);
    return validator.validate(level);
  } catch (error) {
    return {
      valid: false,
      errors: [{
        code: 'VAL-ERROR',
        message: `Validation error: ${error.message}`
      }],
      level
    };
  }
}

/**
 * Creates a new XInvoice instance
 * @returns A new XInvoice instance
 */
export function createXInvoice(): XInvoice {
  return new XInvoice();
}