import {expect, describe, test, beforeEach, vi} from 'vitest'
import {mount} from '@vue/test-utils'
import ServiceOfPacks from "@services/ServiceOfPacks.js"
import {ApiOfPacks, freePacksParams, resFreePacks} from "@mocks/packs.js"
import FreePacks from "@organisms/FreePacks/FreePacks.vue"
import tableConfig from '@organisms/FreePacks/helpers/tableConfig'
import { createStore } from "vuex"
import { store as packs } from '@/store/modules/packs'

describe('test FreePacks', () => {

  const apiOfPacks = new ApiOfPacks()
  const store = createStore({
    modules: {
      packs
    }
  })

  beforeEach(() => {
    store.dispatch('packs/resetStore')
  })

  vi.mock('tabulator-tables', () => {
    const mockTabulator = vi.fn().mockImplementation(() => ({
      // Mock implementation details
    }))
    return { TabulatorFull: mockTabulator } // Adjust based on what you're trying to mock
  })

  const serviceOfPacks = new ServiceOfPacks(apiOfPacks, store)

  test('exist of FreePacks', async () => {

    const wrapper = mount(FreePacks, {
      global: {
        mocks: {
          $store: store
        },
      }
    })

    expect(wrapper.exists()).toBe(true)
  })

  test('Edit fields in FreePacks component', async () => {

    const wrapper = mount(FreePacks, {
      shallow: true,
      global: {
        mocks: {
          $store: store
        },
        props: {
          serviceOfPacks,
        },
        data() {
          const paramsFields = {
            dateFormat: "DD.MM.YYYY",
            dtStart: '',
            dtFinish: new Date(),
            imei: '',
            packNumber: '',
            sortBy: 'pack_dt',
            countEntries: '10',
            sortByOptions: ['pack_dt', 'dt'],
            countEntriesOptions: ['10', '500', '2000', '10000', '50000', '100000']
          }
          return {
            config: tableConfig,
            paramsFields,
            pageState: "await", // await | loading | isLoaded | error
            errorImei: null,
            errorPackNumber: null,
          }
        },
      }
    })

    await serviceOfPacks.setPackFields(wrapper.vm.paramsFields, {key: 'imei', value: '868136035512212'})
    await serviceOfPacks.setPackFields(wrapper.vm.paramsFields, {key: 'dtStart', value: '2024-03-05'})
    await serviceOfPacks.setPackFields(wrapper.vm.paramsFields, {key: 'dtFinish', value: '2024-04-05'})
    await serviceOfPacks.setPackFields(wrapper.vm.paramsFields, {key: 'packNumber', value: '333444555'})
    await serviceOfPacks.setPackFields(wrapper.vm.paramsFields, {key: 'sortBy', value: 'dt'})
    await serviceOfPacks.setPackFields(wrapper.vm.paramsFields, {key: 'countEntries', value: '500'})

    expect(wrapper.vm.paramsFields).toEqual({
      dateFormat: "DD.MM.YYYY",
      imei: '868136035512212', 
      dtStart: '2024-03-05', 
      dtFinish: '2024-04-05', 
      packNumber: '333444555', 
      sortBy: 'dt', 
      countEntries: '500', 
      sortByOptions: ['pack_dt', 'dt'],
      countEntriesOptions: ['10', '500', '2000', '10000', '50000', '100000']
    })
  })

  test('Get free packs in FreePacks component', async () => {

    const wrapper = mount(FreePacks, {
      shallow: true,
      global: {
        mocks: {
          $store: store
        },
        props: {
          serviceOfPacks,
        },
        data() {
          const paramsFields = {
            dtStart: '',
            dtFinish: '',
            imei: ''
          }
          return {
            config: tableConfig,
            paramsFields,
            pageState: "await", // await | loading | isLoaded | error
            errorImei: null,
          }
        },
      }
    })

    await wrapper.setData({paramsFields: {imei: '868136035512212'}})

    await serviceOfPacks.fetchAndSetFreePacks(freePacksParams)

    const freePacks = store.getters['packs/freePacks']

    expect(freePacks).toEqual(resFreePacks)

  })

})


