import { AnnotationPermissions } from '@models/file/annotation.permissions';
import { AnnotationWrapper } from '@models/file/annotation.wrapper';
import { Dictionary, LogEntryEngines, SuperTypes } from '@red/domain';

export const canUndo = (annotation: AnnotationWrapper, isApprover: boolean) => !isApprover && annotation.pending;

export const canForceHint = (annotation: AnnotationWrapper, canAddRedaction: boolean) =>
    canAddRedaction && annotation.isIgnoredHint && !annotation.pending;

export const canForceRedaction = (annotation: AnnotationWrapper, canAddRedaction: boolean) =>
    canAddRedaction &&
    (annotation.isSkipped || (annotation.IMAGE_HINT && annotation.superType === SuperTypes.Hint)) &&
    !annotation.isFalsePositive &&
    !annotation.pending;

export const canAcceptRecommendation = (annotation: AnnotationWrapper) => annotation.isRecommendation && !annotation.pending;

export const canMarkAsFalsePositive = (annotation: AnnotationWrapper, annotationEntity: Dictionary) =>
    annotation.canBeMarkedAsFalsePositive && !annotation.hasBeenResizedLocally && annotationEntity?.hasDictionary;

export const canRemoveOnlyHere = (annotation: AnnotationWrapper, canAddRedaction: boolean, autoAnalysisDisabled: boolean) =>
    canAddRedaction &&
    (autoAnalysisDisabled || !annotation.pending) &&
    (annotation.isRedacted || (annotation.isHint && !annotation.isImage));

export const canRemoveFromDictionary = (annotation: AnnotationWrapper, autoAnalysisDisabled: boolean) =>
    annotation.isModifyDictionary &&
    (annotation.isRedacted || annotation.isSkipped || annotation.isHint || (annotation.isIgnoredHint && !annotation.isRuleBased)) &&
    (autoAnalysisDisabled || !annotation.pending) &&
    annotation.isDictBased;

export const canRemoveRedaction = (annotation: AnnotationWrapper, permissions: AnnotationPermissions) =>
    (!annotation.isIgnoredHint || !annotation.isRuleBased) &&
    (permissions.canRemoveOnlyHere || permissions.canRemoveFromDictionary || permissions.canMarkAsFalsePositive);

export const canChangeLegalBasis = (annotation: AnnotationWrapper, canAddRedaction: boolean, autoAnalysisDisabled: boolean) =>
    canAddRedaction && annotation.isRedacted && (autoAnalysisDisabled || !annotation.pending);

export const canRecategorizeAnnotation = (annotation: AnnotationWrapper, canRecategorize: boolean, autoAnalysisDisabled: boolean) =>
    canRecategorize &&
    (annotation.isImage || (annotation.isHint && !annotation.isRuleBased)) &&
    (autoAnalysisDisabled || !annotation.pending);

export const canResizeAnnotation = (
    annotation: AnnotationWrapper,
    canAddRedaction: boolean,
    autoAnalysisDisabled: boolean,
    hasDictionary = false,
) =>
    (canAddRedaction &&
        !annotation.isSkipped &&
        !annotation.isIgnoredHint &&
        (autoAnalysisDisabled || !annotation.pending) &&
        (annotation.isRedacted ||
            annotation.isImage ||
            (annotation.isHint && !annotation.isRuleBased) ||
            (!annotation.isHint && hasDictionary && annotation.isRuleBased))) ||
    annotation.isRecommendation;

export const canResizeInDictionary = (annotation: AnnotationWrapper, permissions: AnnotationPermissions) =>
    permissions.canResizeAnnotation &&
    annotation.isModifyDictionary &&
    !annotation.hasBeenResizedLocally &&
    !(annotation.hasBeenForcedHint || annotation.hasBeenForcedRedaction);

export const canEditAnnotation = (annotation: AnnotationWrapper) => (annotation.isRedacted || annotation.isSkipped) && !annotation.isImage;

export const canEditHint = (annotation: AnnotationWrapper) =>
    ((annotation.isHint && !annotation.isRuleBased) || annotation.isIgnoredHint) && !annotation.isImage;

export const canEditImage = (annotation: AnnotationWrapper) => annotation.isImage;
