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

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

export const canAcceptSuggestion = (annotation: AnnotationWrapper, isApprover: boolean, canProcessManualRedaction: boolean) => {
    const can = isApprover && (annotation.isSuggestion || annotation.isDeclinedSuggestion);
    return annotation.isSuggestionAdd || annotation.isSuggestionRemoveDictionary ? can && canProcessManualRedaction : can;
};

export const canRejectSuggestion = (annotation: AnnotationWrapper, isApprover: boolean, canProcessManualRedaction: boolean) => {
    const can = isApprover && annotation.isSuggestion;
    return annotation.isSuggestionAdd || annotation.isSuggestionRemoveDictionary ? can && canProcessManualRedaction : can;
};

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

export const canForceRedaction = (annotation: AnnotationWrapper, canAddOrRequestRedaction: boolean) =>
    canAddOrRequestRedaction && annotation.isSkipped && !annotation.isFalsePositive && !annotation.pending;

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

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

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

export const canRemoveOrSuggestToRemoveFromDictionary = (annotation: AnnotationWrapper) =>
    annotation.isModifyDictionary &&
    (annotation.isRedacted || annotation.isSkipped || annotation.isHint) &&
    !annotation.pending &&
    !annotation.hasBeenResized;

export const canRemoveOrSuggestToRemoveRedaction = (annotations: AnnotationWrapper[], permissions: AnnotationPermissions) =>
    annotations.length === 1 &&
    (permissions.canRemoveOrSuggestToRemoveOnlyHere ||
        permissions.canRemoveOrSuggestToRemoveFromDictionary ||
        permissions.canMarkAsFalsePositive);

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

export const canRecategorizeImage = (annotation: AnnotationWrapper) =>
    ((annotation.isImage && !annotation.isSuggestion) || annotation.isSuggestionRecategorizeImage) && !annotation.pending;

export const canResizeAnnotation = (annotation: AnnotationWrapper, canAddOrRequestRedaction: boolean) =>
    canAddOrRequestRedaction &&
    (((annotation.isRedacted || annotation.isImage) && !annotation.isSuggestion) ||
        annotation.isSuggestionResize ||
        annotation.isRecommendation) &&
    !annotation.pending;
