// src/QmsDashboardView.tsx
import React, { useEffect } from 'react';
import { requireNativeComponent, NativeEventEmitter, NativeModules, ViewStyle } from 'react-native';

type Props = {
  style?: ViewStyle;
  ClientID?: string;
  ClientCode?: string;
  user_token?: string;

  // New callback for dashboard close
  onClose?: (event: { status: string }) => void;
};

const NativeQmsDashboardView = requireNativeComponent<Props>('QmsDashboardView');

export const QmsDashboardView: React.FC<Props> = ({ onClose, ...props }) => {
  useEffect(() => {
    let subscription: any;

    if (onClose) {
      const emitter = new NativeEventEmitter(NativeModules.QmsDashboardEmitter);
      subscription = emitter.addListener('onClose', onClose);
    }

    return () => {
      subscription?.remove();
    };
  }, [onClose]);

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