'use client';

import { useCustomerMutation } from '@/hooks/api/customers';
import { initData, useSignal } from '@telegram-apps/sdk-react';
import { useEffect, useState } from 'react';

export function UpdateProfile() {
  const initDataUser = useSignal(initData.user);
  const { mutate: updateProfile } = useCustomerMutation();
  const [hasUpdated, setHasUpdated] = useState(false);

  useEffect(() => {
    if (!hasUpdated) {
      updateProfile({
        data: {
          active: true,
          photoUrl: initDataUser?.photoUrl || undefined,
        },
      });
      setHasUpdated(true);
    }
  }, [hasUpdated, initDataUser?.photoUrl, updateProfile]);

  return null;
}
