import {expect, describe, test, vi} from 'vitest'
import {mount} from '@vue/test-utils'
import LastMachinesByPackNum from '@organisms/LastMachinesByPackNumber/LastMachinesByPackNum.vue'
import tableConfig from '@organisms/LastMachinesByPackNumber/helpers/tableConfig'
import { createStore } from "vuex"
import { store as machines } from '@/store/modules/machines'
import ServiceOfMachines from "@services/ServiceOfMachines"
import {resLastMachines, AdapterOfMachines} from "@mocks/machines.js"

describe('test LastPacksByPackNum component', () => {

  const adapterOfMachines = new AdapterOfMachines()
  const store = createStore({
    modules: {
      machines
    }
  })

  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 serviceOfMachines = new ServiceOfMachines(adapterOfMachines, store)

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

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

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

  test('Get last machines by packs numbers in LastMachinesByPackNum component', async () => {

    const wrapper = mount(LastMachinesByPackNum, {
      shallow: true,
      global: {
        mocks: {
          $store: store
        },
        props: {
          serviceOfMachines,
        },
        data() {
          return {
            packs: '',
            config: tableConfig,
            pageState: "await", // await | loading | isLoaded | error
            errorPacks: null,
          }
        },
      }
    })

    await wrapper.setData({packs: [[7683, 7684, 7685]]})

    const packsFromData = wrapper.vm.packs

    await serviceOfMachines.fetchAndSetLastMachinesByPackNum(packsFromData)

    const lastMachines = store.getters['machines/lastMachinesByPackNumber']

    expect(lastMachines).toEqual(resLastMachines)

  })

  test('Edit field packs in LastMachinesByPackNum component', async () => {

    const wrapper = mount(LastMachinesByPackNum, {
      shallow: true,
      global: {
        mocks: {
          $store: store
        },
        props: {
          serviceOfMachines,
        },
        data() {
          return {
            packs: '',
            config: tableConfig,
            pageState: "await", // await | loading | isLoaded | error
            errorPacks: null,
          }
        },
      }
    })

    await wrapper.vm.editPacks('7681, 7682, 7683')

    const packsFromData = wrapper.vm.packs

    expect(packsFromData).toEqual('7681, 7682, 7683')

  })

})


