/**
 * Manual axios mock for Vitest
 * This avoids ESM transformation issues with the axios package
 */

import { vi } from 'vitest';

const mockAxiosInstance = {
  get: vi.fn(() => Promise.resolve({ data: {} })),
  post: vi.fn(() => Promise.resolve({ data: {} })),
  put: vi.fn(() => Promise.resolve({ data: {} })),
  delete: vi.fn(() => Promise.resolve({ data: {} })),
  patch: vi.fn(() => Promise.resolve({ data: {} })),
  request: vi.fn(() => Promise.resolve({ data: {} })),
  interceptors: {
    request: {
      use: vi.fn(() => 0),
      eject: vi.fn(),
      clear: vi.fn(),
    },
    response: {
      use: vi.fn(() => 0),
      eject: vi.fn(),
      clear: vi.fn(),
    },
  },
  defaults: {
    headers: {
      common: {},
      get: {},
      post: {},
      put: {},
      delete: {},
      patch: {},
    },
    baseURL: '',
    timeout: 0,
    withCredentials: false,
  },
  getUri: vi.fn(),
  head: vi.fn(() => Promise.resolve({ data: {} })),
  options: vi.fn(() => Promise.resolve({ data: {} })),
  postForm: vi.fn(() => Promise.resolve({ data: {} })),
  putForm: vi.fn(() => Promise.resolve({ data: {} })),
  patchForm: vi.fn(() => Promise.resolve({ data: {} })),
};

const axios = {
  ...mockAxiosInstance,
  create: vi.fn(() => ({ ...mockAxiosInstance })),
  isAxiosError: vi.fn((error: any) => error?.isAxiosError === true),
  isCancel: vi.fn(() => false),
  all: vi.fn((promises: Promise<any>[]) => Promise.all(promises)),
  spread: vi.fn((callback: Function) => (arr: any[]) => callback(...arr)),
  toFormData: vi.fn(),
  formToJSON: vi.fn(),
  CancelToken: {
    source: vi.fn(() => ({
      token: {},
      cancel: vi.fn(),
    })),
  },
  Axios: vi.fn(),
  AxiosError: vi.fn(),
  Cancel: vi.fn(),
  CanceledError: vi.fn(),
  VERSION: '1.0.0',
  default: mockAxiosInstance,
};

export default axios;
export const AxiosError = class extends Error {
  isAxiosError = true;
  response?: any;
  request?: any;
  config?: any;
  code?: string;

  constructor(message?: string) {
    super(message);
    this.name = 'AxiosError';
  }
};
export type { AxiosRequestConfig } from 'axios';
