import { parseIrr, rialToToman, tomanToRial, multiplyIrr, formatIrr, formatIrrToToman } from './money';

describe('money utils', () => {
  describe('parseIrr', () => {
    it('parses digit strings to bigint', () => {
      expect(parseIrr('23300000')).toBe(BigInt(23300000));
    });

    it('stays integer-safe beyond Number.MAX_SAFE_INTEGER', () => {
      const huge = '9007199254740993'; // MAX_SAFE_INTEGER + 2
      expect(parseIrr(huge)).toBe(BigInt('9007199254740993'));
    });

    it('accepts integer numbers and bigints', () => {
      expect(parseIrr(1500)).toBe(BigInt(1500));
      expect(parseIrr(BigInt(1500))).toBe(BigInt(1500));
    });

    it('rejects non-integer input', () => {
      expect(() => parseIrr('12.5')).toThrow();
      expect(() => parseIrr('abc')).toThrow();
      expect(() => parseIrr(12.5)).toThrow();
    });
  });

  describe('rialToToman', () => {
    it('divides Rials by 10', () => {
      expect(rialToToman('23300000')).toBe(BigInt(2330000));
    });
  });

  describe('tomanToRial', () => {
    it('multiplies Toman by 10 into an IRR digit-string', () => {
      expect(tomanToRial('280000')).toBe('2800000');
      expect(tomanToRial(280000)).toBe('2800000');
    });

    it('stays integer-safe for large Toman amounts', () => {
      expect(tomanToRial('900719925474099')).toBe('9007199254740990');
    });

    it('rejects non-integer input', () => {
      expect(() => tomanToRial('12.5')).toThrow();
    });
  });

  describe('multiplyIrr', () => {
    it('multiplies an IRR amount by an integer count', () => {
      // 280,000 Toman/hr = 2,800,000 IRR × 6 hours = 16,800,000 IRR.
      expect(multiplyIrr('2800000', 6)).toBe('16800000');
    });

    it('rejects a negative or non-integer count', () => {
      expect(() => multiplyIrr('2800000', -1)).toThrow();
      expect(() => multiplyIrr('2800000', 1.5)).toThrow();
    });
  });

  describe('formatting', () => {
    it('groups Rials in en locale', () => {
      expect(formatIrr('23300000', 'en')).toBe('23,300,000');
    });

    it('converts and groups Toman in en locale', () => {
      expect(formatIrrToToman('23300000', 'en')).toBe('2,330,000');
    });

    it('renders Persian digits in fa locale', () => {
      // fa-IR uses Persian digits and its own grouping separator.
      const out = formatIrrToToman('23300000', 'fa');
      expect(out).not.toMatch(/[0-9]/); // no ASCII digits
      expect(out).toContain('۲');
    });
  });
});
