/**
 * Copyright 2024 Phenix Real Time Solutions, Inc. Confidential and Proprietary. All Rights Reserved.
 */
import {JSX} from 'react';
import {useSelector} from 'react-redux';

import {AppStore} from 'store';
import {DataRowType} from 'components/table';
import {SingleStreamIndicator, OfflineIndicator, MultiStreamIndicator, LoadingIndicator} from './indicators';

interface IPublishingStateIndicator {
  row: DataRowType;
  publishingStateKey: string;
  idKey: string;
}

const PublishingStateIndicator = ({
  row,
  publishingStateKey,
  idKey
}: IPublishingStateIndicator): JSX.Element => {
  const id = row[idKey];
  const publishingState = useSelector((state: AppStore) => publishingStateKey && state[publishingStateKey as keyof AppStore]?.publishingState);
  const rowPublishingState = publishingState.find((record: Record<string, any>) => record[idKey] === id);

  if (!rowPublishingState) {
    return <LoadingIndicator />;
  }

  if (!rowPublishingState.isOnline) {
    return <OfflineIndicator />;
  }

  if (rowPublishingState.multipleStreams) {
    return <MultiStreamIndicator />;
  }

  return <SingleStreamIndicator />;
};

export default PublishingStateIndicator;