"use client";

import { useCallback, useEffect } from "react";
import { useTranslation } from "react-i18next";

import { getAdminPlayTypes } from "@/api/admin-config";
import {
  formatAdminPlayCodeLabel,
  getAdminPlayTypesLoadPromise,
  resolveAdminPlayTypeDisplayName,
} from "@/lib/admin-play-types";

export function useAdminPlayTypeCatalog(): void {
  useEffect(() => {
    void getAdminPlayTypesLoadPromise(getAdminPlayTypes);
  }, []);
}

/** 返回按当前界面语言解析的玩法标签（显示名 + 编码） */
export function useAdminPlayCodeLabel(): (playCode: string | null | undefined) => string {
  const { i18n } = useTranslation();
  useAdminPlayTypeCatalog();

  return useCallback(
    (playCode: string | null | undefined) => formatAdminPlayCodeLabel(playCode, i18n.language),
    [i18n.language],
  );
}

/** 仅显示名，不含编码（用于下拉选项等） */
export function useAdminPlayCodeDisplayName(): (playCode: string | null | undefined) => string {
  const { i18n } = useTranslation();
  useAdminPlayTypeCatalog();

  return useCallback(
    (playCode: string | null | undefined) =>
      resolveAdminPlayTypeDisplayName(playCode, i18n.language),
    [i18n.language],
  );
}
