import { describe, test, it, expect } from 'vitest'
import {createI18n, VueI18n} from 'vue-i18n'
import I18nUtils from "~/services/utils/i18nUtils";
import {EnumChoices} from "~/types/interfaces";

describe('translateEnum', () => {
    let i18nUtils: I18nUtils

    beforeEach(() => {
        // @ts-ignore
        const i18n = vi.fn() as VueI18n;
        i18nUtils = new I18nUtils(i18n)
        i18n.t = vi.fn((msg: string) => msg.replace('This is the letter', 'C\'est la lettre'))
    })

    test('with simple enum', () => {
        const input: EnumChoices = [
            { value: 'Alpha', label: 'This is the letter A' },
            { value: 'Beta', label: 'This is the letter B' },
            { value: 'Epsilon', label: 'This is the letter E' },
        ]

        const expected: EnumChoices = [
            { value: 'Alpha', label: 'C\'est la lettre A' },
            { value: 'Beta', label: 'C\'est la lettre B' },
            { value: 'Epsilon', label: 'C\'est la lettre E' },
        ]

        expect(i18nUtils.translateEnum(input)).toEqual(expected)
    })

    test('with simple enum and sorting enabled', () => {
        const input: EnumChoices = [
            { value: 'Epsilon', label: 'This is the letter E' },
            { value: 'Alpha', label: 'This is the letter A' },
            { value: 'Beta', label: 'This is the letter B' },
        ]

        const expected: EnumChoices = [
            { value: 'Alpha', label: 'C\'est la lettre A' },
            { value: 'Beta', label: 'C\'est la lettre B' },
            { value: 'Epsilon', label: 'C\'est la lettre E' },
        ]

        expect(i18nUtils.translateEnum(input, true)).toEqual(expected)
    })
})

describe('formatPhoneNumber', () => {
    let i18nUtils: I18nUtils

    beforeEach(() => {
        // @ts-ignore
        const i18n = vi.fn() as VueI18n;
        i18nUtils = new I18nUtils(i18n)
    })
    test('with valid international phone number', () => {
        expect(i18nUtils.formatPhoneNumber('+33611223344')).toEqual('06 11 22 33 44')
    })
    test('with valid non-formatted phone number', () => {
        expect(i18nUtils.formatPhoneNumber('0611223344', 'FR')).toEqual('06 11 22 33 44')
    })
    test('with empty string', () => {
        expect(() => i18nUtils.formatPhoneNumber('', 'FR')).toThrowError('NOT_A_NUMBER')
    })
    test('with invalid string', () => {
        expect(() => i18nUtils.formatPhoneNumber('abcd', 'FR')).toThrowError('NOT_A_NUMBER')
    })
})
