import { useParams } from "@/lib/router";
import { useCompany } from "../context/CompanyContext";
import { ConvertPanel, FORMAT_GROUPS } from "../components/ConvertPanel";

// All valid formats for case-insensitive validation
const ALL_FORMATS = Object.values(FORMAT_GROUPS).flat();

function normalizeFormatParam(param: string | undefined): string | undefined {
  if (!param) return undefined;
  const lower = param.toLowerCase();
  return ALL_FORMATS.includes(lower) ? lower : undefined;
}

export function ConvertPage() {
  const { sourceFormat, targetFormat } = useParams<{
    sourceFormat?: string;
    targetFormat?: string;
  }>();

  const { selectedCompanyId } = useCompany();
  const companyId = selectedCompanyId ?? "";

  // Normalize to lowercase; silently ignore invalid format strings
  const initialSourceFormat = normalizeFormatParam(sourceFormat);
  const initialTargetFormat = normalizeFormatParam(targetFormat);

  return (
    <div className="flex flex-col gap-6 p-6">
      <h1 className="text-xl font-semibold">Convert File</h1>
      <ConvertPanel
        companyId={companyId}
        initialSourceFormat={initialSourceFormat}
        initialTargetFormat={initialTargetFormat}
      />
    </div>
  );
}
