"use client";

import { useEffect } from "react";

/**
 * MSW 初始化组件
 *
 * NEXT_PUBLIC_API_MOCKING=enabled 时启动 MSW worker
 * 上游就绪后设为 disabled，使用真实 API
 */
export function MswInitializer({ onReady }: { onReady: () => void }) {
  useEffect(() => {
    if (process.env.NEXT_PUBLIC_API_MOCKING === "enabled") {
      import("@/mocks/browser")
        .then(({ worker }) => worker.start({ onUnhandledRequest: "bypass" }))
        .then(onReady)
        .catch(() => onReady());
    } else {
      onReady();
    }
  }, [onReady]);

  return null;
}
