<commit_before>import { Draft } from 'immer';
import { PayloadAction } from '@reduxjs/toolkit';
import { RootState } from './eoStore';
import { eoMessageSelector, eoMessageStateSelector } from './eoSelectors';
import {
    EOState,
    EOMessageParams,
    EOTokenParams,
    EOInitResult,
    EOInitParams,
    EOMessage,
    EODocumentInitializeParams,
    EOLoadEmbeddedResults,
    EOLoadEmbeddedParams,
    EOLoadRemoteParams,
    EOLoadRemoteResults,
    EOMessageReply,
} from './eoType';
import { MessageRemoteImage, MessageState } from '../messages/messagesTypes';
import { getEmbeddedImages, getRemoteImages, updateImages } from '../../helpers/message/messageImages';
import { markEmbeddedImagesAsLoaded } from '../../helpers/message/messageEmbeddeds';
import {
    loadBackgroundImages,
    loadElementOtherThanImages,
    removeProtonPrefix,
} from '../../helpers/message/messageRemotes';
import { initialState } from './eoSlice';

export const getMessageState = (state: Draft<EOState>) => eoMessageStateSelector({ eo: state } as RootState);

export const getEOMessage = (state: Draft<EOState>) => eoMessageSelector({ eo: state } as RootState);

// Get image refs in the state for those in data
const getStateImages = <T extends { image: MessageRemoteImage }>(data: T[], messageState: MessageState) => {
    const remoteImages = getRemoteImages(messageState);

    return data.map(({ image: inputImage, ...rest }) => {
        const image = remoteImages.find((image) => image.id === inputImage.id) as MessageRemoteImage;
        return { image, ...rest };
    });
};

export const reset = (state: Draft<EOState>) => {
    Object.assign(state, initialState);
};

export const initFulfilled = (
    state: Draft<EOState>,
    action: PayloadAction<EOInitResult, string, { arg: EOInitParams }>
) => {
    const { token, decryptedToken, password } = action.payload;

    if (token) {
        state.encryptedToken = token;
    }
    if (decryptedToken) {
        state.decryptedToken = decryptedToken;
    }
    if (password) {
        state.password = password;
    }
    state.isStoreInitialized = true;
};

export const loadEOTokenFulfilled = (
    state: Draft<EOState>,
    action: PayloadAction<string, string, { arg: EOTokenParams }>
) => {
    state.encryptedToken = action.payload;
};

export const loadEOMessageFulfilled = (
    state: Draft<EOState>,
    action: PayloadAction<{ eoMessage: EOMessage; messageState: MessageState }, string, { arg: EOMessageParams }>
) => {
    state.password = action.meta.arg.password;
    state.decryptedToken = action.meta.arg.token;
    state.message = action.payload.eoMessage as any;
    state.messageState = action.payload.messageState as any;
};

export const EODocumentInitializePending = (state: Draft<EOState>) => {
    state.messageState.messageDocument = { initialized: false };
};

