import axios from "axios";
import { handleMockRequest } from "./handlers";

/**
 * 为 axios 实例注册 Mock 适配器（拦截请求并返回本地数据）
 */
export function setupMock(axiosInstance) {
  axiosInstance.defaults.adapter = (config) => {
    const payload = handleMockRequest(config);
    return Promise.resolve({
      data: payload,
      status: 200,
      statusText: "OK",
      headers: { "content-type": "application/json" },
      config,
      request: {},
    });
  };
}

/** 开发环境：同时 Mock 默认 axios（部分页面直接用 axios.delete 全路径） */
export function setupGlobalMock() {
  setupMock(axios);
}

export function isMockEnabled() {
  return process.env.VUE_APP_USE_MOCK === "true";
}
