import type { ChildApi } from '@teres/iframe-bridge';
import logger from '@/utils/logger';

let childPromise: Promise<ChildApi> | null = null;

export function setChildPromise(promise: Promise<ChildApi>) {
  childPromise = promise;
}

export function clearChildPromise() {
  childPromise = null;
}

export async function changeChildLanguage(lng: string) {
  if (!childPromise) return;
  try {
    const child = await childPromise;
    // @ts-ignore
    await child?.changeLanguage?.(lng);
  } catch (err) {
    logger.warn('changeChildLanguage failed', err);
  }
}