import { HeapIgnore } from '@contentsquare/react-native-autocapture';
import React from 'react';
import Logger from '../../core/logging/logger';
import CSQMethodFilter from '../../csq/CSQMethodFilter';
import { CSQMaskProps } from '../../types/types';
import { getHeapIgnoreProps } from '../../utils/utils';
import CSQMaskedView from './CSQMaskedView';
import CSQUnmaskedView from './CSQUnmaskedView';

export const CSQMask: React.FC<CSQMaskProps> = props => {
  React.useEffect(() => {
    CSQMethodFilter.warnIfDisabled('CSQ', 'CSQMask');
  }, []);

  React.useEffect(() => {
    if (
      props.ignoreTextOnly &&
      (props.allowInnerHierarchy !== undefined ||
        props.allowInteraction !== undefined ||
        props.allowProps !== undefined ||
        props.allowText !== undefined ||
        props.allowAccessibilityLabel !== undefined)
    ) {
      Logger.error(
        'CSQMask: When ignoreTextOnly is true, allow* props (allowInnerHierarchy, allowProps, allowText, allowInteraction, allowAccessibilityLabel) are ignored and should not be set.'
      );
    }
  }, [
    props.ignoreTextOnly,
    props.allowInnerHierarchy,
    props.allowProps,
    props.allowText,
    props.allowInteraction,
    props.allowAccessibilityLabel,
  ]);

  const { isSessionReplayMasked = true, children, ...viewProps } = props;
  const heapIgnoreProps = getHeapIgnoreProps(props);
  const CSQComponent = isSessionReplayMasked ? CSQMaskedView : CSQUnmaskedView;

  return (
    <CSQComponent {...viewProps}>
      <HeapIgnore {...heapIgnoreProps}>{children}</HeapIgnore>
    </CSQComponent>
  );
};
