import { describe, it, expect } from 'vitest';
import {
	buildMexTariffDigitsFromCatalogRow,
	formatMexTariffDigitsForDisplay,
	normalizeMexTariffDigitsStored,
	splitMexTariffDigitsForSitar
} from './mexican-tariff-fraction';

describe('mexican-tariff-fraction', () => {
	describe('buildMexTariffDigitsFromCatalogRow', () => {
		it('concatena fracción punteada y NICO', () => {
			expect(
				buildMexTariffDigitsFromCatalogRow({
					fraction: '0101.21.01',
					nico: '01',
					code: '01012101'
				})
			).toBe('0101210101');
		});
		it('usa code si fraction vacía', () => {
			expect(
				buildMexTariffDigitsFromCatalogRow({
					fraction: '',
					nico: '02',
					code: '84713001'
				})
			).toBe('8471300102');
		});
		it('NICO vacío devuelve 00', () => {
			expect(
				buildMexTariffDigitsFromCatalogRow({
					fraction: '0101.21.01',
					nico: null,
					code: '01012101'
				})
			).toBe('0101210100');
		});
	});

	describe('splitMexTariffDigitsForSitar', () => {
		it('separa 10 dígitos', () => {
			expect(splitMexTariffDigitsForSitar('0101210101')).toEqual({
				fraccion8: '01012101',
				nico2: '01'
			});
		});
	});

	describe('normalizeMexTariffDigitsStored', () => {
		it('8 dígitos añade NICO 00', () => {
			expect(normalizeMexTariffDigitsStored('01012101')).toBe('0101210100');
		});
		it('respeta 10 dígitos', () => {
			expect(normalizeMexTariffDigitsStored('0101210103')).toBe('0101210103');
		});
	});

	describe('formatMexTariffDigitsForDisplay', () => {
		it('formatea 8 y 10 dígitos', () => {
			expect(formatMexTariffDigitsForDisplay('01012101')).toBe('0101.21.01');
			expect(formatMexTariffDigitsForDisplay('0101210101')).toBe('0101.21.01.01');
		});
		it('respeta valor con puntos', () => {
			expect(formatMexTariffDigitsForDisplay('0101.21.01')).toBe('0101.21.01');
		});
	});
});
