/**
 * Here is default redirect to `origin` pathname on hard page reload.
 * <p>Updated: now, the current parallel route will write to specific (`customization_source`) search parameter of the origin route.
 * To correct handling it, we need to put them to the client component for manage routing via App Router.
 * You can see the flow of this process by opening returned component.</p>
 */
import type { PageProps, ParallelModalSearchParams } from "@/core/types/common";
import { permanentRedirect } from "next/navigation";
import CustomizationRedirect from "@/app/(visualization)/project/customization/_components/CustomizationRedirect";

type Params = { catchAll: string[] };

export default function Default(props: PageProps<Params, ParallelModalSearchParams<Record<string, string>>>): JSX.Element | never {
  if (props.searchParams.modal) {
    const sourceSearchParams = new URLSearchParams(props.searchParams as Record<string, string>);
    sourceSearchParams.delete("perspective");
    const source = decodeURIComponent(props.searchParams.modal) + "?" + sourceSearchParams.toString();

    const redirectSearchParams = new URLSearchParams({ customization_source: source });
    const redirectRoute = decodeURIComponent(props.searchParams.origin) + "?" + redirectSearchParams.toString();

    return <CustomizationRedirect source={redirectRoute} />;
  }

  return permanentRedirect(decodeURIComponent(props.searchParams.origin));
}