import { splitFinalPrice, distributeSellingComponents } from './markup-tax.helper';

describe('markup-tax.helper', () => {
  describe('splitFinalPrice', () => {
    it('back-solves markup and 5% tax on markup from the final price (BK-437675)', () => {
      const r = splitFinalPrice(11607, 10954.19, 5);
      expect(r.markup).toBe(621.72);
      expect(r.markupTax).toBe(31.09);
      expect(+(10954.19 + r.markup + r.markupTax).toFixed(2)).toBe(11607);
      expect(r.preTax).toBe(11575.91);
    });

    it('charges no tax when there is no slab', () => {
      expect(splitFinalPrice(11576, 10954.19, 0)).toEqual({
        markup: 621.81,
        markupTax: 0,
        preTax: 11576,
      });
    });

    it('charges no tax on a discount (final below cost)', () => {
      const r = splitFinalPrice(900, 1000, 5);
      expect(r.markup).toBe(-100);
      expect(r.markupTax).toBe(0);
    });
  });

  describe('distributeSellingComponents', () => {
    const costs = { hotel: 6000, transport: 2070, activity: 2884.19, special: 0 };

    it('spreads overall markup pro-rata and sums exactly to preTax', () => {
      const out = distributeSellingComponents(costs, { pricing_strategy: 'overall' }, 11575.91);
      expect(out.special).toBe(0);
      expect(+(out.hotel + out.transport + out.activity).toFixed(2)).toBe(11575.91);
      expect(out.hotel).toBeGreaterThan(6000);
    });

    it('weights per-component markups by each component', () => {
      const out = distributeSellingComponents(
        { hotel: 100, transport: 100, activity: 0, special: 0 },
        { pricing_strategy: 'per_component', markup_type: 'amount', hotel_markup: 100 },
        300,
      );
      expect(out).toEqual({ hotel: 200, transport: 100, activity: 0, special: 0 });
    });

    it('returns zeros when there is no cost', () => {
      expect(
        distributeSellingComponents({ hotel: 0, transport: 0, activity: 0, special: 0 }, {}, 0),
      ).toEqual({ hotel: 0, transport: 0, activity: 0, special: 0 });
    });
  });
});
