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

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

  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 LastPacks', async () => {

    const wrapper = mount(LastPacks, {
      props: {
        serviceOfPacks,
      },
      global: {
        mocks: {
          $store: store
        },
        data() {
          const paramsFields = {
            dateFormat: 'DD.MM.YYYY HH:mm',
            dtStart: serviceOfPacks.getDefaultDates(this.dateFormat).dtStart,
            dtFinish: serviceOfPacks.getDefaultDates(this.dateFormat).dtFinish,
            imei: ''
          }
          return {
            config: tableConfig,
            paramsFields,
            pageState: "await", // await | loading | isLoaded | error
            errorImei: null,
          }
        },
      }
    })

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

  test('Get last packs in LastPacks component', async () => {

    const wrapper = mount(LastPacks, {
      shallow: true,
      props: {
        serviceOfPacks,
      },
      global: {
        mocks: {
          $store: store
        },
        data() {
          const paramsFields = {
            dateFormat: 'DD.MM.YYYY HH:mm',
            dtStart: serviceOfPacks.getDefaultDates(this.dateFormat).dtStart,
            dtFinish: serviceOfPacks.getDefaultDates(this.dateFormat).dtFinish,
            imei: ''
          }
          return {
            config: tableConfig,
            paramsFields,
            pageState: "await", // await | loading | isLoaded | error
            errorImei: null,
          }
        },
      }
    })

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

    await wrapper.vm.getPacks()

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

    expect(lastPacks).toEqual(resPacks)

  })

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

    const wrapper = mount(LastPacks, {
      shallow: true,
      props: {
        serviceOfPacks,
      },
      global: {
        mocks: {
          $store: store
        },
        data() {
          const paramsFields = {
            dateFormat: 'DD.MM.YYYY HH:mm',
            dtStart: serviceOfPacks.getDefaultDates(this.dateFormat).dtStart,
            dtFinish: serviceOfPacks.getDefaultDates(this.dateFormat).dtFinish,
            imei: ''
          }
          return {
            config: tableConfig,
            paramsFields,
            pageState: "await", // await | loading | isLoaded | error
            errorImei: null,
          }
        },
      }
    })

    await wrapper.vm.editFields({key: 'imei', value: '868136035512212'})
    await wrapper.vm.editFields({key: 'dtStart', value: '2024-03-05'})
    await wrapper.vm.editFields({key: 'dtFinish', value: '2024-04-05'})

    expect(wrapper.vm.paramsFields).toEqual({dateFormat: 'DD.MM.YYYY HH:mm', imei: '868136035512212', dtStart: '2024-03-05', dtFinish: '2024-04-05'})

  })

})


