import { useSession } from "next-auth/react";
import { useGetCompanyByIdQuery } from "@/redux/api/companyApi";
import { getDefaultUomDisplayLabel, DEFAULT_UOM_VALUE } from "@/utils/defaultUom";

/**
 * Returns the company's default unit of measurement (UoM).
 * Defaults to Sq. Yard when not set.
 */
export function useDefaultUom(): {
  defaultUomValue: string;
  defaultUomLabel: string;
} {
  const { data: session } = useSession();
  const companyId = session?.companyId;
  const { data: companyData } = useGetCompanyByIdQuery(companyId as string, {
    skip: !companyId,
  });

  const raw = (companyData?.data as { defaultUOM?: string } | undefined)?.defaultUOM;
  const defaultUomValue = raw && raw.trim() ? raw : DEFAULT_UOM_VALUE;
  const defaultUomLabel = getDefaultUomDisplayLabel(defaultUomValue);

  return { defaultUomValue, defaultUomLabel };
}
