import { useI18n } from "vue-i18n";

export const useStatus = () => {
  const unknown = "_base_info.unknown";
  const { t } = useI18n();
  const commonStatusKeyMap: Record<number, string> = {
    0: "_base_info.status.enable",
    1: "_base_info.status.disable",
  };

  const getCommonStatusLabel = (code: number | null): string => {
    if (code === null || code === undefined) return "";
    const key = commonStatusKeyMap[code];
    return key ? t(key) : t(unknown);
  };

  const productionPlanStatusKeyMap: Record<number, string> = {
    0: "_base_info.production.production_plan.no_complete",
    1: "_base_info.production.production_plan.completed",
    2: "_base_info.production.production_plan.approving",
    3: "_base_info.production.production_plan.reviewed",
  };

  const getProductionPlanStatusLabel = (code: number | null): string => {
    if (code === null || code === undefined) return "";
    const key = productionPlanStatusKeyMap[code];
    return key ? t(key) : t(unknown);
  };

  const formStatusKeyMap: Record<number, string> = {
    0: "_base_info.form_status.no_approve",
    1: "_base_info.form_status.approve",
    2: "_base_info.form_status.reject",
    3: "_base_info.form_status.no_complete",
    4: "_base_info.form_status.complete",
    5: "_base_info.form_status.receipting",
    6: "_base_info.form_status.imported",
    7: "_base_info.form_status.approving",
    8: "_base_info.form_status.returned",
  };

  const finishedProductReceiptStatusKeyMap: Record<number, string> = {
    1: "_base_info.finished_product_receipt_status.pending_outstock",
    3: "_base_info.finished_product_receipt_status.outstocking",
    4: "_base_info.finished_product_receipt_status.completed",
  };

  const getFinishedProductReceiptStatusLabel = (code: number | null): string => {
    if (code === null || code === undefined) return "";
    const key = finishedProductReceiptStatusKeyMap[code];
    return key ? t(key) : t(unknown);
  };

  const activationStatusKeyMap: Record<boolean, string> = {
    true: "_base_info.activation_status.activated",
    false: "_base_info.activation_status.not_activated",
  };

  const getActivationStatusLabel = (status: boolean | null): string => {
    if (status === null || status === undefined) return "";
    const key = activationStatusKeyMap[status];
    return key ? t(key) : t(unknown);
  };

  const getActivationStatusTxtLabel = (status: string | null): string => {
    if (status === null || status === undefined) return "";
    if (status === "已激活" || status === "Activated") return t("_base_info.activation_status.activated");
    if (status === "未激活" || status === "Not Activated") return t("_base_info.activation_status.not_activated");
    return status;
  };

  const getFormStatusLabel = (code: number | null): string => {
    if (code === null || code === undefined) return "";
    const key = formStatusKeyMap[code];
    return key ? t(key) : t(unknown);
  };

  const purchasePlanItemStatusKeyMap: Record<number, string> = {
    0: "_base_info.purchase.purchase_plan_item.no_start",
    1: "_base_info.purchase.purchase_plan_item.ordered",
    2: "_base_info.purchase.purchase_plan_item.completed",
  };

  const getPurchasePlanItemStatusLabel = (code: number | null): string => {
    if (code === null || code === undefined) return t("_base_info.purchase.purchase_plan_item.no_start");
    const key = purchasePlanItemStatusKeyMap[code];
    return key ? t(key) : t(unknown);
  };

  const purchasePlanStatusKeyMap: Record<number, string> = {
    0: "_base_info.purchase.purchase_plan.pending",
    1: "_base_info.purchase.purchase_plan.in_progress",
    2: "_base_info.purchase.purchase_plan.completed",
  };

  const getPurchasePlanStatusLabel = (code: number | null): string => {
    if (code === null || code === undefined) return "";
    const key = purchasePlanStatusKeyMap[code];
    return key ? t(key) : t(unknown);
  };

  return {
    commonStatusKeyMap,
    getCommonStatusLabel,
    productionPlanStatusKeyMap,
    getProductionPlanStatusLabel,
    formStatusKeyMap,
    getFormStatusLabel,
    finishedProductReceiptStatusKeyMap,
    getFinishedProductReceiptStatusLabel,
    activationStatusKeyMap,
    getActivationStatusLabel,
    getActivationStatusTxtLabel,
    purchasePlanItemStatusKeyMap,
    getPurchasePlanItemStatusLabel,
    purchasePlanStatusKeyMap,
    getPurchasePlanStatusLabel,
  };
};