export const EODocumentInitializeFulfilled = (
    state: Draft<EOState>,
    {
        payload: { dataChanges, initialized, preparation, decryption, errors, messageImages },
    }: PayloadAction<EODocumentInitializeParams>
) => {
    const messageState = getMessageState(state);

    if (messageState) {
        Object.assign(messageState.data, dataChanges);
        if (messageState.messageDocument) {
            messageState.messageDocument.initialized = initialized;
            messageState.messageDocument.document = preparation?.document;
            messageState.messageDocument.plainText = preparation?.plainText;
        }
        messageState.decryption = decryption;
        messageState.errors = errors;
        messageState.loadRetry = messageState.loadRetry ? messageState.loadRetry + 1 : 1;
        messageState.messageImages = messageImages;
    }
};
<commit_msg>Fix type issue<commit_after>import { Draft } from 'immer';
import { PayloadAction } from '@reduxjs/toolkit';
import { RootState } from './eoStore';
import { eoMessageSelector, eoMessageStateSelector } from './eoSelectors';
import {
    EOState,
    EOMessageParams,
    EOTokenParams,
    EOInitResult,
    EOInitParams,
    EOMessage,
    EODocumentInitializeParams,
    EOLoadEmbeddedResults,
    EOLoadEmbeddedParams,
    EOLoadRemoteParams,
    EOLoadRemoteResults,
    EOMessageReply,
} from './eoType';
import { MessageRemoteImage, MessageState } from '../messages/messagesTypes';
import { getEmbeddedImages, getRemoteImages, updateImages } from '../../helpers/message/messageImages';
import { markEmbeddedImagesAsLoaded } from '../../helpers/message/messageEmbeddeds';
import {
    loadBackgroundImages,
    loadElementOtherThanImages,
    removeProtonPrefix,
} from '../../helpers/message/messageRemotes';
import { initialState } from './eoSlice';

export const getMessageState = (state: Draft<EOState>) => eoMessageStateSelector({ eo: state } as RootState);

export const getEOMessage = (state: Draft<EOState>) => eoMessageSelector({ eo: state } as RootState);

// Get image refs in the state for those in data
const getStateImages = <T extends { image: MessageRemoteImage }>(data: T[], messageState: MessageState) => {
    const remoteImages = getRemoteImages(messageState);

    return data.map(({ image: inputImage, ...rest }) => {
        const image = remoteImages.find((image) => image.id === inputImage.id) as MessageRemoteImage;
        return { image, ...rest };
    });
};

export const reset = (state: Draft<EOState>) => {
    Object.assign(state, initialState);
};

export const initFulfilled = (
    state: Draft<EOState>,
    action: PayloadAction<EOInitResult, string, { arg: EOInitParams }>
) => {
    const { token, decryptedToken, password } = action.payload;

    if (token) {
        state.encryptedToken = token;
    }
    if (decryptedToken) {
        state.decryptedToken = decryptedToken;
    }
    if (password) {
        state.password = password;
    }
    state.isStoreInitialized = true;
};

export const loadEOTokenFulfilled = (
    state: Draft<EOState>,
    action: PayloadAction<string, string, { arg: EOTokenParams }>
) => {
    state.encryptedToken = action.payload;
};

export const loadEOMessageFulfilled = (
    state: Draft<EOState>,
    action: PayloadAction<{ eoMessage: EOMessage; messageState: MessageState }, string, { arg: EOMessageParams }>
) => {
    state.password = action.meta.arg.password;
    state.decryptedToken = action.meta.arg.token;
    state.message = action.payload.eoMessage as any;
    state.messageState = action.payload.messageState as any;
};

export const EODocumentInitializePending = (state: Draft<EOState>) => {
    state.messageState.messageDocument = { initialized: false };
};

export const EODocumentInitializeFulfilled = (
    state: Draft<EOState>,
    {
        payload: { dataChanges, initialized, preparation, decryption, errors, messageImages },
    }: PayloadAction<EODocumentInitializeParams>
) => {
    const messageState = getMessageState(state);

    if (messageState) {
        Object.assign(messageState.data!, dataChanges);
        if (messageState.messageDocument) {
            messageState.messageDocument.initialized = initialized;
            messageState.messageDocument.document = preparation?.document;
            messageState.messageDocument.plainText = preparation?.plainText;
        }
        messageState.decryption = decryption;
        messageState.errors = errors;
        messageState.loadRetry = messageState.loadRetry ? messageState.loadRetry + 1 : 1;
        messageState.messageImages = messageImages;
    }
};

{'fraction_non_alphanumeric': 0.11655042918454936, 'fraction_numerical': 0.000536480686695279, 'mean_word_length': 3.77706598334401, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5209681', 'n_tokens_mistral': 2150, 'n_tokens_neox': 2164, 'n_words': 474}