46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import { useBranchingQuiz } from "./FlowControlLogic/useBranchingQuiz";
|
|
import { useLinearQuiz } from "./FlowControlLogic/useLinearQuiz";
|
|
import { useAIQuiz } from "./FlowControlLogic/useAIQuiz";
|
|
import { Status } from "@/model/settingsData";
|
|
|
|
interface StatusData {
|
|
status: Status;
|
|
haveRoot: string | null;
|
|
}
|
|
|
|
// выбор способа управления в зависимости от статуса
|
|
let cachedManager: () => ReturnType<typeof useLinearQuiz>;
|
|
export let statusOfQuiz: "line" | "branch" | "ai";
|
|
|
|
function analyicStatus({ status, haveRoot }: StatusData) {
|
|
if (status === "ai") statusOfQuiz = "ai";
|
|
if (status === "start") {
|
|
// Если есть ветвление, то settings.cfg.haveRoot будет заполнен
|
|
if (haveRoot) statusOfQuiz = "branch";
|
|
else statusOfQuiz = "line";
|
|
} else throw new Error("quiz is inactive");
|
|
}
|
|
|
|
export const initDataManager = (data: StatusData) => {
|
|
analyicStatus(data);
|
|
switch (statusOfQuiz) {
|
|
case "line":
|
|
cachedManager = useLinearQuiz;
|
|
break;
|
|
case "branch":
|
|
cachedManager = useBranchingQuiz;
|
|
break;
|
|
case "ai":
|
|
cachedManager = useAIQuiz;
|
|
break;
|
|
}
|
|
};
|
|
|
|
// Главный хук (интерфейс для потребителей)
|
|
export const useQuestionFlowControl = () => {
|
|
if (!cachedManager) {
|
|
throw new Error("DataManager not initialized! Call initDataManager() first.");
|
|
}
|
|
return cachedManager();
|
|
};
|