import type { PConnProps } from '@pega/react-sdk-components/lib/types/PConnProps';

interface ReferenceProps extends PConnProps {
  // If any, enter additional props that only exist on this component
  visibility?: boolean;
  context?: string;
  readOnly?: boolean;
  displayMode?: string;
}

export default function Reference(props: ReferenceProps) {
  const { visibility = true, context = '', getPConnect, readOnly = false, displayMode = '' } = props;

  const pConnect = getPConnect();
  const referenceConfig = { ...pConnect.getComponentConfig() };

  delete referenceConfig?.name;
  delete referenceConfig?.type;
  delete referenceConfig?.visibility;

  const viewMetadata: any = pConnect.getReferencedView();

  if (!viewMetadata) {
    // console.log("View not found ", pConnect.getComponentConfig());
    return null;
  }

  const viewObject = {
    ...viewMetadata,
    config: {
      ...viewMetadata.config,
      ...referenceConfig
    }
  };

  // @ts-expect-error - Argument of type 'null' is not assignable to parameter of type 'string'.
  const viewComponent: any = pConnect.createComponent(viewObject, null, null, {
    pageReference: context && context.startsWith('@CLASS') ? '' : context
  });

  if (referenceConfig.inheritedProps && referenceConfig.inheritedProps.length > 0) {
    const inheritedProps = pConnect.getInheritedProps();
    referenceConfig.inheritedProps = Object.keys(inheritedProps).map(prop => ({ prop, value: inheritedProps[prop] }));
  }

  viewComponent.props.getPConnect().setInheritedConfig({
    ...referenceConfig,
    readOnly,
    displayMode
  });

  if (visibility !== false) {
    return <>{viewComponent}</>;
  }
  return null;
}
