'use client';
import * as Common from './Common';
import { FormContext, FormContextProvider } from './context/form-context';
import * as Documents from './Documents';
import { Header } from './Header';
import { Overlay } from './Overlay';
import type { FormComponentProps } from './types';
import { createUrl } from '@/utils/url';
import { Background, Divider } from '@repo/ui';
import type { FC } from 'react';
import { useContext } from 'react';

function Content(props: FormComponentProps) {
  const { title } = props;
  const { pageUrlParams } = useContext(FormContext);

  return (
    <Background className="relative flex w-full flex-col gap-2 p-5">
      <Overlay />
      <Header title={title} link={'/ius' + createUrl({ ...pageUrlParams, route: '/conditions' })} />
      <Common.Elements {...props} />
      <Common.Buttons />
      <Divider />
      <Documents.Files {...props} />
      <Documents.Buttons />
      <Divider />
    </Background>
  );
}

function withContext<T extends FormComponentProps>(Component: FC<T>) {
  return (props: T) => {
    const { pageUrlParams } = props;

    return (
      <FormContextProvider pageUrlParams={pageUrlParams}>
        <Component {...props} />
      </FormContextProvider>
    );
  };
}

export const Form = withContext(Content);
