/* eslint-disable sonarjs/function-return-type */
'use client';

import { useBackButton, useClientOnce, useDidMount, useViewport } from '@/hooks/telegram';
import { init } from '@/utils/telegram/init';
import { type PropsWithChildren } from 'react';

export function TelegramProvider(props: Readonly<PropsWithChildren>) {
  // Unfortunately, Telegram Mini Apps does not allow us to use all features of
  // the Server Side Rendering. That's why we are showing loader on the server
  // side.
  const didMount = useDidMount();

  if (!didMount) return null;

  return <RootInner {...props} />;
}

function RootInner({ children }: PropsWithChildren) {
  const isDevelopment = process.env.NODE_ENV === 'development';

  const debug = isDevelopment;

  // Initialize the library.
  useClientOnce(() => {
    init(debug);
  });

  useViewport();
  useBackButton();

  return children;
}
