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

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

const axios = {
  ...mockAxiosInstance,
  create: jest.fn(() => ({ ...mockAxiosInstance })),
  isAxiosError: jest.fn((error: any) => error?.isAxiosError === true),
  isCancel: jest.fn(() => false),
  all: jest.fn((promises: Promise<any>[]) => Promise.all(promises)),
  spread: jest.fn((callback: Function) => (arr: any[]) => callback(...arr)),
  toFormData: jest.fn(),
  formToJSON: jest.fn(),
  CancelToken: {
    source: jest.fn(() => ({
      token: {},
      cancel: jest.fn(),
    })),
  },
  Axios: jest.fn(),
  AxiosError: jest.fn(),
  Cancel: jest.fn(),
  CanceledError: jest.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';
